结果映射(ResultMap)
resultMap是MyBatis中非常重要强大的元素。它在处理一些链接比较复杂的语句编辑映射语句的时候,能够代替实现同等功能的长达数千行的代码。ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
创建数据表
这里分别创建雇员表(sys_employees)和部门表(base_department)用来测试,创建语句如下:
//雇员表
create table sys_employees(
id int not null primary key auto_increment,
employe_name varchar(50) not null,
employe_code varchar(12) not null,
employe_phone varchar(11) not null,
department_code varchar(8) not null);
//部门表
create table base_department(
id int not null primary key auto_increment,
department_name varchar(20) not null,
department_code varchar(8) not null,
department_addr varchar(50) null);
接下来插入测试数据,数据如下:
添加配置
- 添加两个数据表对应的实体类
package com.kons.bean;
public class Employe {
private int id;
private String employe_name;
private String employe_code;
private String employe_phone;
private String department_code;
private Department department;
//省略get和set方法
@Override
public String toString() {
return "Employe:{"+employe_name+","+employe_code+","+department_code+","+employe_phone+"}";
}
}
package com.kons.bean;
import java.util.List;
public class Department {
private int id;
private String department_name;
private String department_code;
private String department_addr;
private List<Employe> employes;
//省略get和set方法
}
- 添加mapper.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.kons.dao.IDepartmIentDao">
</mapper>
- 添加IDepartmIentDao接口
package com.kons.dao;
import com.kons.bean.Department;
import com.kons.bean.Employe;
public interface IDepartmentDao {
//查询部门所有的雇员信息
Department findEmployeesByDepart(String code);
//查询雇员以及雇员所在的部门信息
Employe findEmployInfo(String code);
}
一对多关联查询
在mapper.xml添加配置
<resultMap id="departMap" type="department">
<id column="id" property="id"/>
<result column="department_code" property="department_code"/>
<result column="department_name" property="department_name"/>
<result column="department_addr" property="department_addr"/>
<collection property="employes" ofType="employe">
<id column="e_id" property="id"/>
<result property="employe_code" column="employe_code"/>
<result property="employe_name" column="employe_name"/>
<result property="employe_phone" column="employe_phone"/>
<result property="department_code" column="department_code"/>
</collection>
</resultMap>
<select id="findEmployeesByDepart" resultMap="departMap">
SELECT
d.id,d.department_code,d.department_name,d.department_addr,
p.id as e_id,p.employe_code,p.employe_name,p.employe_phone,p.department_code
FROM base_department d
LEFT JOIN sys_employees p ON d.department_code=p.department_code
WHERE d.department_code=#{department_code}
</select>
上面sql语句中的collection标签可以表示集合类型,其中:
- property中填写集合在Department这个bean中的属性名称,此时是employes
- ofType中填写集合的泛型
在collection标签中填写的内容关联的是Employe中的属性。
注意:当自动映射查询结果时,MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性。 这意味着如果发现了 ID 列和 id 属性,MyBatis 会将列 ID 的值赋给 id 属性。
多对一关联查询
多对一指的是在查询多方对象的时候,同时将其所关联的一方对象也查询出来。由于在查询多方对象时也是一个一个查询,所以多对一关联查询,其实就是一对一关联查询。即一对一关联查询的实现方式与多对一的实现方式是相同的。
首先在mapper.xml文件中添加配置
<resultMap id="employeMap" type="employe">
<id column="id" property="id"/>
<result property="employe_code" column="employe_code"/>
<result property="employe_name" column="employe_name"/>
<result property="employe_phone" column="employe_phone"/>
<result property="department_code" column="department_code"/>
<association property="department" javaType="department">
<id property="id" column="d_id"/>
<result column="department_code" property="department_code"/>
<result column="department_name" property="department_name"/>
<result column="department_addr" property="department_addr"/>
</association>
</resultMap>
<select id="findEmployInfo" resultMap="employeMap">
SELECT
d.id as d_id,d.department_code,d.department_name,d.department_addr,
p.id,p.employe_code,p.employe_name,p.employe_phone,p.department_code
FROM sys_employees p
LEFT JOIN base_department d ON p.department_code=d.department_code
WHERE p.employe_code=#{employe_code}
</select>
其中association标签体现Department和Employe的关联关系:
- property里面填写在Employe类中的属性名称
- javaType里面填写在Employe类中的的属性类型
测试
修改App.java文件:
package com.kons;
import com.kons.bean.Department;
import com.kons.bean.Employe;
import com.kons.dao.IDepartmentDao;
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.apache.log4j.BasicConfigurator;
import java.io.Reader;
public class App
{
private static String mybatisConfig="mybatis-config.xml";
public static void main( String[] args ) throws Exception {
BasicConfigurator.configure();
try (SqlSession sqlSession=App.getSqlSession()){
IDepartmentDao departmentDao=sqlSession.getMapper(IDepartmentDao.class);
Department department=departmentDao.findEmployeesByDepart("DP003");
Employe employee=departmentDao.findEmployInfo("E000001");
System.out.println(employee.toString());
for (Employe employe:department.getEmployes()){
System.out.println(employe.toString());
}
}
}
//省略sqlSessionFactory以及getSqlSession();
}
测试结果如下: