mybatis返回结果处理

简介: mybatis返回结果处理

准备工作

本文以实体类为Car对象进行统一分析处理,其对应的数据库中的表如下

java中的Car对象如下:

1. /**
2.  * @author 风轻云淡
3.  */
4. @Data
5. public class Car {
6. /**
7.      * 包装类防止null问题
8.      */
9. private Long id;
10. 
11. private String carNum;
12. 
13. private String brand;
14. 
15. private Double guidePrice;
16. 
17. private String produceTime;
18. 
19. private String carType;
20. 
21. 
22. }

返回对象为pojo实体类(Car)

对应的接口

1. /**
2.      * 根据id查询
3.      * @param id
4.      * @return Car对象
5.      */
6.     Car selectById(Long id);

对应的xml文件

1. <select id="selectById" resultType="pojo.Car">
2.         select
3.                id as  id ,
4.                car_num as  carNum,
5.                brand as brand,
6.                guide_price as  guidePrice,
7.                car_type as  carType,
8.                produce_time as produceTime
9.         from t_car where id=#{id}
10. </select>

resultType我个人的理解就是我们查询sql语句返回的结构集的行的玩意,也就是一个记录

对应的测试代码:

1. @Test
2. public void test01(){
3. SqlSession session = SqlSessionUtil.openSession();
4. CarMapper mapper = session.getMapper(CarMapper.class);
5. Car car = mapper.selectById(3L);
6.         System.out.println(car);
7.     }

返回集合对象(List<Car>)

当查询的记录条数是多条的时候,必须使用集合接收。如果使用单个实体类接收会出现异常。如果返回的是一条记录可以用集合接收

对应的接口

1.     /**
2.      * 查询所有car
3.      * @return
4.      */
5.     List<Car> selectAll();

对应的xml文件

1. <select id="selectAll" resultType="pojo.Car">
2.         select
3.                  id as  id ,
4.                car_num as  carNum,
5.                brand as brand,
6.                guide_price as  guidePrice,
7.                car_type as  carType,
8.                produce_time as produceTime
9.         from t_car
10. </select>

对应的测试代码:

1. @Test
2. public void test02(){
3. SqlSession session = SqlSessionUtil.openSession();
4. CarMapper mapper = session.getMapper(CarMapper.class);
5.         List<Car> cars = mapper.selectAll();
6.         System.out.println(cars);
7.     }

返回Map

当返回的数据,没有合适的实体类对应的话,可以采用Map集合接收。字段名做key,字段值做value。查询如果可以保证只有一条数据,则返回一个Map集合即可。

对应的接口

1. /**
2.      * 通过id查询一条记录,返回Map集合
3.      * @param id
4.      * @return
5.      */
6.     Map<String, Object> selectByIdRetMap(Long id);

对应的xml文件

1. <select id="selectByIdRetMap" resultType="map">
2.         select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car where id = #{id}
3. </select>

resultMap="map",这是因为mybatis内置了很多别名。【参见mybatis开发手册】

对应的测试代码:

1. @Test
2. public void test03(){
3. SqlSession session = SqlSessionUtil.openSession();
4. CarMapper mapper = session.getMapper(CarMapper.class);
5.         Map<String, Object> map = mapper.selectByIdRetMap(3L);
6.         System.out.println(map);
7.     }

当然,如果返回一个Map集合,可以将Map集合放到List集合中吗?当然可以,这里就不再测试了。

反过来,如果返回的不是一条记录,是多条记录的话,只采用单个Map集合接收,这样同样会出现之前的异常:TooManyResultsException

返回List<Map>

查询结果条数大于等于1条数据,则可以返回一个存储Map集合的List集合。List<Map>等同于List<Car>

对应的接口

1. /**
2.      * 查询所有的Car,返回一个List集合。List集合中存储的是Map集合。
3.      * @return
4.      */
5.     List<Map<String,Object>> selectAllRetListMap();

对应的xml文件

1. <select id="selectAllRetListMap" resultType="map">
2.         select 
3.             id,car_num 
4.             carNum,brand,
5.             guide_price 
6.             guidePrice,
7.             produce_time 
8.             produceTime,car_type carType 
9.         from t_car
10. </select>

对应的测试代码:

1. @Test
2. public void test04(){
3. SqlSession session = SqlSessionUtil.openSession();
4. CarMapper mapper = session.getMapper(CarMapper.class);
5.         List<Map<String, Object>> list = mapper.selectAllRetListMap();
6.         System.out.println(list);
7.     }

返回Map<String,Map>

拿Car的id做key,以后取出对应的Map集合时更方便。

 对应的接口

1. /**
2.      * 获取所有的Car,返回一个Map集合。
3.      * Map集合的key是Car的id。
4.      * Map集合的value是对应Car。
5.      * @return
6.      */
7. @MapKey("id")
8. Map<Long,Map<String,Object>> selectAllRetMap();

@MapKey("id")指定一个字段作为返回Map中的key,一般使用唯一键来做key

对应的xml文件

1. <select id="selectAllRetMap" resultType="map">
2.         select 
3.                id,
4.                car_num carNum,
5.                brand,
6.                guide_price guidePrice,
7.                produce_time produceTime,
8.                car_type carType 
9.         from t_car
10. </select>

对应的测试代码:

1. @Test
2. public void test05(){
3. SqlSession session = SqlSessionUtil.openSession();
4. CarMapper mapper = session.getMapper(CarMapper.class);
5.         Map<Long, Map<String, Object>> map = mapper.selectAllRetMap();
6.         System.out.println(map);
7.     }

返回总记录条数

需求:查询总记录条数

 对应的接口

1. /**
2.      * 获取总记录条数
3.      * @return
4.      */
5. Long selectTotal();

对应的xml文件

1. <!--long是别名,可参考mybatis开发手册。-->
2. <select id="selectTotal" resultType="long">
3.   select count(*) from t_car
4. </select>

对应的测试代码:

1. @Test
2. public void testSelectTotal(){
3. CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
4. Long total = carMapper.selectTotal();
5.     System.out.println(total);
6. }

要点

  • (1)resultType 属性可以指定结果集的类型,它支持基本类型和实体类类型。我们在前面的 CRUD 案例中已经对此属性进行过应用了。
  • (2)需要注意的是,它和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须使用全限定类名。例如:我们的实体类此时必须是全限定类名
  • (3)同时,当是实体类名称是,还有一个要求,实体类中的属性名称必须和查询语句中的列名保持一致,否则无法实现封装。

相关文章
|
5月前
|
Java 数据库连接 mybatis
mybatis返回结果为List<Map<String, Object>>的写法
mybatis返回结果为List<Map<String, Object>>的写法
358 1
|
5月前
|
SQL 存储 Java
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
65 0
|
11月前
|
SQL Java 数据库连接
Mybatis使用collection标签实现一对多关联查询,返回结果集list中嵌套list
Mybatis使用collection标签实现一对多关联查询,返回结果集list中嵌套list
424 0
|
SQL 缓存 Java
Mybatis Sql 执行全链路, 跟踪返回结果
Mybatis Sql 执行全链路, 跟踪返回结果
182 0
Mybatis Sql 执行全链路, 跟踪返回结果
|
21天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
8天前
|
SQL XML Java
springboot整合mybatis-plus及mybatis-plus分页插件的使用
这篇文章介绍了如何在Spring Boot项目中整合MyBatis-Plus及其分页插件,包括依赖引入、配置文件编写、SQL表创建、Mapper层、Service层、Controller层的创建,以及分页插件的使用和数据展示HTML页面的编写。
springboot整合mybatis-plus及mybatis-plus分页插件的使用
|
2月前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
93 1
|
2月前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
2月前
|
Java 关系型数据库 MySQL
1、Mybatis-Plus 创建SpringBoot项目
这篇文章是关于如何创建一个SpringBoot项目,包括在`pom.xml`文件中引入依赖、在`application.yml`文件中配置数据库连接,以及加入日志功能的详细步骤和示例代码。