服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Mybatis一对多与多对一查询处理详解

Mybatis一对多与多对一查询处理详解

2021-08-19 11:47KittyGuy Java教程

这篇文章主要给大家介绍了关于Mybatis一对多与多对一查询处理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

要点

  • 主要还是结果集映射(resultMap)
  • association标签: 一个复杂类型的关联;许多结果将包装成这种类型(JavaBean)嵌套结果映射,关联可以是 resultMap 元素,或是对其它结果映射的引用
  • collection标签: 一个复杂类型的集合(List)嵌套结果映射,集合可以是resultMap元素,或是对其它结果映射的引用

一对多(association)

数据库结构

Mybatis一对多与多对一查询处理详解

tid是student的外键,是teacher表的id

JavaBean

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

mapper.java

public interface StudentMapper {
 List<Student> getStudent();
 List<Student> getStudent2();
}

Student类里面有teacher,要想查出Student中的Teacher就需要映射。

方法有二

(1)

<select id="getStudent" resultMap="StudentTeacher">
    select * from mybatis.student
  </select>
  <resultMap id="StudentTeacher" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
  </resultMap>
        <!--子查询-->
  <select id="getTeacher" resultType="Teacher">
     select * from mybatis.teacher where id=#{Anything}
  </select>

Mybatis一对多与多对一查询处理详解

(2)

<select id="getStudent2" resultMap="StudentTeacher2">
    select * from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id
  </select>
  <resultMap id="StudentTeacher2" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" javaType="Teacher">
      <result property="id" column="tid"/>
      <result property="name" column="name"/>
    </association>
  </resultMap>

Mybatis一对多与多对一查询处理详解

多对一

JavaBean

public class Teacher2 {
  private int id;
  private String name;
  //一个老师对应多个学生
  private List<Student2> students;
}
public class Student2 {
  private int id;
  private String name;
  private int tid;
}

mapper.java

public interface TeacherMapper2 {
  List<Teacher2> getTeacher(@Param("id") int id);
}

mapper.xml

<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mybatis.DAO.TeacherMapper2">
  <select id="getTeacher" parameterType="int" resultMap="teacherS">
    select s.id, s.name, s.tid,t.id as tid, t.name as tname from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id and t.id=#{id}
  </select>
  <resultMap id="teacherS" type="teacher2">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="student2">
      <result property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="tid" column="tid"/>
    </collection>
  </resultMap>
</mapper>

Mybatis一对多与多对一查询处理详解

小结

  • 一对多和多对一区别不大
  • 其实就是association(类)和collection(集合)的区别
  • 还有ofType和javaType的区别
  • 如果查询结果不符合预期,请设置别名试一试

到此这篇关于Mybatis一对多与多对一查询处理的文章就介绍到这了,更多相关Mybatis一对多与多对一查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6934624701633265677

延伸 · 阅读

精彩推荐