多对一
多个学生对于一个老师
环境搭建
- 导入lombok
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
- 新建实体类Teacher,Student
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
@Data
public class Teacher {
private int id;
private String name;
}
- 建立Mapper接口
public interface StudentMapper {
public List<Student> getStudent();
public List<Student> getStudent2();
}
public interface TeacherMapper {
@Select("select * from teacher where id =#{id}")
Teacher getTeacher(@Param("id") int id);
}
- 建立Mapper.xml文件(StudentMapper.xml)
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.weirdo.dao.StudentMapper">
</mapper>
- 在核心配置文件中绑定注册我们的Mapper接口或者文件 【方式很多,随心选】
<mappers>
<mapper class="com.weirdo.dao.TeacherMapper"/>
<mapper class="com.weirdo.dao.StudentMapper"/>
</mappers>
- 测试查询是否能够成功
按照查询嵌套处理
<select id="getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{id};
</select>
@Test
public void testStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentlist = mapper.getStudent();
for (Student student : studentlist) {
System.out.println(student);
}
sqlSession.close();
}
按照结果嵌套处理
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid =t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
@Test
public void testStudent2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentlist = mapper.getStudent2();
for (Student student : studentlist) {
System.out.println(student);
}
sqlSession.close();
}
回顾Mysql多对一查询方式:
- 子查询 (按照查询嵌套)
- 联表查询 (按照结果嵌套)
一对多
按照查询嵌套处理
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from mybatis.teacher where id = #{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from mybatis.student where tid=#{tid}
</select>
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
结果输出
Teacher(id=1, name=张老师,students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小兰, tid=1), Student(id=4, name=小黑, tid=1), Student(id=5, name=小白, tid=1)])
按照结果嵌套处理
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
小结
- 关联 - association 【多对一】
- 集合 - collection 【一对多】
- javaType & ofType:
JavaType用来指定实体类中的类型 ofType用来指定映射到List或者集合中的pojo类型,泛型中的约束类型
|