9.1 表结构
现在有两张表
student
id | name | tid |
---|---|---|
1 | Tom | 1 |
2 | Jack | 1 |
3 | Alice | 1 |
4 | Eric | 2 |
5 | Bob | 2 |
teacher
id | name |
---|---|
1 | Teacher Zhang |
2 | Teacher Li |
学生表的 tid 和 教师表的 id 对应。
我们不难看出这是一个多表查询
9.2 创建实体类
Student.java
package com.lu.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.Alias;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Alias("Student")
public class Student {
private int id;
private String name;
private Teacher teacher;
}
Teacher.java
package com.lu.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
@AllArgsConstructor
@Alias("Teacher")
public class Teacher {
private int id;
private String name;
}
9.3 创建mapper
StudentMapper.java
package com.lu.dao;
import com.lu.entity.Student;
import java.util.List;
public interface StudentMapper {
// 查询所有学生的信息,以及对应老师的信息
List<Student> getStudent();
}
9.4 创建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.lu.dao.StudentMapper">
<select id="getStudent" resultType="Student">
select * from student;
</select>
</mapper>
package com.lu;
import com.lu.dao.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
mapper.getStudent().forEach(System.out::println);
sqlSession.close();
}
}
查询结果
Student(id=1, name=Tom, teacher=null)
Student(id=2, name=Jack, teacher=null)
Student(id=3, name=Alice, teacher=null)
Student(id=4, name=Eric, teacher=null)
Student(id=5, name=Bob, teacher=null)
并没有得到想要的效果,我们还发现 teacher 属性是空的。
那如何解决呢,我们需要使用结果集映射
9.5 梳理思路
我们先来梳理下思路
- 查询所有Student
- 将得到的tid去查询对应的老师
- 将查询到的老师绑定到对应字段上
那么我们就有两种查询思路
- 按照步骤来处理
- 按照结果来处理
9.6 按步骤处理
<?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.lu.dao.StudentMapper">
<!-- 查询思路:
需求分析:
我们的目标是查找所有学生
已知学生字段类型:
id int
name String
teacher Teacher
我们发现字段 teacher 不是基本类型,并且数据库中没有这个字段,那么我们就要考虑 使用结果集映射
-->
<!--1. 查询所有学生,对结果进行映射-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<!--2. 前几个字段是默认的,不需要处理-->
<resultMap id="StudentTeacher" type="Student">
<!--3. 这里字段3 进行实体类关联-->
<association property="teacher" column="tid" select="getTeacher" javaType="Teacher"/>
</resultMap>
<!--通过上面传递过来的tid 查询对应老师-->
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid};
</select>
</mapper>
Student(id=1, name=Tom, teacher=Teacher(id=1, name=Teacher Zhang))
Student(id=2, name=Jack, teacher=Teacher(id=1, name=Teacher Zhang))
Student(id=3, name=Alice, teacher=Teacher(id=1, name=Teacher Zhang))
Student(id=4, name=Eric, teacher=Teacher(id=2, name=Teacher Li))
Student(id=5, name=Bob, teacher=Teacher(id=2, name=Teacher Li))
我们来梳理下查询过程
- 建立SQL
- 对结果进行映射
实体类字段进行关联
-
9.7 按结果处理
既然是按照结果处理,那么我们就生成结果,然后再梳理之间的关系.
select s.id,s.name,t.name
from student as s,teacher as t
where s.tid=t.id;
```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">
-
<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>
``` 我们来梳理下查询过程
- 建立多表查询SQL
- 给对应字段起别名
- 绑定字段
- 映射类型
- 绑定字段