本章节主要介绍mybatis的resultMap的用法,包含了级联查询、关联查询、懒加载、鉴别器的相关讲解和代码用例
首先这是测试代码用到的实体类POJO
Employee.java:
package com.wcg.mybatis.entity;
import java.io.Serializable;
/**
* @author wcg
* @create 2019-05-06 10:04
*/
public class Employee implements Serializable {
private static final long serialVersionUID = -6248806586248836314L;
private Integer id;
private String lastName;
private String gender;
private String email;
private Department department;
public Employee() {
}
// setting getting 省略
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
", department=" + department +
'}';
}
}
Department.java
package com.wcg.mybatis.entity;
import java.io.Serializable;
import java.util.List;
/**
* @author wcg
* @create 2019-05-07 23:59
*/
public class Department implements Serializable {
private static final long serialVersionUID = -6380361622841645277L;
private Integer id;
private String deptName;
private List<Employee> employeeList;
public Department() {
}
// setting getting 省略
@Override
public String toString() {
return "Department{" +
"id=" + id +
", deptName='" + deptName + '\'' +
", employeeList=" + employeeList +
'}';
}
}
数据库的建表语句DDL:
CREATE TABLE `tbl_employee` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`dept_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dept_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
一、自定义结果集映射resultMap
EmployeeMapper.java 接口:
package com.wcg.mybatis.dao;
import com.wcg.mybatis.entity.Employee;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* @author wcg
* @create 2019-05-06 17:48
*/
public interface EmployeeMapperPlus {
Employee getEmpById(Integer id);
}
EmployeeMapper.xml sql映射文件:
<?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.wcg.mybatis.dao.EmployeeMapperPlus">
<!--
自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
-->
<resultMap id="employee" type="com.wcg.mybatis.entity.Employee">
<!--
指定主键列:
id定义主键会底层有优化;
column:字段
property:JavaBean属性
-->
<id column="id" property="id"></id>
<!-- 定义普通列封装规则 -->
<result column="last_name" property="lastName"></result>
<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
<result column="gender" property="gender"></result>
<result column="email" property="email"></result>
</resultMap>
<!-- resultMap:自定义结果集映射规则; -->
<!-- Employee getEmpById(Integer id); -->
<select id="getEmpById" resultMap="employee">
select id,last_name,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
测试代码:
package com.wcg.mybatis.test;
import com.wcg.mybatis.dao.EmployeeMapperPlus;
import com.wcg.mybatis.entity.Employee;
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.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
/**
* @author wcg
* @create 2019-05-08 21:55
*/
public class ResultMapTest {
public SqlSessionFactory sessionFactory = null;
@BeforeEach
public void test()throws Exception{
// 根据全局配置文件(xml)创建一个SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void resultMapTest(){
// 获取 SqlSession 的实例 。SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。通过 SqlSession 实例来直接执行已映射的 SQL 语句
SqlSession sqlSession = null;
try {
sqlSession = sessionFactory.openSession();
// 通过获取接口代理对象来执行sql语句
EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);
Employee employee = mapper.getEmpById(2);
System.out.println(employee);
} finally {
// 资源关闭,放在finally中确保一定会执行
sqlSession.close();
}
}
}
测试结果:
DEBUG 05-08 22:43:39,908 ==> Preparing: select id,last_name,email,gender from tbl_employee where id = ? (BaseJdbcLogger.java:145)
DEBUG 05-08 22:43:39,989 ==> Parameters: 2(Integer) (BaseJdbcLogger.java:145)
DEBUG 05-08 22:43:40,022 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=2, lastName='BB', gender='0', email='[email protected]', department=null}
二、级联属性封装结果集
EmployeeMapper.java 接口方法:
Employee getEmpByIdWithDeptName(Integer id);
EmployeeMapper.xml sql映射文件:
<?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.wcg.mybatis.dao.EmployeeMapperPlus">
<!--
联合查询:级联属性封装结果集