关键词搜索

源码搜索 ×
×

ssm-mybatis进阶之复杂结果集映射

发布2021-09-01浏览380次

详情内容

一、简单映射

先准备好数据库和工程,准备工作可以参考之前的记录。下面举例简单说明将数据库中查询的数据映射为对象是如何实现的:

  1. <select id="getAllStudent2" resultType="com.zx.mybatis.pojo.Student">
  2. SELECT *
  3. FROM student;
  4. </select>

  1. @Test
  2. public void getAllStudent2() {
  3. SqlSession sqlSession = MybatisUtils.getSqlSession();
  4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  5. for (Student student : mapper.getAllStudent2()) {
  6. System.out.println(student);
  7. }
  8. sqlSession.close();
  9. }

  1. mapper中编写方法:
  2. List<Student> getAllStudent2();

  3. 映射文件中写查询:
  4. 编写测试代码:
  5. 运行查看结果:

除了映射为pojo,还能映射到map中,具体可以看之前的几篇记录。

二、复杂结果映射简介

实际开发中除了这种只有简单基础类型的对象外,还会遇到一些复杂的情况,如在返回结果映射对象中包含内部类,这种情况在映射中主要分为一对一和一对多。 业务开发中,会将这类复杂对象转换为复杂的嵌套json返回给客户端。

  1. public class StudentAndClass {
  2. private int id;
  3. private String name;
  4. private String sex;
  5. private Class cls;
  6. }
  1. public class ClassStudent {
  2. private String cid;
  3. private String cname;
  4. private List<Student> students;
  5. }
  • 一对一型:包含内部类
  • 一对多型:包含内部对象列表

三、一对一型映射

这类对象特征是pojo中包含内部类。

List<StudentAndClass> getStudentAndClassBySex2(String s);
  1. @Test
  2. public void getStudentAndClassBySex2() {
  3. SqlSession sqlSession = MybatisUtils.getSqlSession();
  4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  5. for (StudentAndClass studentAndClass : mapper.getStudentAndClassBySex2("男")) {
  6. System.out.println(studentAndClass);
  7. }
  8. sqlSession.close();
  9. }

  1. @Test
  2. public void getStudentAndClassBySex2() {
  3. SqlSession sqlSession = MybatisUtils.getSqlSession();
  4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  5. List<StudentAndClass> list = mapper.getStudentAndClassBySex2("男");
  6. for (StudentAndClass studentAndClass : list) {
  7. System.out.println(studentAndClass);
  8. }
  9. //输出json
  10. System.out.println(JSON.toJSON(list));
  11. sqlSession.close();
  12. }

  1. mapper中编写方法:
  2. 映射文件中写查询:
  3. 这里的查询相比上面的简单查询来说就比较复杂了,先看个实例:


    执行查询方法getStudentAndClassBySex2后,结果集映射对象类型为com.zx.mybatis.pojo.StudentAndClass, 过程中字段有通过property属性进行原始字段column的重命名。由于StudentAndClass对象中包含内部类, 需要使用标签association表示关联一对一对象,标签的javaType属性表示内部类的类型。pojo结构如下:
    1. public class StudentAndClass {
    2. private int id;
    3. private String name;
    4. private String sex;
    5. private Class cls;
    6. }
    1. public class Class {
    2. private String id;
    3. private String name;
    4. }
  4. 编写测试代码:
  5. 运行查看结果:
  6. pom.xml中引用fastjson依赖,将数据转换为json输出:

四、一对多型映射

一对多型特征是pojo中包含内部类列表。

List<StudentAndClass> getStudentAndClassBySex2(String s);
  1. @Test
  2. public void getClassStudent2() {
  3. SqlSession sqlSession = MybatisUtils.getSqlSession();
  4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  5. ClassStudent classStudent2 = mapper.getClassStudent2(1);
  6. System.out.println(classStudent2);
  7. Object jsonStr = JSON.toJSON(classStudent2);
  8. System.out.println(jsonStr);
  9. sqlSession.close();
  10. }

  1. mapper中编写方法:
  2. 映射文件中写查询:
  3. 和一对一类似的mapper映射文件写法:


    执行查询方法getClassStudent2后,结果集映射对象类型为com.zx.mybatis.pojo.ClassStudent, 过程中字段有通过property属性进行原始字段column的重命名。由于ClassStudent对象中包含内部类的List对象, 需要使用标签collection表示关联集合,javaType属性表示内部类的List类型,ofType属性表示List所封装的类型。 pojo结构如下:
    1. public class ClassStudent {
    2. private String cid;
    3. private String cname;
    4. private List<Student> students;
    5. }
    1. public class Student {
    2. private int id;
    3. private String name;
    4. private String sex;
    5. }
  4. 编写测试代码:
  5. 运行查看结果:

五、复杂映射为Map+List

除了将复杂映射为一对一和一对多的pojo对象,还可以将其映射为Map和List,如果实际开发中,不需要构造pojo对象进行复杂逻辑,只是将查询的记过数据返回到客户端, 完全可以使用Map和List对象映射,这样做可以省去构建pojo工作。

  1. 一对一:
  2. resultMap标签中的type属性和association标签中的javaType属性,都用map, 结合上述一对一映射为简单pojo原理可以联想到,这里也python教程是将查询结果映射为map+内部map结构。
    编写测试方法输出结果如下:
    1. @Test
    2. public void getStudentAndClassBySex3() {
    3. SqlSession sqlSession = MybatisUtils.getSqlSession();
    4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    5. List<Map<Object, Object>> list = mapper.getStudentAndClassBySex3("男");
    6. for (Map<Object, Object> map : list) {
    7. System.out.println(map);
    8. }
    9. Object jsonStr = JSON.toJSON(list);
    10. System.out.println(jsonStr);
    11. sqlSession.close();
    12. }

  3. 一对多:
  4. resultMap标签中的type属性用mapassociation标签中javaType属性用listofType属性用map表示内部为:List所封装的Map结构。
    编写测试方法输出结果如下:
    1. @Test
    2. public void getClassStudent() {
    3. SqlSession sqlSession = MybatisUtils.getSqlSession();
    4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    5. Map<Object, Object> classStudent = mapper.getClassStudent(1);
    6. System.out.println(classStudent);
    7. Object jsonStr = JSON.toJSON(classStudent);
    8. System.out.println(jsonStr);
    9. sqlSession.close();
    10. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载