Mybatis级联有三种:
- 鉴别器(discriminator):比如根据政治面貌选择使用那张信息表
- 一对一(association):比如学生和学号
- 一对多(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进行映射