Mybatis级联

 Mybatis级联有三种:

  1. 鉴别器(discriminator):比如根据政治面貌选择使用那张信息表
  2. 一对一(association):比如学生和学号
  3. 一对多(collrction):比如班级和学生

例子:

首先建立数据库

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `c_id` int(11) NOT NULL,
  `s_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of class
-- ----------------------------

-- ----------------------------
-- Table structure for `member`
-- ----------------------------
DROP TABLE IF EXISTS `member`;
CREATE TABLE `member` (
  `id` varchar(255) NOT NULL,
  `s_id` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of member
-- ----------------------------

-- ----------------------------
-- Table structure for `party_member`
-- ----------------------------
DROP TABLE IF EXISTS `party_member`;
CREATE TABLE `party_member` (
  `id` varchar(255) NOT NULL,
  `s_id` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of party_member
-- ----------------------------

-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `s_id` int(255) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `political_status` varchar(255) DEFAULT NULL,
  `c_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------

然后根据设计模型建立对应的pojo:

package com.model.bo;

public class Class {
    //班级号
    private int c_id;
    //学号
    private int s_id;
    //学生,班级一对多(如果不使用级联,就不用这行代码,直接用c_id去查)
    private List<Student> s = null;
   /**getter and setter**/
}
package com.model.bo;

public class Student {
    private int c_id;
    private int s_id;
    //学生姓名
    private String name;
    //政治面貌
    private String political_status;
    //学生 政治面貌使用鉴别器区分(不用级联同上,判断后用political_status去查)
    private M m = null;
   /**getter and setter**/
}
package com.model.bo;
//团员
public class Member extends M{
    private String c_id;
    private int s_id;
    private String name;
    /**getter and setter**/
}
package com.model.bo;
//党员
public class Party_member extends M{
    private String c_id;
    private int s_id;
    private String name;
    /**getter and setter**/
}
package com.model.bo;

public class M {
    private String c_id;
    private int s_id;
    private String name;
    /**getter and setter**/
}

再配置映射文件(这里只给出select):

查找学生信息(鉴别器和1对1)

<mapper namespace="com.mapper.StudentMpper">
    <resultMap type="com.model.bo.Student" id="StudentMap">
        <id column="s_id" property="s_id">
        <resultMap column="name" property="name">
        <resultMap column="political_status" property="political_status">
        <resultMap column="c_id " property="c_id ">
        //鉴别器
        <discriminator javaType="String" column="political_status">
            <case value="团员" resultMap="MMap">
            <case value="党员" resultMap="PMMap">
        </discriminator>
    </reuluMap>
    
    //党员resultMap 
    <resultMap type="com.model.bo.M" id="PMMap">
        //学生id,党员信息1对1查询
        <association property="Party_member" column="s_id"  select="select s_id,name,id from member where s_id=${s_id}"/>
    </reuluMap>

    //团员resultMap 
     <resultMap type="com.model.bo.M" id="MMap">
        //学生id,团员信息1对1查询
        <association property="Party_member" column="s_id"  select="select s_id,name,id from party_member where s_id=${s_id}"/>
    </reuluMap>

    //根据学生id查找学生信息
    <select id="getStudentByS_id" resultMap="StudentMap">
        select s_id,name,political_status,c_id from student where s_id=${s_id}
    </select>
</mapper>

查找班级里所有学生(1对多)

<mapper namespace="com.mapper.ClassMpper">
    <resultMap type="com.model.bo.Class" id="ClassMap">
        <id column="c_id" property="c_id">
        <resultMap column="s_id" property="s_id">
        //一对多
        <collection property="s"  column="c_id" select="com.mapper.StudentMpper.getStudentByS_id"/>
    </reuluMap>

    //根据班级id查找所有学生信息
    <select id="getStudentByC_id" resultMap="ClassMap">
        select s_id,name,political_status,c_id from student where c_id=${c_id}
    </select>
</mapper>

 association:1对1

  • select: 表明当前属性是调用select 指定的方法
  • column: 指定将哪一列的值传给这个方法
  • 流程 : 使用select 指定的方法(传入column指定的这列参数的值)查出对象并封装给property 指定的属性

collection :1对多

  • select:表明当前属性是调用select 指定的方法
  • column: 指定将哪一列的值传给这个方法
  • 流程 : 使用select 指定的方法(传入column指定的这列参数的值)查出对象并封装给property 指定的属性

discriminator:鉴别器

  • column:代表使用哪个字段进行鉴别
  • case:用于区分,类似java的switch...case...语句
  • resultMap:制定用哪一个resultMap进行映射
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值