当前位置:   article > 正文

MyBatis中一对多_mybatis一对多

mybatis一对多

一对多的理解:

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

数据库可参考多对一

实例:


下面的例子是在SpringBoot环境下搭建的。这里有两种方式来进行多对一的查询。

按照查询进行嵌套处理就像SQL中的子查询
按照结果进行嵌套处理就像SQL中的联表查询


只举一种来说,按照结果进行嵌套处理就像SQL中的联表查询。比较好理解。

1. pom.xml 依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.mybatis.spring.boot</groupId>
  7. <artifactId>mybatis-spring-boot-starter</artifactId>
  8. <version>2.1.1</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <scope>test</scope>
  18. </dependency>


2. application.properties

  1. mybatis.mapper-locations=classpath:mapper/*.xml
  2. mybatis.type-aliases-package=com.cc.pojo
  3. mybatis.config-location=classpath:mybatis-config.xml
  4.  
  5. spring.datasource.username=root
  6. spring.datasource.password=123456
  7. spring.datasource.url=jdbc:mysql://localhost:3306/mis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8


3.日志配置(这里采用mybatis标准的日志。STDOUT_LOGGING。)

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3.         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.     <settings>
  7.         <setting name="logImpl" value="STDOUT_LOGGING" />
  8.     </settings>
  9. </configuration>


4 主要代码

1) pojo包下的Student类,Teacher类。

package com.cc.pojo; 

  1. package com.cc.pojo;
  2. import lombok.Data;
  3. public class Student {
  4. private int id;
  5. private String name;
  6. private int tid;
  7. //GET,SET,ToString,有参,无参构造
  8. }
  1. package com.cc.pojo;
  2. import java.util.List;
  3. public class Teacher {
  4. private int id;
  5. private String name;
  6. //一个老师多个学生
  7. private List<Student> students;
  8. }

2)mapper下的接口。

package com.cc.mapper;
 

  1. package com.cc.mapper;
  2. import org.apache.ibatis.annotations.Mapper;
  3. @Mapper
  4. public interface StudentMapper {
  5. }
  1. package com.cc.mapper;
  2. import com.cc.pojo.Teacher;
  3. import org.apache.ibatis.annotations.Mapper;
  4. @Mapper
  5. public interface TeacherMapper {
  6. //获取指定老师,及老师下的所有学生
  7. public Teacher getTeacher(int id);
  8. }

3)编写Mapper接口对应的 mapper.xml配置文件 

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.cc.mapper.StudentMapper">
  6. </mapper>
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.cc.mapper.TeacherMapper">
  6. <!--
  7. 思路:
  8. 1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
  9. 2. 对查询出来的操作做结果集映射
  10. 1. 集合的话,使用collection!
  11. JavaType和ofType都是用来指定对象类型的
  12. JavaType是用来指定pojo中属性的类型
  13. ofType指定的是映射到list集合属性中pojo的类型。
  14. -->
  15. <select id="getTeacher" resultMap="TeacherStudent">
  16. select s.id sid, s.name sname , t.name tname, t.id tid
  17. from student s,teacher t
  18. where s.tid = t.id and t.id=#{tid}
  19. </select>
  20. <resultMap id="TeacherStudent" type="Teacher">
  21. <result property="id" column="tid"/>
  22. <result property="name" column="tname"/>
  23. <collection property="students" ofType="Student">
  24. <result property="id" column="sid" />
  25. <result property="name" column="sname" />
  26. <result property="tid" column="tid" />
  27. </collection>
  28. </resultMap>
  29. </mapper>

4)测试

  1. package com.cc;
  2. import com.cc.mapper.TeacherMapper;
  3. import com.cc.pojo.Teacher;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest
  11. public class TeacherTest {
  12. @Autowired
  13. private TeacherMapper teacherMapper;
  14. @Test
  15. public void testTeacher() {
  16. Teacher teacher = teacherMapper.getTeacher(2);
  17. System.out.println(teacher);
  18. }
  19. }

5)运行结果

Teacher{id=2, name='大老师', students=[Student(id=1, name=小明, tid=2), Student(id=2, name=小红, tid=2), Student(id=3, name=小张, tid=2), Student(id=4, name=小李, tid=2), Student(id=5, name=小王, tid=2)]}

 

注意:resultMap映射的时候属性不能少,不然输不出来。

 

对于多对一和一对多的小结:

  1. 关联-association
  2. 集合-collection
  3. 所以association是用于一对一和多对一,而collection是用于一对多的关系
  4. JavaType和ofType都是用来指定对象类型的

    • JavaType是用来指定pojo中属性的类型
    • ofType指定的是映射到list集合属性中pojo的类型。

 


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/830582
推荐阅读
相关标签
  

闽ICP备14008679号