学习目标:
MyBatis中多对一的处理
学习内容:
多对一处理
学习时间:
学习产出:
多对一处理
1、环境搭建
POJO
- Student
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName Student
* @Author $童一
* @Description $
* @Param $
* @return $
* @Date $ $
**/
//使用lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
- Teacher
package com.mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName Teacher
* @Author $童一
* @Description $
* @Param $
* @return $
* @Date $ $
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
-
Mapper.xml
在resource下面建立与mapper接口相同的包,放置StudentMapper.xml和TeacherMapper.xml然后在mybatis-config.xml中注册
<mappers> <mapper resource="com/mybatis/mapper/StudentMapper.xml"/> <mapper resource="com/mybatis/mapper/TeacherMapper.xml"/> </mappers>
2、第一种:按照查询嵌套处理:(子查询)
StudentMapper
public interface StudentMapper {
//多对一处理(通过子查询)
List<Student> getStudentList();
//多对一处理(通过关联查询)
List<Student> getStudentList2();
}
TeacherMapper
public interface TeacherMapper {
//测试环境
@Select("select * from teacher where id=#{id}")
Teacher getTeacherById(@Param("id") int id);
Teacher getTeacher();
}
StudentMapper.xml
<!--第一种:按照查询嵌套处理:(子查询)
逻辑:
1、查询出来所有的学生
2、根据查询出来的学生tid,通过select寻找对应的老师
-->
<select id="getStudentList" resultMap="StudentMap">
select * from student;
</select>
<resultMap id="StudentMap" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性,我们需要单独处理 对象:association 集合:collection -->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{tid};
</select>
Test
@Test
public void getStudentListTest(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudentList();
for (Student student : studentList) {
System.out.println(student);
}
}
注意:在mybatis-config.xml中设置别名
<typeAliases>
<typeAlias type="com.mybatis.pojo.Student" alias="Student"/>
<typeAlias type="com.mybatis.pojo.Teacher" alias="Teacher"/>
</typeAliases>
3、第二种:按照结果嵌套处理:(关联查询)
<!--第二种:按照结果嵌套处理:(关联查询)-->
<select id="getStudentList2" resultMap="StudentMap2">
select s.id as sid,s.name as sname,t.name as tname
from student as s,teacher as t
where s.tid=t.id;
</select>
<resultMap id="StudentMap2" 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
@Test
public void getStudentListTest2(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudentList2();
for (Student student : studentList) {
System.out.println(student);
}
}