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

一、简单映射

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

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

@Test
public void getAllStudent2() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    for (Student student : mapper.getAllStudent2()) {
        System.out.println(student);
    }
    sqlSession.close();
}

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

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

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

二、复杂结果映射简介

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

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

三、一对一型映射

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

List<StudentAndClass> getStudentAndClassBySex2(String s);
@Test
public void getStudentAndClassBySex2() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    for (StudentAndClass studentAndClass : mapper.getStudentAndClassBySex2("男")) {
        System.out.println(studentAndClass);
    }
    sqlSession.close();
}

@Test
public void getStudentAndClassBySex2() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    List<StudentAndClass> list = mapper.getStudentAndClassBySex2("男");
    for (StudentAndClass studentAndClass : list) {
        System.out.println(studentAndClass);
    }
    //输出json
    System.out.println(JSON.toJSON(list));
    sqlSession.close();
}

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


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

四、一对多型映射

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

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

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


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

五、复杂映射为Map+List

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

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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架中的MyBatis升级到MyBatis-Plus是可行的,可以实现共存。SSM框架由Spring、Spring MVC和MyBatis组成,而MyBatis-Plus是对MyBatis的增强扩展。下面将介绍如何将它们共存。 首先,需要将MyBatis升级到MyBatis-Plus。可以将MyBatis-Plus的依赖项添加到项目的pom.xml文件中,替换原有的MyBatis依赖。然后,需要对原有的MyBatis配置文件进行修改。MyBatis-Plus提供了一些方便的功能和特性,如自动填充、逻辑删除等,可以根据项目需求选择开启或关闭。 在SSM框架中,MyBatis-Plus可以与原有的Spring框架和Spring MVC框架完美共存。Spring框架负责管理和配置各种Bean,MyBatis-Plus可以与Spring框架一起使用,将其作为DAO层的组件进行管理。在Spring的配置文件中,可以将MyBatis-Plus的配置文件加入到配置中。 在Spring MVC框架中,可以继续使用原有的控制器、服务和视图解析器等组件。MyBatis-Plus可以与Spring MVC框架无缝成,通过Spring MVC接收请求,然后调用MyBatis-Plus进行数据访问和处理。 在具体开发过程中,可以利用MyBatis-Plus提供的一些特性简化开发工作。例如,可以使用MyBatis-Plus的代码生成器来自动生成DAO、实体类和Mapper等代码,减少手动编写的工作量。 总结来说,将SSM框架中的MyBatis升级到MyBatis-Plus是完全可以实现的,它们可以共存并完美成。通过使用MyBatis-Plus,我们可以更加便捷地开发和管理数据库操作,提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值