Mybatis学习笔记来自B站狂神说Java Mybatis系列视频~
视频链接:https://www.bilibili.com/video/BV1NE411Q7Nx
纯个人手打,可能会有错误,欢迎指正交流探讨~
个人邮箱:460168087@qq.com
9、多对一
多对一的理解:
- 多个学生对应一个老师
- 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师!
1. 按查询嵌套处理
步骤:
-
创建实体类
//Student 实体类 package com.gang.pojo; import lombok.Data; @Data public class Student { private int id; private String name; private Teacher teacher; }
//Teacher 实体类 package com.gang.pojo; import lombok.Data; @Data public class Teacher { private int id; private String name; }
-
给StudentMapper接口增加方法
//按查询嵌套处理
public List<Student> getStudents();
-
在StudentMapper.xml中编写对应的sql语句
<!-- 按查询嵌套处理 1. 获取所有学生的信息 select * from mybatis.student 2. 结果集映射,上述查出来的结果,id,name类型均不变,但是mysql中的tid数据 在这里需要转换成与pojo对应的teacher类 类型 3. 嵌套查询 --> <select id="getStudents" resultMap="StudentTeacher"> select * from mybatis.student </select> <resultMap id="StudentTeacher" type="Student"> <!--association关联属性 property实体类中的属性名 column数据库对应字段名 javaType属性类型 select为嵌套查询指定的select语句的名字,与后面对应--> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <!-- 这里传递过来的id,只有一个属性的时候,下面可以写任何值 association中column多参数配置: column="{key=value,key=value}" 其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。 --> <select id="getTeacher" resultType="teacher"> select * from mybatis.teacher where id=#{id} </select> <!-- 传递多参数时 example: <resultMap id="StudentTeacher" type="Student"> <association property="teacher" column="{id=tid,name=tid}" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="teacher"> select * from teacher where id = #{id} and name = #{name} </select> -->
-
编写完后去Mybatis配置文件中,注册Mapper!
-
测试
@Test public void test1(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studentList = mapper.getStudents(); for (Student student : studentList) { System.out.println(student); } sqlSession.close(); }
2. 按结果嵌套处理
步骤:
-
接口编写
//按结果嵌套处理 public List<Student> getStudents2();
-
编写StudentMapper.xml
<!-- 按结果嵌套处理--> <select id="getStudents2" resultMap="StudentTeacher2"> select s.id sid,s.name sname, t.id tid,t.name tname from student s, teacher t where s.tid=t.id </select> <!-- 上述查询结果的返回类型为Student 类,因此进行结果集映射与之对应 然后Student中的teacher类型为Teacher实体类,基于association进行进一步的映射 --> <resultMap id="StudentTeacher2" type="Student"> <id property="id" column="sid"/> <id property="name" column="sname"/> <association property="teacher" javaType="Teacher" > <result property="id" column="tid"/> <result property="name" column="tname"/> </association> </resultMap>
-
测试
@Test public void test1(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studentList = mapper.getStudents2(); for (Student student : studentList) { System.out.println(student); } sqlSession.close(); }
3. 注意
- 按照查询进行嵌套处理就像SQL中的子查询,即先查一个表,然后在此基础上再查另一个表
- 按照结果进行嵌套处理就像SQL中的联表查询,一次性直接查询多个表
10、一对多
一对多的理解:
- 一个老师拥有多个学生
- 如果对于老师这边,就是一个一对多的现象,即从一个老师下面拥有一群学生(集合)!
1. 按查询嵌套处理
步骤:
-
实体类编写
@Data public class Student { private int id; private String name; private int tid; }
@Data public class Teacher { private int id; private String name; private List<Student> students; }
-
TeacherMapper接口编写
public Teacher getTeacher2(int id);
-
TeacherMapper.xml 编写
<!--按查询嵌套处理--> <select id="getTeacher2" resultMap="TeacherStudent2"> select id,name from teacher where id=#{id} </select> <resultMap id="TeacherStudent2" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudents"/> </resultMap> <select id="getStudents" resultType="Student"> select * from student where tid=#{id} </select>
-
将Mapper文件注册到MyBatis-config文件中
<!--使用class绑定接口--> <mappers> <mapper class="com.gang.mapper.StudentMapper"/> <mapper class="com.gang.mapper.TeacherMapper"/> </mappers>
-
测试
@Test public void Test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher2(1); System.out.println(teacher); sqlSession.close(); }
2. 按结果嵌套处理
步骤:
-
接口编写
public Teacher getTeacher(int id);
-
TeacherMapper.xml 编写
<!--按结果嵌套处理--> <select id="getTeacher" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid=t.id </select> <resultMap id="TeacherStudent" type="Teacher"> <id property="id" column="tid"/> <id property="name" column="tname"/> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap>
-
测试
@Test public void Test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher(1); System.out.println(teacher); sqlSession.close(); }
3. 注意
- 关联-association
- 集合-collection
- 所以association是用于一对一和多对一,而collection是用于一对多的关系
- JavaType和ofType都是用来指定对象类型的
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
注意说明:
- 保证SQL的可读性,尽量通俗易懂
- 根据实际要求,尽量编写性能更高的SQL语句
- 注意属性名和字段不一致的问题
- 注意一对多和多对一 中:字段和属性对应的问题
- 尽量使用Log4j,通过日志来查看自己的错误