Mybatis implements multi-table query, cascade, cache

Get into the habit of writing together! This is the second day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

one-to-one relational query

  • Scenario: Query employee information (Emp table) and department (Dept table) information based on employee number
  • Need to create a Dept attribute in the Emp entity class
<!--Emp findEmpJoinDeptByEmpno(int empno);-->
    <resultMap id="empJoinDept" type="emp">
        <!--设置emp本身的八个属性的映射关系-->
        <id property="empno" column="empno"></id>
        <result property="ename" column="ename"></result>
        <result property="job" column="job"></result>
        <result property="sal" column="sal"></result>
        <result property="hiredate" column="hiredate"></result>
        <result property="mgr" column="mgr"></result>
        <result property="comm" column="comm"></result>
        <result property="deptno" column="deptno"></result>
        <!--
        association 处理一对一
        封装一对一信息关系的标签
        property  emp类的属性名
        javaType  用哪个类的对象给属性赋值
        -->
        <association property="dept" javaType="dept">
            <id column="deptno" property="deptno"></id>
            <result column="dname" property="dname"></result>
            <result column="loc" property="loc"></result>
        </association>
    </resultMap>
    <select id="findEmpJoinDeptByEmpno" resultMap="empJoinDept" >
        select * from
        emp e
        left join dept  d
        on e.deptno =d.deptno
        where empno = #{empno}
    </select>
复制代码

One-to-many association query

  • Scenario: Query department information (Dept table) and all employee information in the department (Emp table) according to the department number
  • Because a department has multiple employees, we need to encapsulate a List collection in the Dept entity class to store the queried Emp objects
<mapper namespace="com.msb.mapper.DeptMapper">
    <!--Dept findDeptJoinEmpsByDeptno(int deptno);-->
    <resultMap id="deptJoinEmps" type="dept">
        <id column="deptno" property="deptno"></id>
        <result column="dname" property="dname"></result>
        <result column="loc" property="loc"></result>
        <!--处理一对多关系的标签-->
        <collection property="empList" ofType="emp" >
            <!--设置emp本身的八个属性的映射关系-->
            <id property="empno" column="empno"></id>
            <result property="ename" column="ename"></result>
            <result property="job" column="job"></result>
            <result property="sal" column="sal"></result>
            <result property="hiredate" column="hiredate"></result>
            <result property="mgr" column="mgr"></result>
            <result property="comm" column="comm"></result>
            <result property="deptno" column="deptno"></result>
        </collection>
    </resultMap>
    <select id="findDeptJoinEmpsByDeptno" resultMap="deptJoinEmps">
        select * from dept d left join emp e on d.deptno =e.deptno where d.deptno =#{deptno}
    </select>
复制代码

Many-to-many association query

  • Scenario: Query project information according to the project number, and information of all employees involved in the project

image.png

  • The relationship shown in the figure above,
  • So we need to create a List collection in the Project table to encapsulate the ProjectRecord object,
  • Then we create an attribute value of Emp in the ProjectRecord table to store the emp object
<mapper namespace="com.msb.mapper.ProjectMapper">
    <!--Project findProjectJoinEmpsByPid(int pid);-->
    <resultMap id="projectJoinEmps" type="project">
        <id column="pid" property="pid"></id>
        <result column="pname" property="pname"></result>
        <result column="money" property="money"></result>
        <!--一对多 集合属性 collection-->
        <collection property="projectRecords" ofType="projectRecord">
            <id column="empno" property="empno"></id>
            <id column="pid" property="pid"></id>
            <!--一对一 -->
            <association property="emp" javaType="emp">
                <id property="empno" column="empno"></id>
                <result property="ename" column="ename"></result>
                <result property="job" column="job"></result>
                <result property="sal" column="sal"></result>
                <result property="hiredate" column="hiredate"></result>
                <result property="mgr" column="mgr"></result>
                <result property="comm" column="comm"></result>
                <result property="deptno" column="deptno"></result>
            </association>
        </collection>
    </resultMap>
    <select id="findProjectJoinEmpsByPid"  resultMap="projectJoinEmps">
        select * from
        project p
        left join projectrecord pr
        on p.pid = pr.pid
        left join emp e
        on e.empno = pr.empno
        where p.pid= #{pid}
    </select>
复制代码

Cascading queries (immediate loading and lazy loading)

  • Cascading query is to create two interfaces, two xml configuration files, and query two tables with the same conditions

  • Cascading queries eliminate the need to manually query and assemble data

  • The code configured in the EmpMapper.xml file:

前缀 略
<mapper namespace="com.msb.mapper.EmpMapper">
    <!--List<Emp> findEmpsByDeptno(int deptno);-->
    <select id="findEmpsByDeptno" resultType="emp">
        select * from emp where deptno =#{deptno}
    </select>
复制代码
  • Code configured in the DeptMapper.xml file
<mapper namespace="com.msb.mapper.DeptMapper">
    <!--Dept findDeptByDeptno(int deptno);
    select="com.msb.mapper.EmpMapper.findEmpsByDeptno" 调用的另一个SQL语句
    javaType="list"  实体类的属性数据类型
    column="deptno"  给另一个SQL语句传入的参数列
    jdbcType="INTEGER" 参数对应JDBC的数据类型
    fetchType="eager"  加载方式 eager 积极加载  lazy延迟加载-->
    <resultMap id="deptJoinEmps" type="dept">
        <id property="deptno" column="deptno"></id>
        <result property="dname" column="dname"></result>
        <result property="loc" column="loc"></result>
        <collection property="empList"
                    select="com.msb.mapper.EmpMapper.findEmpsByDeptno"
                    javaType="list"
                    column="deptno"
                    jdbcType="INTEGER"
                    fetchType="eager"
        >
        </collection>
    </resultMap>
    <select id="findDeptByDeptno" resultMap="deptJoinEmps">
        select * from dept where deptno =#{deptno}
    </select>
复制代码

L1 cache

image.png

  • The first-level cache in mybatis is enabled by default, and the data is stored in the SQLSession.
//如果之间执行了增删改查,sqlSession调用了commit方法,它会自动清空一级缓存
sqlSession.commit();    
复制代码

L2 cache

image.png

  • The second-level cache is a cache marked by namespace and must be manually opened.
  • Requires that the entity class must be instantiated
  • Global switch: Turn on the second level cache in the tag configuration in the sqlMapConfig.xml file
	<settings>

    	<setting name="cacheEnabled" value="true"/>

	</settings>
复制代码
  • Sub switch: enable the cache in the mapper file where the second level cache is to be enabled
<mapper namespace="com.msb.mapper.EmployeeMapper">
	<cache/>
</mapper>
复制代码

Annotation development

  • Annotation development is written above the abstract methods in the interface. It is suitable for writing some basic query statements, and statements that are not frequently changed.
	//查询语句
    @Select("select * from dept where deptno =#{deptno}")
    Dept findByDeptno(int deptno);
    //修改语句
    @Update("update dept set dname =#{dname}, loc =#{loc} where deptno =#{deptno}")
    int updateDept(Dept dept);
    //增加语句
    @Insert("insert into dept values(DEFAULT,#{dname},#{loc})")
    int addDept(Dept dept);
    //删除语句
    @Delete("delete from dept where deptno =#{deptno}")
    int removeDept(int deptno)
复制代码

There is a gap of 108,000 miles between ideal and reality.

Guess you like

Origin juejin.im/post/7088315120668901413