一、复杂查询环境搭建
# 创建老师表并插入数据
create table `teacher` (
`id` int(10) not null,
`name` varchar(30) default null,
primary key (`id`)
) engine=innodb default charset=utf8;
insert into teacher(`id`, `name`) values (1, '秦老师');
# 创建学生表并插入数据
create table `student` (
`id` int(10) not null,
`name` varchar(30) default null,
`tid` int(10) default null,
primary key (`id`),
key `fktid` (`tid`),
constraint `fktid` foreign key (`tid`) references `teacher` (`id`)
) engine=innodb default charset=utf8;
insert into `student` (`id`, `name`, `tid`) values
('1', '小明', '1'),
('2', '小红', '1'),
('3', '小张', '1'),
('4', '小李', '1'),
('5', '小王', '1');
二、配置环境
//实体类,学生
public class Student {
private int id;
private String name;
private int tid;
public Student() {}
public Student(int id, String name, int tid) {
this.id = id;
this.name = name;
this.tid = tid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tid=" + tid +
'}';
}
}
//实体类,老师
public class Teacher {
private int id;
private String name;
private List<Student> students;
public Teacher() {}
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}
public interface StudentMapper {}
public interface TeacherMapper {
//测试环境
List<Teacher> getTeachers();
}
<?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.comprehensive.dao.TeacherMapper">
<!-- 测试环境 -->
<select id="getTeachers" resultType="com.comprehensive.pojo.Teacher">
select * from mybatis.teacher
</select>
</mapper>
public class Test_OneToMany {
@Test
//测试环境
public void test() {
SqlSession sqlSession = MyBatis_Util.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
for(Teacher teacher : mapper.getTeachers()) System.out.println(teacher);
}
}
发现问题:从结果上看,Teacher类中的private List<Student> students;
属性未被赋值
解决问题:一对多实现
三、一对多
按照查询嵌套处理
需求分析(SQL实现)
select t.id as t_id, t.name as t_name from mybatis.teacher t
public interface TeacherMapper { //一对多: //方式一:按照查询嵌套处理 Teacher getTeacherWithStudents(); }
```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">
```java
public class Test_OneToMany {
@Test
//方式一:按照查询嵌套结果
public void test1() {
SqlSession sqlSession = MyBatis_Util.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacherWithStudents();
System.out.println(teacher);
for(Student student : teacher.getStudents()) {
System.out.println(student);
}
sqlSession.close();
}
}
按照结果嵌套处理
select t.id as t_id, t.name as t_name, s.id as s_id, s.name as s_name, s.tid as s_tid
from mybatis.teacher t, mybatis.student s
where t.id=s.tid
public interface TeacherMapper {
//一对多:
//方式二:按照结果嵌套处理
Teacher getTeacherWithStudents2();
}
<?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.comprehensive.dao.TeacherMapper">
<!-- 方式二:按照结果嵌套 -->
<select id="getTeacherWithStudents2" resultMap="teacherWithStudents2">
select t.id as t_id, t.name as t_name, s.id as s_id, s.name as s_name, s.tid as s_tid from mybatis.teacher t, mybatis.student s where t.id=s.tid
</select>
<resultMap id="teacherWithStudents2" type="com.comprehensive.pojo.Teacher">
<result property="id" column="t_id"/>
<result property="name" column="t_name"/>
<!-- 集合中的泛型信息,用ofType获取 -->
<collection property="students" ofType="com.comprehensive.pojo.Student">
<result property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="tid" column="s_tid"/>
</collection>
</resultMap>
</mapper>
public class Test_OneToMany {
@Test
//方式二:按照结果嵌套
public void test2() {
SqlSession sqlSession = MyBatis_Util.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacherWithStudents2();
System.out.println(teacher);
for(Student student : teacher.getStudents()) System.out.println(student);
sqlSession.close();
}
}