1 創建關係
CREATE TABLE tb_clazz
(
`id` INT AUTO_INCREMENT,
`code` VARCHAR(64),
`name` VARCHAR(12),
PRIMARY KEY (`id`)
);
CREATE TABLE tb_student
(
`id` INT AUTO_INCREMENT,
`clazz_id` INT,
`name` VARCHAR(20),
`sex` CHAR(2),
`age` TINYINT,
PRIMARY KEY (`id`),
FOREIGN KEY (`clazz_id`) REFERENCES tb_clazz (`id`)
);
INSERT INTO tb_clazz(`id`, `code`, `name`)
VALUES (1, '02131704', '軟工四班');
INSERT INTO tb_student(`name`, `clazz_id`, `sex`, `age`)
VALUES ('jack', 1, '男', 23),
('rose', 1, '女', 18),
('tom', 1, '男', 21),
('alice', 1, '女', 20);
@Data
public class Clazz {
/**
* 班級id,主鍵
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 班級編號
*/
private String code;
/**
* 班級名稱
*/
private String name;
/**
* 班級和學生是一對多的關係,即一個班級可以有多個學生
*/
private List<Student> students;
}
@Data
public class Student {
/**
* 學生id,主鍵
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 性別
*/
private String sex;
/**
* 年齡
*/
private Integer age;
/**
* 學生和班級是多對一的關係,即一個學生只屬於一個班級
*/
private Clazz clazz;
}
2 collection定義一對多關係
查詢指定班級的所有學生
<!--
ClazzMapper.xml
-->
<resultMap type="cn.yuanyu.mybatis.entity.Clazz" id="clazzResultMap">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
<!--
一對多關聯映射
fetchType="lazy"表示懶加載
ClazzMapper.xml
-->
<collection property="students" javaType="ArrayList"
column="id" ofType="cn.yuanyu.mybatis.entity.Student"
select="cn.yuanyu.mybatis.mapper.StudentMapper.selectStudentByClazzId"
fetchType="lazy">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
</collection>
</resultMap>
<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
SELECT *
FROM tb_clazz
WHERE id = #{id}
</select>
<!--
根據班級id查詢學生信息
StudentMapper.xml
-->
<select id="selectStudentByClazzId" resultType="cn.yuanyu.mybatis.entity.Student">
SELECT *
FROM tb_student
WHERE clazz_id = #{id}
</select>
<!--
ClazzMapper.xml
-->
<resultMap type="cn.yuanyu.mybatis.entity.Clazz" id="clazzResultMap">
<id property="id" column="cId"/>
<result property="code" column="cCode"/>
<result property="name" column="cName"/>
<!-- -->
<collection property="students" ofType="cn.yuanyu.mybatis.entity.Student">
<id property="id" column="sId"/>
<result property="name" column="sName"/>
<result property="sex" column="sSex"/>
<result property="age" column="sAge"/>
</collection>
</resultMap>
<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
SELECT c.id cId,
c.code cCode,
c.name cName,
s.id sId,
s.name sName,
s.sex sSex,
s.age sAge
FROM tb_clazz c
JOIN tb_student s ON c.id = s.clazz_id and c.id = #{id}
</select>
public interface ClazzMapper {
/**
* 根據id查詢班級信息,以及所有學生
*
* @param id 班級id
* @return 班級
*/
Clazz selectClazzById(Integer id);
}
@Test
public void selectClazzById() {
Clazz clazz = clazzMapper.selectClazzById(1);
System.out.println("\n\n---------------------分割線---------------------\n\n");
//List<Student> students = clazz.getStudents(); //這裏纔開始執行查詢
3 association定義多對一關係
查詢用戶信息,包括所在的班級信息
<!--
StudentMapper.xml
-->
<resultMap type="cn.yuanyu.mybatis.entity.Student" id="studentResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<!--
多對一關聯映射
-->
<association property="clazz"
select="cn.yuanyu.mybatis.mapper.ClazzMapper.getClazzById"
column="clazz_id">
</association>
</resultMap>
<!-- 根據id查詢學生信息 -->
<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
SELECT *
FROM tb_student
WHERE id = #{id}
</select>
<!--
ClazzMapper.xml
-->
<select id="getClazzById" resultType="cn.yuanyu.mybatis.entity.Clazz">
SELECT *
FROM tb_clazz
WHERE id = #{id}
</select>
<!--
StudentMapper.xml
-->
<resultMap type="cn.yuanyu.mybatis.entity.Student" id="studentResultMap">
<id property="id" column="sId"/>
<result property="name" column="sName"/>
<result property="sex" column="sSex"/>
<result property="age" column="sAge"/>
<!--
多對一關聯映射
-->
<association property="clazz" javaType="cn.yuanyu.mybatis.entity.Clazz">
<id property="id" column="cId"/>
<result property="code" column="cCode"/>
<result property="name" column="cName"/>
</association>
</resultMap>
<!-- 根據id查詢學生信息,多表連接 -->
<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
SELECT s.id sId,
s.name sName,
s.sex sSex,
s.age sAge,
c.id cId,
c.code cCode,
c.name cName
FROM tb_student s,
tb_clazz c
WHERE s.clazz_id = c.id
AND s.id = #{id}
</select>
public interface StudentMapper {
/**
* 根據id查詢學生信息,班級信息也要查詢出來
*
* @param id 學生id
* @return 學生
*/
Student selectStudentById(Integer id);
}
@Test
public void selectStudentById() {
Student student = studentMapper.selectStudentById(1);