1、多对一
多对一的理解:
- 多个学生对应一个老师
- 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师!
1.1、数据库设计
CREATE TABLE `teacher` (
`id` int(11) 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(11) NOT NULL,
`name` varchar(30) DEFAULT NULL,
`tid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `tid` (`tid`),
CONSTRAINT `student_ibfk_1` 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)
1.2、搭建环境
1、IDEA安装Lombok插件
2、引入Maven依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
3、编写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private Integer id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Teacher teacher;
}
4、编写相应的Mapper接口
public interface StudentMapper {
}
5、新建对应的映射文件
<?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="org.com.mapper.StudentMapper">
</mapper>
1.3、按查询嵌套处理
1、在StudentMapper接口中怎加一个方法
// 查找所有学生
List<Student> studentList();
2、编写相应的映射文件
<?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="org.com.mapper.StudentMapper">
<!-- 子查询方式解决多对一问题 -->
<resultMap id="studentMap" type="student">
<!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
<association property="teacher" column="tid" select="selectTeacherById" javaType="teacher"/>
</resultMap>
<select id="studentList" resultMap="studentMap" >
select * from student
</select>
<!--
这里传递过来的id,只有一个属性的时候,下面可以写任何值
association中column多参数配置:
column="{key=value,key=value}"
其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
-->
<select id="selectTeacherById" resultType="teacher">
select * from teacher where id = #{id}
</select>
</mapper>
3、在MyBatis核心配置中注册Mapper
<mappers>
<mapper class="org.com.mapper.StudentMapper"/>
</mappers>
4、测试
@Test
public void test1(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.studentList();
for (Student student : studentList) {
System.out.println(student);
}
MyBatisUtils.closeSqlSession();
}
结果:
1.4、按查询结果处理
1、在StudentMapper接口中新增一个方法
// 查找所有学生
List<Student> studentList2();
2、编写相应的映射文件
<?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="org.com.mapper.StudentMapper">
<!-- 按查询结果处理多对一问题 -->
<select id="studentList2" resultMap="studentMap2">
select s.id as s_id,s.name as s_name,t.id as t_id,t.name as t_name
from student s
inner join teacher t on s.tid = t.id
</select>
<resultMap id="studentMap2" type="student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<association property="teacher" javaType="teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
</mapper>
3、去mybatis-config文件中注册【此处应该处理过了】
4、测试
@Test
public void test2(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.studentList2();
for (Student student : studentList) {
System.out.println(student);
}
MyBatisUtils.closeSqlSession();
}
结果与1.3的一样
小结:
按照查询进行嵌套处理就像SQL中的子查询
按照结果进行嵌套处理就像SQL中的联表查询
2、一对多
一对多的理解:
- 一个老师拥有多个学生
- 如果对于老师这边,就是一个一对多的现象,即从一个老师下面拥有一群学生(集合)!
2.1、搭建环境
1、编写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private Integer id;
private String name;
private List<Student> studentList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer tid;
}
剩下的和上面多对一的一样,搭完环境要先测试一下!
2.2、按查询嵌套处理
1、在TeacherMapper中添加一个方法
public interface TeacherMapper {
// 查询所有老师信息
List<Teacher> teacherList();
}
2、编写相应的映射文件
<!-- 按查询嵌套处理 -->
<select id="teacherList" resultMap="teacherMap">
select * from teacher
</select>
<resultMap id="teacherMap" type="teacher">
<id property="id" column="id"/>
<collection property="studentList" javaType="list" column="id" select="selectStudentById"/>
</resultMap>
<select id="selectStudentById" resultType="student">
select * from student where tid=#{id}
</select>
3、在MyBatis核心配置文件中注册Mapper
<mappers>
<mapper class="org.com.mapper.TeacherMapper"/>
</mappers>
4、测试
@Test
public void test1(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacherList = mapper.teacherList();
for (Teacher teacher : teacherList) {
System.out.println(teacher);
}
MyBatisUtils.closeSqlSession();
}
结果:
2.3、按查询结果处理
1、在TeacherMapper中添加一个方法
// 查询所有老师信息
List<Teacher> teacherList2();
2、映射文件
<?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="org.com.mapper.TeacherMapper">
<!-- 按查询结果处理 -->
<!--
思路:
1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
2. 对查询出来的操作做结果集映射
1. 集合的话,使用collection!
JavaType和ofType都是用来指定对象类型的
JavaType是用来指定pojo中属性的类型
ofType指定的是映射到list集合属性中pojo的类型。
-->
<select id="teacherList2" resultMap="teacherMap2">
select t.id as t_id, t.name as t_name, s.id as s_id, s.name as s_name
from teacher t
inner join student s on t.id = s.tid
</select>
<resultMap id="teacherMap2" type="teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<collection property="studentList" ofType="student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="tid" column="t_id"/>
</collection>
</resultMap>
</mapper>
4、注册mapper,上面已经注册过了
5、测试
@Test
public void test2(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacherList = mapper.teacherList2();
for (Teacher teacher : teacherList) {
System.out.println(teacher);
}
MyBatisUtils.closeSqlSession();
}
结果与2.2的一样
3、总结
1、关联-association
2、集合-collection
3、所以association是用于一对一和多对一,而collection是用于一对多的关系
4、JavaType和ofType都是用来指定对象类型的
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
注意说明:
1、保证SQL的可读性,尽量通俗易懂
2、根据实际要求,尽量编写性能更高的SQL语句
3、注意属性名和字段不一致的问题
4、注意一对多和多对一 中:字段和属性对应的问题
5、尽量使用Log4j,通过日志来查看自己的错误