• Mybatis关联查询之一对多和多对一XML配置详解


    Mybatis关联查询之一对多和多对一XML配置详解

    2017年12月21日 10:53:05 esinsis 阅读数:72642

    平时在开发过程中dao、bean和XML文件都是自动生成的,很少写XML的配置关系,今天记录一下mybatis的关联查询中的多对一和一对多的情况。

    • 首先是有两张表(学生表Student和老师Teacher表),为了更易懂,这里只设置了最简单的几个必要字段。表结构如下图

    Student表:

    Teacher表:

    • 创建实体bean
    Teacher.java:
    
     
    1. import java.util.List;

    2.  
    3. /**

    4. * TODO

    5. * @version 创建时间:2017年12月21日 上午9:02:45

    6. */

    7. public class Teacher {

    8.  
    9. private Integer id;

    10. private String name;

    11. private String className;

    12. private List<Student> students;

    13.  
    14. public List<Student> getStudents() {

    15. return students;

    16. }

    17.  
    18. public void setStudents(List<Student> students) {

    19. this.students = students;

    20. }

    21.  
    22. public Integer getId() {

    23. return id;

    24. }

    25.  
    26. public void setId(Integer id) {

    27. this.id = id;

    28. }

    29.  
    30. public String getName() {

    31. return name;

    32. }

    33.  
    34. public void setName(String name) {

    35. this.name = name;

    36. }

    37.  
    38. public String getClassName() {

    39. return className;

    40. }

    41.  
    42. public void setClassName(String className) {

    43. this.className = className;

    44. }

    45.  
    46. }

    Sfudent.java

    
     
    1. /**

    2. * TODO

    3. *

    4. * @author 作者 E-mail:2332999366@qq.com

    5. * @version 创建时间:2017年12月21日 上午9:01:17

    6. */

    7. public class Student {

    8.  
    9. private Integer id;

    10. private String name;

    11. private Integer teacherId;

    12. private String className;

    13. private Teacher teacher;

    14.  
    15.  
    16. public Teacher getTeacher() {

    17. return teacher;

    18. }

    19.  
    20. public void setTeacher(Teacher teacher) {

    21. this.teacher = teacher;

    22. }

    23.  
    24. public Integer getId() {

    25. return id;

    26. }

    27.  
    28. public void setId(Integer id) {

    29. this.id = id;

    30. }

    31.  
    32. public String getName() {

    33. return name;

    34. }

    35.  
    36. public void setName(String name) {

    37. this.name = name;

    38. }

    39.  
    40. public Integer getTeacherId() {

    41. return teacherId;

    42. }

    43.  
    44. public void setTeacherId(Integer teacherId) {

    45. this.teacherId = teacherId;

    46. }

    47.  
    48. public String getClassName() {

    49. return className;

    50. }

    51.  
    52. public void setClassName(String className) {

    53. this.className = className;

    54. }

    55.  
    56. @Override

    57. public String toString() {

    58. return "{id:"+this.id+",name:"+this.name+",className:"+this.className+",teacherId:"+this.teacherId+"}";

    59. }

    60. }

    • 下面重点来了:配置Mapper.xml文件:
    
     
    1. <?xml version="1.0" encoding="UTF-8" ?>

    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    3. <mapper namespace="com.tz.mybatis.dao.studentDao">

    4.  
    5. <!-- /////////////////////////////////一对多的第一种写法,一般考虑到性能问题,不会这么实现//////////////////////// -->

    6. <resultMap type="Teacher" id="teacherMap">

    7. <id column="id" property="id"/>

    8. <result column="name" property="name"/>

    9. <collection property="students" ofType="Student" column="id">

    10. <id column="sid" property="id"/><!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->

    11. <result column="sname" property="name"/><!-- property对应JavaBean中的属性名 -->

    12. <result column="className" property="className"/>

    13. </collection>

    14. </resultMap>

    15.  
    16.  
    17. <!-- 查询所有的老师级各自的所有学生 -->

    18. <select id="getTeachers" parameterType="Teacher" resultMap="teacherMap">

    19. SELECT

    20. t.id,

    21. t.NAME,

    22. t.class_Name,

    23. s.id AS sid,

    24. s. NAME AS sname,

    25. s.class_name as className

    26. FROM

    27. teacher t

    28. LEFT JOIN student s ON t.id = s.teacher_id

    29. </select>

    30. </mapper>

    • 测试类:
     
    
     
    1. package com.tz.test;

    2.  
    3. import java.io.IOException;

    4. import java.io.InputStream;

    5. import java.util.List;

    6. import org.apache.ibatis.io.Resources;

    7. import org.apache.ibatis.session.SqlSession;

    8. import org.apache.ibatis.session.SqlSessionFactory;

    9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;

    10. import org.junit.Before;

    11. import org.junit.Test;

    12. import com.tz.mybatis.bean.Student;

    13. import com.tz.mybatis.bean.Teacher;

    14.  
    15. public class TeacherTest {

    16.  
    17. private SqlSessionFactory sqlSessionFactory;

    18.  
    19. @Before

    20. public void init() throws IOException {

    21. String resource = "mybatis-config.xml";

    22. InputStream inputStream = Resources.getResourceAsStream(resource);

    23. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    24. }

    25.  
    26. @Test

    27. public void getTeachers() {

    28. SqlSession session = sqlSessionFactory.openSession();

    29. List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getTeachers");

    30. System.out.println(list);

    31. }

    32.  
    33. }

    下面给出第二种写法:

    
     
    1. <!-- //////////////////////////////////////////////一对多的第二种写法///////////////////////////////////////////////////// -->

    2. <resultMap type="Teacher" id="teacherMaps">

    3. <id column="id" property="id"/>

    4. <result column="name" property="name"/>

    5. <result column="class_name" property="className"/>

    6. <collection property="students" ofType="Student" select="getStudents" column="id">

    7. </collection>

    8. </resultMap>

    9.  
    10.  
    11. <!-- 查询所有的老师级各自的所有学生 -->

    12. <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps">

    13. SELECT

    14. t.id,

    15. t.NAME,

    16. t.class_name

    17. FROM

    18. teacher t

    19. </select>

    20.  
    21. <select id="getStudents" parameterType="int" resultType="Student">

    22. select

    23. s.id,

    24. s. NAME,

    25. s.class_name as className

    26. from student s

    27. where teacher_id = #{id}

    28. </select>


    测试类:
    
     
    1. @Test

    2. public void getTeachers2() {

    3. SqlSession session = sqlSessionFactory.openSession();

    4. List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getAllTeacher");

    5. System.out.println(list);

    6. }

    查询学生信息(多对一):

    首先还是配置文件:

    
     
    1. <resultMap type="Student" id="studentMap">

    2. <id column="id" property="id"/>

    3. <result column="name" property="name"/>

    4. <result column="class_name" property="className"/>

    5. <result column="teacher_id" property="teacherId"/>

    6. <association property="teacher" select="getTeacher" column="teacher_id" javaType="Teacher">

    7. <!-- 这里要注意的是column对应的是student中的外键,而且需是表字段名 -->

    8. </association>

    9. </resultMap>

    10.  
    11.  
    12. <select id="getStudent" resultMap="studentMap">

    13. SELECT

    14. s.id,

    15. s.name,

    16. s.class_name,

    17. s.teacher_id

    18. FROM

    19. student s

    20. </select>

    21.  
    22. <select id="getTeacher" resultType="Teacher" parameterType="int">

    23. SELECT

    24. t.id,

    25. t.name,

    26. t.class_name as className

    27. FROM teacher t

    28. where id = #{teacher_id}

    29. </select>


    测试类:
    
     
    1. @Test

    2. public void getStudents() {

    3. SqlSession session = sqlSessionFactory.openSession();

    4. List<Student> list = session.selectList("com.tz.mybatis.dao.studentDao.getStudent");

    5. System.out.println(list);

    6. }


    最后:当然如果不想配置这么麻烦的信息,可以直接写一个关联查询的SQL语句,返回结果直接由Map接受即可。不过这样就不太符合面向对象的理念了。
  • 相关阅读:
    Celery 分布式任务队列入门
    异步通信----WebSocket
    爬虫框架之scrapy
    《JavaScript 高级程序设计》第一章:简介
    NodeJS学习:环境变量
    cmd 与 bash 基础命令入门
    H5开发中的故障
    认识 var、let、const
    netsh & winsock & 对前端的影响
    scrollify
  • 原文地址:https://www.cnblogs.com/grj001/p/12225552.html
Copyright © 2020-2023  润新知