一、鉴别器和一对多级联

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问题。

Mybaits(9)MyBatis级联-2的更多相关文章

  1. (三)mybatis级联的实现

    mybatis级联的实现 开篇         级联有三种对应关系: 1.一对一(association):如学号与学生  2.一对多(collection):如角色与用户  3.多对多(discri ...

  2. Mybatis 级联查询 (一对多 )

    后台系统中 涉及到添加试卷 问题 答案的一个模块的.我需要通过试卷 查询出所有的试题,以及试题的答案.这个主要要使用到Mybatis的级联查询. 通过试卷 查询出与该试卷相关的试题(一对多),查询出试 ...

  3. Mybatis级联:关联、集合和鉴别器的使用

    Mybatis中级联有关联(association).集合(collection).鉴别器(discriminator)三种.其中,association对应一对一关系.collection对应一对多 ...

  4. mybatis级联

    mybatis中有时候表不能都分成单表进行查询,表之间会有联系,这时候需要将表进行级联 下面讲一下如何将mybatis中 的表进行级联.映射表关系如下 1:创建数据表 DROP TABLE IF EX ...

  5. Mybatis级联,使用JOIN和Associa,以及一些ID覆盖和自动变换。

    先说下坑,比如数据库的字段是 DW_ID  ,用generator讲mybatis自动转换的时候,会省略下表_变成dwId,所以我们之后自己手动设计的时候也尽量换成dwId: generate的myb ...

  6. mybatis 级联

    级联是一个数据库实体的概念.一对多的级联,一对多的级联,在MyBatis中还有一种被称为鉴别器的级联,它是一种可以选择具体实现类的级联. 级联不是必须的,级联的好处是获取关联数据十分便捷,但是级联过多 ...

  7. mybatis级联查询,多对一查询问题

    在使用Mybatis进行多表级联查询时遇到了一个问题:查询结果只有一项,但正确结果是两项.经测试,SQL语句本身没有问题. 在SQL映射文件(XML)中: <!-- 级联查询数据 --> ...

  8. mybaits(十)mybatis常见面试

      面试题总结 1.MyBatis 解决了什么问题? 或:为什么要用 MyBatis? 或:MyBatis 的核心特性? 1)资源管理(底层对象封装和支持数据源) 2)结果集自动映射 3)SQL 与代 ...

  9. mybatis ---- 级联查询 一对多 (集合映射)

    关联有嵌套查询和嵌套结果两种方式,本文是按照嵌套结果这种方式来说明的 上一章介绍了多对一的关系,用到了<association></association>,这是一个复杂类型的 ...

随机推荐

  1. Linux 常用工具iptables

    iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...

  2. ubuntu18.04编译jdk8

    准备编译环境 sudo apt-get install -y zip unzip build-essential libx11-dev libxext-dev libxrender-dev libxt ...

  3. LeetCode 380. Insert Delete GetRandom O(1) 常数时间插入、删除和获取随机元素(C++/Java)

    题目: Design a data structure that supports all following operations in averageO(1) time. insert(val): ...

  4. A Simple Introduction To Computer Networking

    Most networking discussions are a jumble of acronyms. Forget the configuration details - what are th ...

  5. c++引用深入探讨

    (偶然翻起自己的旧博,忽然发现大三的时候写的这篇文章,仔细看看觉得写的还是那么回事,所以赶紧搭救出来) 引用的声明:   基本格式:引用类型 &引用名=被引用对象 &运算符:声明运算符 ...

  6. 学习Qt的资源-网站、论坛、博客等

    来自<零基础学Qt 4编程>一书的附录 附录C Qt资源 C.1 Qt 官方资源 全球各大公司以及独立开发人员每天都在加入 Qt 的开发社区.他们已经认识到了Qt 的架构本身便可加快应用程 ...

  7. Codeforces 1065C Make It Equal (差分+贪心)

    题意:n个塔,第i个塔由$h_i$个cube组成,每次可以切去某高度h以上的最多k个cube,问你最少切多少次,可以让所有塔高度相等 k>=n, n<=2e5 思路:差分统计每个高度i有的 ...

  8. learn about sqlserver partitition and partition table 1

    Dear all, Let get into business, the partitions on sql server is very different with that on oracle. ...

  9. num08---原型模式

    关键点,实现 Cloneable  接口, 重写clone() 方法.克隆出的对象属性保持一致. 案例: 原型模式在spring 源码中的应用: =========================== ...

  10. 9.3.2 map端连接-CompositeInputFormat连接类

    1.1.1         map端连接-CompositeInputFormat连接类 (1)使用CompositeInputFormat连接类需要满足三个条件: 1)两个数据集都是大的数据集,不能 ...