欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis级联问题(association、collection和discriminator)

程序员文章站 2022-04-22 07:59:18
...

mybatis的级联一般分为三种。一对一,一对多,多对多。但是实际生活中多对多比较少,因为它很复杂,书本上说是建议把多对多分解成双向一对多。接下来开始简介:

association:一对一,比如你这个人和身份证就是一一对应的(我之前的requestMap的博客中简单讲了下这个元素的作用http://blog.csdn.net/zyf2333/article/details/77603028

collection:一对多,比如一个班级对应着多个学生。

discriminator:鉴别器,根据特定的条件去关联不同的结果集。比如有个Person表。你想实例化一个人,没问题,但是你要根据实际情况来判断是实例化一个男性,还是实例化一个女性。(就类似于c语言的“switch”)

这里主要讲一下collection的一对多如何配置xml。利用书本上的例子来吧

首先每个学生要学习很多门课程,而每门课程又有相对应的课程成绩,因此此处就有了两个级联(学生->课程一对多;课程->课程成绩一对一)。一对一用的association,一对多用的collection。创建课程和成绩的POJO。

学生->课程1成绩->课程1
   ->课程2成绩->课程2
   ->课程3成绩->课程3
  (LectureBean代表课程)
  (StrudentLectureBean代表课程成绩)
  伪代码如下(主要是关注xml怎么配置):
public class LectureBean{
    private Integer id;
    private String lectureName;
    private String note;
    ...setter and getter...
}
public class StrudentLectureBean{
    private int id;
    private Integer studentID;
    //用来读取课程信息
    private LectureBean lecture;
    private BigDecimal grade;
    private String note;
    ...setter and getter...
}

这里还有个StudentBean

public class StudentBean{
    ...省略,包含了姓名,性别,id等诸多信息
    //用来读入多个课程成绩
    List<StudentLectureBean> studentLectureList=new List<StudentLectureBean>();
    ...省略
}

各个mapper.xml配置清单如下。看着代码来理解吧:
StudentMapper.xml

<?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="domain.blog.mappers.AuthorMapper">

    <!-- 先设定resultMap -->
    <resultMap type="com.learn.chapter4.po.StudentBean" id="studentMap">
        <id property="id" column="id" />
        <result property="cnname" column="cnname" />
        <result property="sex" column="sex" jdbcType="INTEGER"
            javaType="com.learn.chapter4.enums.SexEnum" />
        <result property="note" column="note" />

        <!-- 接下来是正文! -->
        <!-- 引用了StudentLectureMapper.xml中的方法 -->
        <collection property="studentLectureList" column="id"
            select="com.learn.chapter4.mapper.StudentLectureMapper.findStudentLectureByStuId"></collection>
    </resultMap>

    <select id="getStudent" parameterType="int" resultMap="studentMap">
        select
        id,cnname,sex,note from t_student where id=#{id}
    </select>
</mapper>  

StudentLectureMapper.xml

<?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="domain.blog.mappers.AuthorMapper">

    <!-- 先设定resultMap -->
    <resultMap type="com.learn.chapter4.po.StudentLectureBean"
        id="studentLectureMap">
        <id property="id" column="id" />
        <result property="studentId" column="student_id" />
        <result property="grade" column="grade" />
        <result property="note" column="note" />
        <association property="lecture" column="lecture_id"
            select="com.learn.chapter4.mapper.LectureMapper.getLecture" />
    </resultMap>

    <select id="findStudeentLectureByStuId" parameterType="int"
        resultMap="studentLectureMap">
        select
        id,student_id,lecture_id,grade,note from
        t_student_lecture where id=#{id}
    </select>
</mapper>  

LectureMapper.xml

<?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="domain.blog.mappers.AuthorMapper">

    <select id="getLecture" parameterType="int"
        resultType="com.learn.chapter4.po.LectureBean">
        select
        id,lecture_name as lectureName,note from t_lecture
        where id=#{id}
    </select>
</mapper>  

从这里就可看到各个mapper之间是如何关联的了。

接下来看看鉴别器discriminator
假设有一个男女学生的表,我们要根据不同的情况来**不同的实例。
男女学生类伪代码如下

//男学生
public class MaleStudentBean extends StudentBean{
    private List<StudentHealthMaleBean> studentHealthMaleBean=null;
    ...setter and getter
}

//女学生
public class FemaleStudentBean extends StudentBean{
    private List<StudentHealthFemaleBean> studentHealthFemaleBean=null;
    ...setter and getter
}

改动StudentMapper.xml,具体请看代码以及代码注释

<?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="domain.blog.mappers.AuthorMapper">

    <!-- 先设定resultMap -->
    <resultMap type="com.learn.chapter4.po.StudentBean" id="studentMap">
        <id property="id" column="id" />
        <result property="cnname" column="cnname" />
        <result property="sex" column="sex" jdbcType="INTEGER"
            javaType="com.learn.chapter4.enums.SexEnum" />
        <result property="note" column="note" />
        <collection property="studentLectureList" column="id"
            select="com.learn.chapter4.mapper.StudentLectureMapper.findStudentLectureByStuId"></collection>

        <!-- 开始写鉴别器!此处根据不同的case,引用不同的resultMap,就写在下方 -->
        <discriminator javaType="int" column="sex">
            <case value="1" resultMap="maleStudentMap" />
            <case value="2" resultMap="femaiStudentMap" />
        </discriminator>

    </resultMap>

    <!-- 写上面不同case对应的reusltMap 先男生再女生-->
    <resultMap type="com.learn.chapter4.po.MaleStudentBean" id="maleStudentMap"
        extends="studentMap">
        <collection property="studentHealthMaleList(此处是另外关联的表,不用关心了)"
            select="...略" />
    </resultMap>
    <resultMap type="com.learn.chapter4.po.FemaleStudentBean" id="femaleStudentMap"
        extends="studentMap">
        <collection property="studentHealthMaleList(此处是另外关联的表,不用关心了)"
            select="...略" />
    </resultMap>
    <!-- 结束 -->

    <select id="getStudent" parameterType="int" resultMap="studentMap">
        select
        id,cnname,sex,note from t_student where id=#{id}
    </select>
</mapper>  
相关标签: mybatis