B056-Mybatis增强

目录

      • 域对象(domain)之间的关系
        • A 表结构上是如何设计关系的
        • B 实体对象上是如何设计关系的
        • 回顾多表查询
      • 结果映射
        • 多对一/一对一
          • Domain
            • Department
            • Employee
          • Mapper
            • EmployeeMapper
            • EmployeeMapper_嵌套结果.xml(推荐使用)
            • EmployeeMapper_嵌套查询.xml
          • Many2OneTest
        • 一对多
          • domain
            • Department
            • Employee
          • mapper
            • DepartmentMapper
            • DepartmentMapper_嵌套查询.xml(建议使用)
            • DepartmentMapper_嵌套结果.xml
          • One2ManyTest
      • 缓存

域对象(domain)之间的关系

关系:java对象之间的关系
分类:1. 一对多,2. 多对一,3. 一对一,4. 多对多,

A 表结构上是如何设计关系的

B056-Mybatis增强_第1张图片
B056-Mybatis增强_第2张图片
B056-Mybatis增强_第3张图片

B 实体对象上是如何设计关系的

B056-Mybatis增强_第4张图片

回顾多表查询

先查出宽表,取别名,然后做自定义映射
B056-Mybatis增强_第5张图片

结果映射

为什么要使用结果映射
原因一:解决表字段名和对象属性名不一样的情况.
原因二:关联对象查询,在mybatis不会默认查询出来,需要自己查询结果并且通过resultMap来配置

关联映射处理方式
嵌套查询和嵌套结果
B056-Mybatis增强_第6张图片

多对一/一对一

Domain
Department
@Data
public class Department {
    private Long id;
    private String name;
}
Employee
@Data
public class Employee {
    private Integer id;
    private String name;
    /*在多对一种:通过对象引用来表明关系*/
    private Department dept;
}
Mapper
EmployeeMapper
public interface EmployeeMapper {
    List<Employee> loadAll();
}
EmployeeMapper_嵌套结果.xml(推荐使用)
   
    <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>
EmployeeMapper_嵌套查询.xml
    
    <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>
Many2OneTest
    @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();
    }

一对多

domain
Department
@Data
public class Department {
    private Long id;
    private String name;
    /*在一对多中:通过集合引用属性来表明关系*/
    private List<Employee> employees;
}
Employee
@Data
public class Employee {
    private Long id;
    private String name;
}
mapper
DepartmentMapper
public interface DepartmentMapper {
    List<Department> loadAll();
}
DepartmentMapper_嵌套查询.xml(建议使用)
	
    <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>
DepartmentMapper_嵌套结果.xml
    
    <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>
One2ManyTest
    @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();
    }

缓存

B056-Mybatis增强_第7张图片

你可能感兴趣的:(笔记总结,mybatis)