MyBatis基础篇之结果映射ResultMap级联属性封装

一、前言

前面一节,我们简单的介绍了使用ResultMap封装简单单表数据查询结果,查询结果很简单,就那几个属性,那么如果我们查询的用户信息里面有角色信息,一个用户对应一个角色,我们现在要求查处用户信息的同时,查出用户的角色信息,那么这个时候我们要怎么处理呢?

二、案例

♦参照<<MyBatis基础篇之简单入门>><<MyBatis基础篇之结果映射ResultMap的简单使用>>等相关章节,重新搭建一个工程MyBatisComplexResultMapDemo

♦在MySQL下新建数据库表t_user和t_role,并插入若干条数据

CREATE TABLE t_user (
  id int(10) NOT NULL AUTO_INCREMENT,
  loginId varchar(20) DEFAULT NULL,
  userName varchar(100) DEFAULT NULL,
  roleId int(10),
  note varchar(255) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

INSERT INTO t_user(loginId,userName,roleId,note) VALUES ('queen', '奎恩', 1, '专门负责提鞋的。。。');
INSERT INTO t_user(loginId,userName,roleId,note) VALUES ('king', '金狮子', 2, '磁性果实能力');
INSERT INTO t_user(loginId,userName,roleId,note) VALUES ('Lucy', '路西', 3, '打败多弗朗明哥。。。');
=========================================================
CREATE TABLE t_role (
  id int(10) NOT NULL AUTO_INCREMENT,
  roleName varchar(20) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

INSERT INTO t_role(roleName) VALUES ('自定义角色');
INSERT INTO t_role(roleName) VALUES ('前海贼');
INSERT INTO t_role(roleName) VALUES ('未来海贼王');

♦新建JavaBean类 User

/**
 * @since 2017-08-06
 * @author queen 
 * 定义一个Java类
 *
 */
public class User {
	// ID,唯一性
	private int id;
	// 登录ID
	private String loginId;
	// 用户名
	private String userName;
	// 角色
	private Role role;
	// 备注
	private String note;

        public User(){
 
        }
         
	public User(int id, String loginId, String userName, Role role, String note) {
		this.id = id;
		this.loginId = loginId;
		this.userName = userName;
		this.role = role;
		this.note = note;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", loginId=" + loginId + ", userName="
				+ userName + ", role=" + role + ", note=" + note + "]";
	}

}

♦新建JavaBean类 Role

/**
 * @since 2017-08-06
 * @author queen 
 * 定义一个Java类Role
 *
 */
public class Role {
	private int id;
	private String roleName;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

        public Role(){
 
        }
        
	public Role(int id, String roleName) {
		this.id = id;
		this.roleName = roleName;
	}

	@Override
	public String toString() {
		return "Role [id=" + id + ", roleName=" + roleName + "]";
	}

}

♦新建接口类UserMapper.java,增加接口方法getUserById

import com.queen.mybatis.bean.User;
/**
 * @since 2017-08-07
 * @author queen 
 * 定义一个UserMapper接口
 *
 */
public interface UserMapper {
	
	/**
	 * 根据ID查询User信息,返回一条记录User
	 * @param id
	 * @return
	 */
	public User getUserById(int id);
	
}

♦新建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.queen.mybatis.mapper.UserMapper">

	<resultMap type="com.queen.mybatis.bean.User" id="userResultMap">
		<id property="id" column="id"/>
	    <result property="loginId" column="loginId" />
	    <result property="userName" column="userName"/>
	    <result property="role.id" column="role_id"/>
	    <result property="role.roleName" column="roleName" />
	</resultMap>
	
	<select id="getUserById" resultMap="userResultMap">
		select m.id id, m.loginId loginId, m.userName userName, m.roleId roleId, n.id role_id, n.roleName roleName 
			from t_user m left join t_role n on m.roleId=n.id
			where m.id=#{id}
	</select>
	
</mapper>

♦新建测试类MyBatisTest,增加测试方法testGetUserResultMap()

@Test
public void testGetUserResultMap() throws IOException {
	SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
	SqlSession openSession = sqlSessionFactory.openSession();
	try {
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		User user = mapper.getUserById(1);
		System.out.println(user);
		System.out.println(user.getRole());
	} finally {
		openSession.close();
	}
 
}

♦测试控制台打印效果如下:

2017-08-07 23:36:14,897 [main] [com.queen.mybatis.mapper.UserMapper.getUserById]-[DEBUG] ==>  Preparing: select m.id id, m.loginId loginId, m.userName userName, m.roleId roleId, n.id role_id, n.roleName roleName from t_user m left join t_role n on m.roleId=n.id where m.id=? 
2017-08-07 23:36:14,955 [main] [com.queen.mybatis.mapper.UserMapper.getUserById]-[DEBUG] ==> Parameters: 1(Integer)
2017-08-07 23:36:14,997 [main] [com.queen.mybatis.mapper.UserMapper.getUserById]-[DEBUG] <==      Total: 1
User [id=1, loginId=queen, userName=奎恩, role=Role [id=1, roleName=自定义角色], note=null]
Role [id=1, roleName=自定义角色]

♦完整工程结构目录如下

MyBatis基础篇之结果映射ResultMap级联属性封装的照片 - 1

至此,我们关于MyBatis基础篇之结果映射ResultMap级联属性封装介绍完毕。我们下一节将介绍如何通过resultMap的association自定义结果映射。

博客地址:http://www.marsitman.com/mybatis/mybatis-resultmap-difficult.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。

 

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!






文件名称:MyBatis基础篇之结果映射ResultMap级联属性封装

更新日期:2017-08-07

作者信息:榕瑾

提示:下载后请检查MD5值,欢迎捐赠本站以及广告合作!

下载地址:点击下载 【文件大小:17 KB】

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注