关系:java对象之间的关系
分类:1. 一对多,2. 多对一,3. 一对一,4. 多对多,
为什么要使用结果映射
原因一:解决表字段名和对象属性名不一样的情况.
原因二:关联对象查询,在mybatis不会默认查询出来,需要自己查询结果并且通过resultMap来配置
@Data
public class Department {
private Long id;
private String name;
}
@Data
public class Employee {
private Integer id;
private String name;
/*在多对一种:通过对象引用来表明关系*/
private Department dept;
}
public interface EmployeeMapper {
List<Employee> loadAll();
}
<resultMap id="employeeMap" type="cn.itsource.many2one.domain.Employee">
<id property="id" column="id"/>
<result property="name" column="name"/>
<association property="dept" javaType="cn.itsource.many2one.domain.Department">
<id property="id" column="did"/>
<result property="name" column="dname"/>
association>
resultMap>
<select id="loadAll" resultMap="employeeMap">
SELECT
e.*,
d.id did,
d.NAME dname
FROM
employee e
JOIN department d ON e.dept_id = d.id
select>
<resultMap id="employeeMap" type="cn.itsource.many2one.domain.Employee">
<id property="id" column="id"/>
<result property="name" column="name"/>
<association property="dept"
column="dept_id"
select="cn.itsource.many2one.mapper.EmployeeMapper.loadDeptById"
javaType="cn.itsource.many2one.domain.Department">
association>
resultMap>
<select id="loadAll" resultMap="employeeMap">
SELECT * from employee
select>
<select id="loadDeptById" parameterType="long" resultType="cn.itsource.many2one.domain.Department">
SELECT * from department where id = #{id}
select>
@Test
public void testMany2One() {
//1.获取连接
SqlSession session = MybatisUtil.getSession();
//2.得到mapper对象
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
//3.执行操作
mapper.loadAll().forEach(a -> {
System.out.println(a);
});
//4.释放资源
session.close();
}
@Data
public class Department {
private Long id;
private String name;
/*在一对多中:通过集合引用属性来表明关系*/
private List<Employee> employees;
}
@Data
public class Employee {
private Long id;
private String name;
}
public interface DepartmentMapper {
List<Department> loadAll();
}
<resultMap id="departmentMap" type="cn.itsource.one2many.domain.Department">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="employees"
column="id"
select="loadEmpsByDeptId"
ofType="cn.itsource.one2many.domain.Employee">
collection>
resultMap>
<select id="loadAll" resultMap="departmentMap">
select * from department
select>
<select id="loadEmpsByDeptId" parameterType="long" resultType="cn.itsource.one2many.domain.Employee">
select * from employee where dept_id = #{id}
select>
<resultMap id="departmentMap" type="cn.itsource.one2many.domain.Department">
<id property="id" column="did"/>
<result property="name" column="dname"/>
<collection property="employees" ofType="cn.itsource.one2many.domain.Employee">
<id property="id" column="id"/>
<result property="name" column="name"/>
collection>
resultMap>
<select id="loadAll" resultMap="departmentMap">
SELECT
e.*,
d.id did,
d.NAME dname
FROM
employee e
JOIN department d ON e.dept_id = d.id
select>
@Test
public void testOne2Many() {
//1.获取连接
SqlSession session = MybatisUtil.getSession();
//2.得到mapper对象
DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
//3.执行操作
mapper.loadAll().forEach(a -> {
System.out.println(a);
});
//4.释放资源
session.close();
}