mybati处理结果集二

MyBatis 一对多和多对一的关系

配置和jar包

MyBatisConfig和MyBatisUtils(SqlSessionFactory工厂)配置和一对一配置一样,详情见一对一配置,架包也一样


实体类

  1. public class Classes {
  2. private int cid;
  3. private String cname;
  4. private String cdate;
  5. List<Student> stus;

  1. public class Student {
  2. private int sid;
  3. private String sname;
  4. private String sex;
  5. private int age;
  6. private String addr;
  7. private String birth;
  8. private int cid;
  9. private String major;
  10. private Classes classes;


方法一: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="test">
  6. <!-- 一对多 -->
  7. <resultMap type="Classes" id="Classess" >
  8. <id column="cid" property="cid" />
  9. <collection property="stus" column="cid" select="findStudent" />
  10. </resultMap>
  11. <select id="findStudent" parameterType="int" resultType="Student">
  12. select * from Student where cid=#{value}
  13. </select>
  14. <select id="seeAll" resultMap="Classess">
  15. select * from Classes
  16. </select>
  17. <!-- 多对一 -->
  18. <resultMap type="Student" id="seeStudents">
  19. <id column="sid" property="sid" />
  20. <association property="classes" column="cid" select="seeClasses" />
  21. </resultMap>
  22. <select id="seeClasses" resultType="Classes" parameterType="int" >
  23. select * from Classes where cid=#{value}
  24. </select>
  25. <select id="seeAllStudents" resultMap="seeStudents">
  26. select * from student
  27. </select>
  28. </mapper>

在Mapper.xml配置中一对一和多对一的映射关联用: association,一对多和多对多用 collection

测试类

  1. //一对多
  2. @org.junit. Test
  3. public void test () throws Exception {
  4. SqlSession session = MyBatisUtils.getSqlSession();
  5. List<Classes> list = session.selectList( "test.seeAll");
  6. for(Classes cl:list){
  7. System.out.println(cl);
  8. for(Student s:cl.getStus()){
  9. System.out.println(s);
  10. }
  11. }
  12. }
  13. //多对一
  14. @org.junit. Test
  15. public void test2 () throws Exception {
  16. SqlSession session = MyBatisUtils.getSqlSession();
  17. List<Student> list = session.selectList( "test.seeAllStudents");
  18. for(Student st:list){
  19. System.out.println(st+ "____"+st.getClasses());
  20. }
  21. }


方法二:注解方式

接口类

一对多(ClassesDao)

  1. package com.mingde.dao;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Many;
  4. import org.apache.ibatis.annotations.Result;
  5. import org.apache.ibatis.annotations.ResultMap;
  6. import org.apache.ibatis.annotations.Results;
  7. import org.apache.ibatis.annotations.Select;
  8. import org.apache.ibatis.mapping.FetchType;
  9. import com.mingde.po.Classes;
  10. public interface ClassesDao {
  11. //查询所有
  12. @Select( "select * from Classes")
  13. //定义ResultMap
  14. @Results(id= "ClassesResult",value={
  15. @Result(column= "cid",property= "cid"),
  16. @Result(column= "cid",property= "stus",many= @Many(select= "com.mingde.dao.StudentDao.findStudents",fetchType=FetchType.LAZY))
  17. })
  18. public List<Classes> SeeAllClasses()throws Exception;
  19. //根据id查询
  20. @Select( "select * from Classes where cid=#{value}")
  21. @ResultMap( "ClassesResult") //引入上面定义的ResultMap
  22. public Classes findClasses(int cid)throws Exception;
  23. }

多对一(StudentDao)

  1. package com.mingde.dao;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.One;
  4. import org.apache.ibatis.annotations.Result;
  5. import org.apache.ibatis.annotations.ResultMap;
  6. import org.apache.ibatis.annotations.Results;
  7. import org.apache.ibatis.annotations.Select;
  8. import org.apache.ibatis.mapping.FetchType;
  9. import com.mingde.po.Student;
  10. public interface StudentDao {
  11. //根据班级cid查询
  12. @Select( "select * from student where cid=#{value}")
  13. public List<Student> findStudents(int cid)throws Exception;
  14. //查询所有
  15. @Select( "select * from student")
  16. @Results(id= "findClasses",value={ //定义ResultMap
  17. @Result(column= "sid",property= "sid"),
  18. @Result(column= "cid",property= "classes",one= @One(select= "com.mingde.dao.ClassesDao.findClasses",fetchType=FetchType.LAZY))
  19. })
  20. public List<Student> SeeStudents()throws Exception;
  21. //根据学生sid查询
  22. @Select( "select * from student where sid=#{value}")
  23. @ResultMap( "findClasses") //引用上面的ResultMap
  24. public Student findStudentById(int sid)throws Exception;
  25. }

注意:上面的配置是即配置一对多也配置多对一,这里是为了学者学习;但一般配置一对多后,就不会多对一,因为这样互相配置后会产生循环加载,导致内存溢出


测试类

  1. //注解一对多
  2. @org.junit. Test
  3. public void test3 () throws Exception {
  4. //获取工厂,连接接口使用方法
  5. SqlSession session = MyBatisUtils.getSqlSession();
  6. ClassesDao mapper = session.getMapper(ClassesDao.class);
  7. //查询所有
  8. List<Classes> seeAllClasses = mapper.SeeAllClasses();
  9. for(Classes c:seeAllClasses){
  10. System.out.println(c);
  11. for(Student st:c.getStus()){
  12. System.out.println(st);
  13. }
  14. }
  15. //根据id查询
  16. Classes findClasses = mapper.findClasses( 1);
  17. System.out.println(findClasses);
  18. System.out.println(findClasses.getStus());
  19. }
  20. //注解多对一
  21. @org.junit. Test
  22. public void test4 () throws Exception {
  23. //获取工厂,连接接口使用方法
  24. SqlSession session = MyBatisUtils.getSqlSession();
  25. StudentDao mapper = session.getMapper(StudentDao.class);
  26. //查询所有
  27. List<Student> seeStudents = mapper.SeeStudents();
  28. for(Student s:seeStudents){
  29. System.out.println(s+ "______"+s.getClasses());
  30. }
  31. //根据id查询
  32. Student st = mapper.findStudentById( 1126);
  33. System.out.println(st);
  34. System.out.println(st.getClasses());
  35. }


总结:

1、在Mapper.xml配置中一对一和多对一的映射关联用:association,一对多和多对多用collection

2、一般配置一对多后,就不会多对一,因为这样互相配置后会产生循环加载,导致内存溢出

3、配置一对多或多对一的依赖关系,主要抓住配置二者的主键和外建,如:


  1. //查询所有
  2. @Select( "select * from student")
  3. @Results(id= "findClasses",value={ //定义ResultMap
  4. @Result(column= "sid",property= "sid"), //主键,下面的是外建
  5. @Result(column= "cid",property= "classes",one= @One(select= "com.mingde.dao.ClassesDao.findClasses",fetchType=FetchType.LAZY))
  6. })
  7. public List<Student> SeeStudents()throws Exception;


  1. <!-- 一对多 -->
  2. <resultMap type="Classes" id="Classess" >
  3. <id column="cid" property="cid" /> //主键id
  4. <collection property="stus" column="cid" select="findStudent" /> //外建
  5. </resultMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值