复杂查询

一、多对一查询

环境搭建:

student:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.saxon.pojo;

public class Student {
private int id;
private String name;
private Teacher teacher;

@Override
public String toString () {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
'}';
}
}

teacher:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.saxon.pojo;

import java.util.List;

public class Teacher {
private int id;
private String name;
private int tid;


@Override
public String toString () {
return "Teacher{" +
"id=" + id +
", name='" + name + '\''+
'}';
}
}

1.第一种根据查询嵌套查询

1
2
3
4
5
6
7
8
9
10
11
<resultMap id="student" type="student">
<association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>
</resultMap>

<select id="getStudentInfo" resultMap="student">
select * from student;
</select>

<select id="getTeacher" resultType="teacher">
select * from mybatis.teacher where id=#{id}
</select>

2.第二种:根据查询的结果查询

1
2
3
4
5
6
7
8
9
10
11
12
<select id="getStudentInfo2" resultMap="student">
select s.id as id,s.name as student,t.name as teacherName
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="student" type="student">
<result property="id" column="id"/>
<result property="name" column="student"/>
<association property="teacher" column="tid" javaType="teacher">
<result column="teacherName" property="name"/>
</association>
</resultMap>

我们查出来的数据有一个是一个对象,那么我们就用这个对象再来查询一次;根据我们的sql语句,我们的teacherName是查询的别名,对应的就是实体类中的老师的名字;这个效果和子查询的结果是一样的;

二、一对多查询

环境搭建:

student:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.saxon.pojo;

public class Student {
private int id;
private String name;
private int tid;

@Override
public String toString () {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tid=" + tid +
'}';
}
}

teacher:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.saxon.pojo;

import java.util.List;

public class Teacher {
private int id;
private String name;
private List<Student> students;


@Override
public String toString () {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", list=" + students +
'}';
}
}

1.第一种根据查询结果查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.saxon.Dao.teacherMapper">
<select id="getTeacher" resultMap="teach">
select t.id ,t.name,s sid,s.name sname from teacher t,student s
where t.id=s.tid and t.id=#{id}
</select>
<resultMap id="teach" type="teacher">
<result column="id" property="id"/>
<result property="name" column="name"/>
<collection property="students" ofType="student">
<result column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="tid" property="tid"/>
</collection>
</resultMap>
</mapper>

步骤:

1.先根据写出的函数来进行一个语句的书写;

2.对结果集合进行一个映射。

3.字段里面有的映射出来 其中column表示的是数据库中的字段,property表示的是实体类的字段;一一对应关系;

4.我们的老师类子段中由一个集合,那么结果映射就是一个集合,结果集合就用collection来表示;oftype表示泛型;

5.在对我们的泛型里面的字段进行一个映射,有的字段进行一个一一对应;

特别注意的是我不知道为何学生字段必须全部有,删除一个无法编译;

2.第二种 不会,因为直接看不懂

总结:集合使用collection,对象使用assoation,前者泛型使用oftype后者对象使用JavaType