Mybatis复习

一、多对一的处理

多对一的理解

  • 多个学生对应一个老师
  • 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师!

1.数据库设计

Mybatis复习

2.搭建测试环境

Lombok

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version>
</dependency>

实体类编写

@Data //GET,SET,ToString,有参,无参构造
public class Teacher {
    private int id;
    private String name;
}
@Data
public class Student {
    private int id;
    private String name;
    //多个学生可以是同一个老师,即多对一
    private Teacher teacher;
}

编写Mapper接口

public interface StudentMapper {
}
public interface TeacherMapper {
}

编写Mapper.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.sjx.mapper.StudentMapper">
    
</mapper>
<?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.sjx.mapper.TeacherMapper">
    
</mapper>

3.按查询嵌套处理

  1. 给StudentMapper接口增加方法
//获取所有学生及对应老师的信息
public List<Student> getStudents();
  1. 编写对应的Mapper文件
思路:
1. 获取所有学生的信息
2. 根据获取的学生信息的老师ID->获取该老师的信息
3. 思考问题,这样学生的结果集中应该包含老师,该如何处理呢,数据库中我们一般使用关联查询?
    1. 做一个结果集映射:StudentTeacher
    2. StudentTeacher结果集的类型为 Student
    3. 学生中老师的属性为teacher,对应数据库中为tid。
	多个 [1,...)学生关联一个老师=> 一对一,一对多
4. 查看官网找到:association – 一个复杂类型的关联;使用它来处理关联查询
<?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.sjx.mapper.StudentMapper">
    <select id="getStudents" resultMap="StudentTeacher">
        select * from student
    </select>
    
    <resultMap id="StudentTeacher" type="Student">
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    
    <select id="getTeacher" resultType="teacher">
        select * from teacher where id = #{id}
    </select>
</mapper>
  1. 编写完毕去Mybatis配置文件中,注册Mapper!

  2. 注意点说明:

<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>
  1. 测试
@Test
public void testGetStudents(){
    SqlSession session = MybatisUtils.getSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List<Student> students = mapper.getStudents();
    for (Student student : students){
    System.out.println(
        "学生名:"+ student.getName()
        +"\t老师:"+student.getTeacher().getName());
    }
}

4.按结果嵌套处理(推荐使用)

  1. 接口方法编写
public List<Student> getStudents2()
  1. 编写对应的mapper文件
<select id="getStudents2" resultMap="StudentTeacher2" >
    select s.id sid, s.name sname , t.name tname
    from student s,teacher t
    where s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
	<id property="id" column="sid"/>
	<result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
    	<result property="name" column="tname"/>
    </association>
</resultMap>

二、一对多处理

一对多的理解:

  • 一个老师拥有多个学生
  • 如果对于老师这边,就是一个一对多的现象,即从一个老师下面拥有一群学生(集合)!

实体类编写

@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;
}

1.按结果嵌套处理

  1. TeacherMapper接口编写方法
//获取指定老师,及老师下的所有学生
public Teacher getTeacher(int id);
  1. 编写接口对应的Mapper配置文件
思路:
    1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
    2. 对查询出来的操作做结果集映射
       集合的话,使用collection!
       JavaType和ofType都是用来指定对象类型的
       JavaType是用来指定pojo中属性的类型
       ofType指定的是映射到list集合属性中pojo的类型。
<mapper namespace="com.sjx.mapper.TeacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid, s.name sname , t.name tname, t.id tid
    from student s,teacher t
    where s.tid = t.id and t.id=#{id}
</select>
<resultMap id="TeacherStudent" type="Teacher">
	<result 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>
</mapper>
  1. 将Mapper文件注册到MyBatis-config文件中

  2. 测试

@Test
public void testGetTeacher(){
    SqlSession session = MybatisUtils.getSession();
    TeacherMapper mapper = session.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher(1);
    System.out.println(teacher.getName());
    System.out.println(teacher.getStudents());
}

2.按查询嵌套处理

  1. TeacherMapper接口编写方法
public Teacher getTeacher2(int id);
  1. 编写接口对应的Mapper配置文件
<select id="getTeacher2" resultMap="TeacherStudent2">
	select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
	<!--column是一对多的外键 , 写的是一的主键的列名-->
<collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
	select * from student where tid = #{id}
</select>
  1. 将Mapper文件注册到MyBatis-config文件中

  2. 测试

@Test
public void testGetTeacher2(){
    SqlSession session = MybatisUtils.getSession();
    TeacherMapper mapper = session.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher2(1);
    System.out.println(teacher.getName());
    System.out.println(teacher.getStudents());
}

三、小结

  1. 关联-association
  2. 集合-collection
  3. 所以association是用于一对一和多对一,而collection是用于一对多的关系
  4. JavaType和ofType都是用来指定对象类型的
  • JavaType是用来指定pojo中属性的类型
  • ofType指定的是映射到list集合属性中pojo的类型。

Mybatis复习

上一篇:kingbaseES R6主备流复制集群创建级联复制案例


下一篇:网络报文分析利器eBPF