鉴别器(discriminator)是MyBatis为我们提供的第三个级联也是最后一个。基于之前两篇级联中的场景,现增加学生们去体检,但男女体检项目不一样,我们把男女体检表做成两张表,当然我想也可以设计为一张表,只有女生的项目男生不填就行了,为了讲解鉴别器就把男女体检表分开。鉴别器的作用在这里就是根据性别的不同去不同的表里进行查询体检情况,例如是男生就在男生体检表里查询,是女生就在女生体检表里查询。
POJO类我们也分为了男生、女生,他们分别继承之前的Student类。
MaleStudent类:
1 package day_8_mybatis.pojo;
2
3 import java.util.List;
4
5 /**
6 * @author turbo
7 *
8 * 2016年11月6日
9 */
10 public class MaleStudent extends Student {
11 List<MaleStudentHealth> studentHealthList;
12 //省略getter/setter方法
13 }
一个学生和他体检表的对应关系应该是一对一的关系,为什么在这里是一对多的关系呢?呃……这是因为在体检表的设计中有一个日期的字段,也就是说一个学生在不同时间的体检情况都有记录,所以学生和体检表的对应关系就是一对多的关系,在这里也就是一个List的引用。
FemaleStudent类:
1 package day_8_mybatis.pojo;
2
3 import java.util.List;
4
5 /**
6 * @author turbo
7 *
8 * 2016年11月6日
9 */
10 public class FemaleStudent extends Student{
11 List<FemaleStudentHealth> studentHealthList;
12 //省略getter/setter方法
13 }
这里的List的引用道理同上。
现在看看体检表的POJO类。
MaleStudentHealth类:
package day_8_mybatis.pojo;
/**
* @author turbo
*
* 2016年11月6日
*/
public class MaleStudentHealth {
private int id;
private int studentId;
private String date;
private String prostate; //前列腺
//省略setter/getter方法
}
FemaleStudentHealth类:
1 package day_8_mybatis.pojo;
2
3 /**
4 * @author turbo
5 *
6 * 2016年11月6日
7 */
8 public class FemaleStudentHealth {
9 private int id;
10 private int studentId;
11 private String date;
12 private String womb;
13 //省略setter/getter方法
14 }
基本的数据结构设计就是上面所贴出来的代码了。下面我们看看mapper映射器,对于体检情况的查询不管男生女生都是通过student_id来查询的。
查询根据男生的student_id查询该生的体检表:
1 package day_8_mybatis.mapper;
2
3 import day_8_mybatis.pojo.MaleStudentHealth;
4
5 /**
6 * @author turbo
7 *
8 * 2016年11月6日
9 */
10 public interface MaleStudentHealthMapper {
11 MaleStudentHealth findMaleStudentHealthByStudentId(int id);
12 }
其对应的MaleStudentHealthMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
6 <mapper namespace="day_8_mybatis.mapper.MaleStudentHealthMapper">
7 <select id="findMaleStudentHealthByStudentId" parameterType="int" resultType="day_8_mybatis.pojo.MaleStudentHealth">
8 select id, student_id as studentId, date, prostate from t_male_student_health where student_id = #{id}
9 </select>
10 </mapper>
查询根据女生的student_id查询该生的体检表:
1 package day_8_mybatis.mapper;
2
3 import day_8_mybatis.pojo.FemaleStudentHealth;
4
5 /**
6 * @author turbo
7 *
8 * 2016年11月6日
9 */
10 public interface FemaleStudentHealthMapper {
11 FemaleStudentHealth findFemaleStudentHealthByStudentById(int id);
12 }
其对应的FemaleStudentHealthMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="day_8_mybatis.mapper.FemaleStudentHealthMapper">
6 <select id="findFemaleStudentHealthByStudentById" parameterType="int" resultType="day_8_mybatis.pojo.FemaleStudentHealth">
7 select id, student_id as studentId, date, womb from t_female_student_health where student_id = #{id}
8 </select>
9 </mapper>
基本工作已经做完了,剩下就是在StudentMapper.xml利用鉴别器来查询不同的体检表。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="day_8_mybatis.mapper.StudentMapper">
6 <resultMap type="day_8_mybatis.pojo.Student" id="studentMap">
7 <id property="id" column="id"/>
8 <result property="name" column="name"/>
9 <result property="sex" column="sex"/>
10 <association property="selfCard" column="id" select="day_8_mybatis.mapper.SelfCardMapper.findSelfCardByStudentId"/>
11 <collection property="courseScoreList" column="id" select="day_8_mybatis.mapper.CourseScoreMapper.findCourseScoreByStudentId" />
12 <discriminator javaType="string" column="sex">
13 <case value="男" resultMap="maleStudentMap"/>
14 <case value="女" resultMap="femaleStudentMap"/>
15 </discriminator>
16 </resultMap>
17
18 <select id="getStudent" parameterType="int" resultMap="studentMap">
19 select id, name, sex from t_student where id = #{id}
20 </select>
21
22 <resultMap id="maleStudentMap" type="day_8_mybatis.pojo.MaleStudent" extends="studentMap">
23 <collection property="studentHealthList" select="day_8_mybatis.mapper.MaleStudentHealthMapper.findMaleStudentHealthByStudentId" column="id" />
24 </resultMap>
25
26 <resultMap id="femaleStudentMap" type="day_8_mybatis.pojo.FemaleStudent" extends="studentMap">
27 <collection property="studentHealthList" select="day_8_mybatis.mapper.FemaleStudentHealthMapper.findFemaleStudentHealthByStudentById" column="id" />
28 </resultMap>
29 </mapper>
第12-15行就是本节的要讲的discriminator鉴别器,它通过查询出来的学生性别选择不同的体检表来查询出体检情况。
最后稍微修改的测试类,即可测试结果。
1 package day_8_mybatis;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import org.apache.ibatis.io.Resources;
7 import org.apache.ibatis.session.SqlSession;
8
9 import day_8_mybatis.mapper.StudentMapper;
10 import day_8_mybatis.pojo.MaleStudent;
11 import day_8_mybatis.util.SessionFactory2;
12
13 /**
14 * 客户端
15 * @author turbo
16 *
17 * 2016年11月6日
18 */
19 public class Main {
20
21 /**
22 * @param args
23 * @throws IOException
24 */
25 public static void main(String[] args) throws Exception {
26 String resource = "day_8_mybatis/mybatis-config.xml"; //获取mybatis配置文件路径
27 InputStream inputStream = Resources.getResourceAsStream(resource);
28 SqlSession sqlSession = SessionFactory2.getInstance(inputStream).openSession();
29 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
30 MaleStudent student =(MaleStudent)studentMapper.getStudent(1);
31 System.out.println("学生:" + student.getName() + " 课程:" + student.getCourseScoreList().get(0).getCourse().getCourseName() + " 分数:" + student.getCourseScoreList().get(0).getScore()+ " 性别:" + student.getSex() + " 前列腺:" + student.getStudentHealthList().get(0).getProstate());
32
33 }
34
35 }
别忘了把新增加的mapper映射注册到mybatis-config.xml配置文件中。