王小码

导航

Mybaits(9)MyBatis级联-2

一、鉴别器和一对多级联

1.完善体检表,分为男雇员体检和女雇员体检表

(1)持久层dao编写

package com.xhbjava.dao;

import com.xhbjava.domain.MaleHealthForm;
/**
 * 男性体检表
 * @author Mr.wang
 *@date    2020年2月27日
 */
public interface IMaleHealthFormDao {
    public MaleHealthForm getMaleHealthForm(Long empId);

}
package com.xhbjava.dao;

import com.xhbjava.domain.FemaleHealthForm;
/**
 * 女体检表
 * @author Mr.wang
 *@date    2020年2月27日
 */
public interface IFemaleHealthFormDao {
    public FemaleHealthForm getFemaleHealthForm(Long empId);

}
package com.xhbjava.dao;

import com.xhbjava.domain.Employee;
/**
 * 雇员接口
 * @author Mr.wang
 *@date    2020年2月27日
 */
public interface IEmployeeDao {
    public Employee getEmployee(Long id);

}

(2)持久层对于的mapper文件编写

<?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.xhbjava.dao.IMaleHealthFormDao">
    <!-- 查询任务 -->
    <select id="getMaleHealthForm" parameterType="Long"
        resultType="com.xhbjava.domain.MaleHealthForm">
        select id, heart, liver, spleen, lung, kidney, prostate,
        note from
        t_male_health_form where emp_id = #{id}
    </select>

</mapper>
<?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.xhbjava.dao.IFemaleHealthFormDao">
    <!-- 查询任务 -->
    <select id="getFemaleHealthForm" parameterType="Long"
        resultType="com.xhbjava.domain.FemaleHealthForm">
        select id, heart, liver, spleen, lung, kidney, uterus, note
        from
        t_female_health_form where emp_id = #{id}
    </select>

</mapper>
<?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.xhbjava.dao.IEmployeeDao">
    <resultMap type="com.xhbjava.domain.Employee" id="employee">
        <id column="id" property="id" />
        <result column="real_name" property="realName" />
        <result column="sex" property="sex"
            typeHandler="com.xhbjava.typeHandler.SexTypeHandler" />
        <result column="birthday" property="birthday" />
        <result column="mobile" property="mobile" />
        <result column="email" property="email" />
        <result column="position" property="position" />
        <result column="note" property="note" />
        <association property="workcard" column="id"
            select="com.xhbjava.dao.IWorkCardDao.getWorkCard" />
        <collection property="employeeTaskList" column="id"
            select="com.xhbjava.dao.IEmployeeTaskDao.getEmployeeTaskByEmpId" />
        <discriminator javaType="long" column="sex">
            <case value="1" resultMap="femaleHealthFormDao"></case>
            <case value="2" resultMap="maleHealthFormDao"></case>
        </discriminator>
    </resultMap>
    <resultMap type="com.xhbjava.domain.FemaleEmployee"
        id="femaleHealthFormDao" extends="employee">
        <association property="femaleHealthForm" column="id"
            select="com.xhbjava.dao.IFemaleHealthFormDao.getFemaleHealthForm" />
    </resultMap>
    <resultMap type="com.xhbjava.domain.MaleEmployee"
        id="maleHealthFormDao" extends="employee">
        <association property="maleHealthForm" column="id"
            select="com.xhbjava.dao.IMaleHealthFormDao.getMaleHealthForm" />
    </resultMap>
    <select id="getEmployee" parameterType="long"
        resultMap="employee">
        select id,rea_name,as realName,sex,birthday,mobile,email,position,note from
        t_employee where id = #{id}
    </select>
</mapper>

我们来分析下IEmployeeDao.xml:

association:对工牌进行一对一级联,我们在前面分析过。
collection:一对多级联,select元素指向sql,通过column制定的sql字段作为参数进行传递,然后将结果返给雇员属性employeeTaskList。
discriminator:鉴别器,属性column代表使用哪个字段继续鉴别,这里是sex,子元素case用来进行区分,类似Java的switch...case...语句。resultMap属性表示用哪个ResultMap去映射,例如sex=1,则使用
femaleHealthFormDao进行映射。
对于雇员体检表id为employee的resultMap,通过femaleHealthFormDao和maleHealthFormDao通过extends元素继承。从而可以通过assocation元素去执行关联的字段和sql。

(3)编写测试类进测试
@Test
    public void getEmployeeTaskByEmpId() {
        Logger logger  = Logger.getLogger(MybatisJunitTest.class);
        Employee employee  = employeeDao.getEmployee(1l);
        System.out.println("===="+employee.getBirthday());
        logger.info(employee.getBirthday());
    }

 

2020-02-27 16:34:33,577 0      [           main] DEBUG ache.ibatis.logging.LogFactory  - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2020-02-27 16:34:34,204 627    [           main] DEBUG source.pooled.PooledDataSource  - PooledDataSource forcefully closed/removed all connections.
2020-02-27 16:34:34,205 628    [           main] DEBUG source.pooled.PooledDataSource  - PooledDataSource forcefully closed/removed all connections.
2020-02-27 16:34:34,205 628    [           main] DEBUG source.pooled.PooledDataSource  - PooledDataSource forcefully closed/removed all connections.
2020-02-27 16:34:34,206 629    [           main] DEBUG source.pooled.PooledDataSource  - PooledDataSource forcefully closed/removed all connections.
2020-02-27 16:34:34,222 645    [           main] DEBUG       org.apache.ibatis.io.VFS  - Class not found: org.jboss.vfs.VFS
2020-02-27 16:34:34,222 645    [           main] DEBUG org.apache.ibatis.io.JBoss6VFS  - JBoss 6 VFS API is not available in this environment.
2020-02-27 16:34:34,224 647    [           main] DEBUG       org.apache.ibatis.io.VFS  - Class not found: org.jboss.vfs.VirtualFile
2020-02-27 16:34:34,225 648    [           main] DEBUG       org.apache.ibatis.io.VFS  - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
2020-02-27 16:34:34,227 650    [           main] DEBUG       org.apache.ibatis.io.VFS  - Using VFS adapter org.apache.ibatis.io.DefaultVFS
2020-02-27 16:34:34,229 652    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Find JAR URL: file:/F:/my_project/Mybatis06/target/classes/com/xhbjava/dao
2020-02-27 16:34:34,230 653    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Not a JAR: file:/F:/my_project/Mybatis06/target/classes/com/xhbjava/dao
2020-02-27 16:34:34,391 814    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IEmployeeDao.class
2020-02-27 16:34:34,393 816    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IEmployeeDao.xml
2020-02-27 16:34:34,394 817    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IEmployeeTaskDao.class
2020-02-27 16:34:34,395 818    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IEmployeeTaskDao.xml
2020-02-27 16:34:34,396 819    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IEmployeeTaskDao1.xml
2020-02-27 16:34:34,397 820    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IFemaleHealthFormDao.class
2020-02-27 16:34:34,398 821    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IFemaleHealthFormDao.xml
2020-02-27 16:34:34,399 822    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IMaleHealthFormDao.class
2020-02-27 16:34:34,400 823    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IMaleHealthFormDao.xml
2020-02-27 16:34:34,401 824    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: ITaskDao.class
2020-02-27 16:34:34,402 825    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: ITaskDao.xml
2020-02-27 16:34:34,403 826    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IWorkCardDao.class
2020-02-27 16:34:34,404 827    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: IWorkCardDao.xml
2020-02-27 16:34:34,405 828    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Listing file:/F:/my_project/Mybatis06/target/classes/com/xhbjava/dao
2020-02-27 16:34:34,407 830    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Find JAR URL: file:/F:/my_project/Mybatis06/target/classes/com/xhbjava/dao/IEmployeeDao.class
2020-02-27 16:34:34,408 831    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Not a JAR: file:/F:/my_project/Mybatis06/target/classes/com/xhbjava/dao/IEmployeeDao.class
2020-02-27 16:34:34,411 834    [           main] DEBUG rg.apache.ibatis.io.DefaultVFS  - Reader entry: ����

从日志中我们可以看到所有级联都成功了,但是也引发了性能问题,这就是N+1问题。

posted on 2020-02-27 16:35  王小码  阅读(227)  评论(0编辑  收藏  举报