我是靠谱客的博主 满意小天鹅,最近开发中收集的这篇文章主要介绍Mybatis(6):返回结果封装ResulMap的高级属性--association标签,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
四部曲:
1.写接口 + 2.写映射sql + 3.把mapper注册到mybatis的配置文件 + 4.写单元测试和运行
(1)新建两个实体类User.java 和 Role.java:
User.java(必须包含一个复杂熟悉Role,因为我们将通过association对嵌入的javaBean进行映射):
package com.smbms.entities;
import java.util.Date;
import java.util.List;
public class User {
private Integer id;
private String userCode;
private String userName;
private String userPassword;
private Integer gender;
private Date birthday;
private String phone;
private String address ;
private Integer userRole;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
private String userRoleName;
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public User(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday,
String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy,
Date modifyDate) {
super();
this.id = id;
this.userCode = userCode;
this.userName = userName;
this.userPassword = userPassword;
this.gender = gender;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userRole = userRole;
this.createdBy = createdBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
public User() {
super();
// TODO 自动生成的构造函数存根
}
@Override
public String toString() {
return "User [id=" + id + ", userCode=" + userCode + ", userName=" + userName + ", userPassword=" + userPassword
+ ", gender=" + gender + ", birthday=" + birthday + ", phone=" + phone + ", address=" + address
+ ", userRole=" + userRole + ", createdBy=" + createdBy + ", creationDate=" + creationDate
+ ", modifyBy=" + modifyBy + ", modifyDate=" + modifyDate + "]";
}
}
Role.java:
package com.smbms.entities;
import java.math.BigInteger;
import java.util.Date;
public class Role {
private Integer id;
private String roleCode;
private String roleName;
private BigInteger createdBy;
private Date creationDate;
private BigInteger modifyBy;
private Date modifyDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public BigInteger getCreatedBy() {
return createdBy;
}
public void setCreatedBy(BigInteger createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public BigInteger getModifyBy() {
return modifyBy;
}
public void setModifyBy(BigInteger modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
@Override
public String toString() {
return "Role [id=" + id + ", roleCode=" + roleCode + ", roleName=" + roleName + ", createdBy=" + createdBy
+ ", creationDate=" + creationDate + ", modifyBy=" + modifyBy + ", modifyDate=" + modifyDate + "]";
}
public Role(Integer id, String roleCode, String roleName, BigInteger createdBy, Date creationDate,
BigInteger modifyBy, Date modifyDate) {
super();
this.id = id;
this.roleCode = roleCode;
this.roleName = roleName;
this.createdBy = createdBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
public Role() {
super();
// TODO 自动生成的构造函数存根
}
}
(2)在接口类UserMapper.java中进行方法定义:
package com.smbms.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.smbms.entities.User;
public interface UserMapper {
public List<User> getUserListByRoleId (@Param("UserRole") Integer RoleId);
}
(3)在sql映射文件 UserMapper.xml 完成配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.smbms.dao.UserMapper">
<resultMap type="com.smbms.entities.User" id="UserRoleResult">
<id property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<!-- -->
<result property="userRole" column="userRole"/>
<association property="role" javaType="com.smbms.entities.Role">
<id property="id" column="r_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association>
</resultMap>
<select id="getUserListByRoleId"
parameterType="Integer" resultMap="UserRoleResult">
select u.* ,r.id as r_id,r.roleCode,r.roleName
from smbms_user u,smbms_role r
where u.userRole=#{UserRole} and u.userRole=r.id
</select>
</mapper>
(4)在全局配置文件注册映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<properties resource="datasources.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.usename}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/smbms/dao/UserMapper.xml"/>
<mapper resource="com/smbms/dao/ProviderMapper.xml"/>
</mappers>
</configuration>
(5)编写单元测试UserTest:
package com.smbms.entities;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.smbms.dao.ProviderMapper;
import com.smbms.dao.UserMapper;
public class UserTest {
public SqlSession getSqlSession() throws IOException{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = build.openSession();
return openSession;
}
//association标签对应于User中的一个复杂属性
@Test
public void test11() throws IOException{
System.out.println("===============test add!");
List<User> userlist = new ArrayList<User> ();
User user = new User();
int count = 0;
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
try{
System.out.println(111);
userlist = mapper.getUserListByRoleId(110);
System.out.println(112);
for(User a:userlist){
System.out.println(a.toString());
System.out.println(a.getRole().getId()+"**"+a.getRole().getRoleCode()+"**"+a.getRole().getRoleName());
}
System.out.println(113);
}catch(Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
}
综上,完成一个返回结果封装ResulMap的高级属性--association标签的Demo。
最后
以上就是满意小天鹅为你收集整理的Mybatis(6):返回结果封装ResulMap的高级属性--association标签的全部内容,希望文章能够帮你解决Mybatis(6):返回结果封装ResulMap的高级属性--association标签所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复