新建Mybatis项目,搭建MyBatis框架过程不再赘述,参考前期博客:开始准备工作,此处只给出新内容
准备工作
创两个表,多对一关系(一个部门可以有多个员工),简单添加几条数据,如下:
t_dept
表,did主键自增
t_emp
表,eid主键自增
创建两个pojo类:
Emp.java
package com.mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
}
Dept.java
package com.mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString
public class Dept {
private Integer did;
private String deptName;
}
1、resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
EmpMapper
接口
/**
* 查询所有员工信息
*/
List<Emp> getAllEmp();
EmpMapper
映射
<resultMap id="empResultMap" type="Emp">
<id property="eid" column="eid" />
<result property="empName" column="emp_name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="email" column="email" />
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select * from t_emp
</select>
测试方法
@Test
public void testGetAllEmp() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
mapper.getAllEmp().forEach(System.out::println);
}
运行结果
2、多对一映射处理
在Emp.java
添加一行属性:
private Dept dept;
a>级联方式处理映射关系
EmpMapper
接口
/**
* 根据id查询员工所对应的部门信息
*/
Emp getEmpAndDept(@Param("eid") Integer eid);
EmpMapper
映射
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="eid" column="eid" />
<result property="empName" column="emp_name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="email" column="email" />
<!--级联属性赋值-->
<result property="dept.did" column="did" />
<result property="dept.deptName" column="dept_name" />
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
select * from t_emp join t_dept on t_emp.did=t_dept.did where t_emp.eid = #{eid}
</select>
测试方法
@Test
public void testGetEmpAndDept() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
System.out.println(mapper.getEmpAndDept(5));
}
运行结果
b>使用association处理映射关系
EmpMapper
映射
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid" />
<result property="empName" column="emp_name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="email" column="email" />
<!--association处理映射-->
<association property="dept" javaType="Dept">
<id property="did" column="did" />
<result property="deptName" column="dept_name" />
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
select * from t_emp join t_dept on t_emp.did=t_dept.did where t_emp.eid = #{eid}
</select>
运行结果同上
c>分步查询
EmpMapper
接口
/**
* 根据id查询员工所对应的部门信息(通过分步查询)
* 分步查询第一步:查询员工信息
*/
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
EmpMapper
映射
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="eid" column="eid" />
<result property="empName" column="emp_name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="email" column="email" />
<association property="dept" select="com.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did" />
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid = #{eid}
</select>
DeptMapper
接口
/**
* 根据id查询员工所对应的部门信息(通过分步查询)
* 分步查询第二步:查询部门信息
*/
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
DeptMapper
映射
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did= #{did}
</select>
测试方法
@Test
public void testGetEmpAndDeptByStep() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
System.out.println(mapper.getEmpAndDeptByStepOne(2));
}
运行结果
3、一对多映射处理
在Dept.java
添加一行属性:
private List<Emp> emps;
a>使用collection处理映射关系
DeptMapper
接口
/**
* 根据部门id查询部门以及部门中的员工信息
*/
Dept getDeptAndEmp(@Param("did") Integer did);
DeptMapper
映射
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
<!--collection处理映射-->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid" />
<result property="empName" column="emp_name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="email" column="email" /> <!--此处不用设置Emp的did属性-->
</collection>
</resultMap>
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select * from t_dept join t_emp on t_dept.did=t_emp.did where t_dept.did=#{did}
</select>
测试方法
@Test
public void testGetDeptAndEmp() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
System.out.println(mapper.getDeptAndEmp(1));
}
运行结果
Dept(did=1, deptName=A, emps=[
Emp(eid=1, empName=张飞, age=36, sex=男, email=100@163.com, dept=null),
Emp(eid=4, empName=孙尚香, age=32, sex=女, email=103@sohu.com, dept=null)
])
b>分步查询
DeptMapper
接口
/**
* 根据id查询部门以及部门中的员工信息(通过分步查询)
* 分步查询第一步:查询部门信息
*/
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
DeptMapper
映射
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
<collection property="emps" select="com.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="did"/>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
select * from t_dept where did= #{did}
</select>
EmpMapper
接口
/**
* 根据id查询部门以及部门中的员工信息(通过分步查询)
* 分步查询第二步:查询员工信息
*/
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
EmpMapper
映射
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did = #{did}
</select>
测试方法
@Test
public void testGetDeptAndEmpByStep() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
System.out.println(mapper.getDeptAndEmpByStepOne(1));
}