Mybatis第五讲——关联一对多
目录结构:
数据表:
t_grade:
t_student:
t_address:
主外键关联关系:
Grade 实体类:
package com.java1234.model;
import java.util.List;
public class Grade {
private Integer id;
private String gradeName;
private List<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Grade [id=" + id + ", gradeName=" + gradeName + ", students=" + students + "]";
}
}
GradeMapper.java:
package com.java1234.mappers;
import com.java1234.model.Grade;
public interface GradeMapper {
public Grade findGradeById(Integer id);
}
GradeMapper.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="com.java1234.mappers.GradeMapper">
<resultMap type="Grade" id="gradeResult">
<result property="id" column="id"/>
<result property="gradeName" column="gradeName"/>
<collection property="students" column="id" select="com.java1234.mappers.StudentMapper.getStudentByGradeId"></collection>
</resultMap>
<select id="findGradeById" resultMap="gradeResult">
select * from t_grade where id=#{id}
</select>
</mapper>
StudentMapper.java:
与上一讲相比,添加了getStudentByGradeId()函数接口。
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="com.java1234.mappers.StudentMapper">
<insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert>
<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>
<update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getStudentById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
<select id="getStudentByGradeId" parameterType="Integer" resultType="Student">
select * from t_student where gradeId=#{gradeId}
</select>
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findAddressById"/>
</resultMap>
<select id="getAllStudents" resultMap="StudentResult">
select * from t_student
</select>
<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>
</mapper>
主要是添加了getStudentByGradeId函数的SQL实现:
测试类:
package com.java1234.service;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.java1234.mappers.GradeMapper;
import com.java1234.model.Grade;
import com.java1234.util.SqlSessionFactoryUtil;
public class GradeTest {
private SqlSession sqlSession=null;
private GradeMapper gradeMapper=null;
@BeforeEach
void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
gradeMapper=sqlSession.getMapper(GradeMapper.class);
}
@AfterEach
void tearDown() throws Exception {
sqlSession.close();
}
/**
* 通过gradeId获取学生信息
*/
@Test
void getStudentByGradeId() {
Grade grade=gradeMapper.findGradeById(1);
System.out.println(grade);
}
}
输出:
Grade [id=1, gradeName=大学一年级, students=[Student [id=1, name=张三, age=22, address=null], Student [id=4, name=王五, age=33, address=null]]]
这里address为空,有点问题,不过和我们这讲的一对多无关,暂且放下,后续再解决。