当前位置: 首页 > news >正文

Mybatis接受查询结果的情况

Mybatis接受查询结果的情况

1.查询结果为单条数据时

1)通过实体类对象接受

        这种方式最直观,当查询结果只有一条记录时使用。在 mapper 接口中定义一个方法,并指定返回类型为对应的实体类类型。mapper.xml 文件中的 <select> 标签使用 resultType 属性来指定结果类型,这里的 resultType 应该是实体类的全限定名。

1-mapper
/*** 单个返回结果* 实体接受查询结果*/
User queryReciverByEnty(@Param("id") Integer id);
2-mapper.xml
<!--/*** 单个返回结果* 实体接受查询结果*/-->
<select id="queryReciverByEnty" resultType="org.xiji.enty.User">select * from user where id=#{id}
</select>
3-test
@Test
public void queryUserInfoByMap(){System.out.println("通过Enty接受返回结果");User user = queryMapper.queryReciverByEnty(1);System.out.println(user.toString());}
4-测试结果

        

2)通过Map

        使用 Map 来接受查询结果通常用于处理那些字段名与实体类属性名不一致的情况。@MapKey 注解用于指定 Map 的键,通常是数据库表中的唯一标识字段(如主键)。注意,在这种情况下,resultType 依然会被设置为 "map"。

1-mapper
/*** 单个返回结果* map接受查询结果*/
@MapKey("id")
Map<String,Object> queryReciverByMap(@Param("id") Integer id);
2-mapper.xml
<!--通过map接受查询结果-->
<select id="queryReciverByMap" resultType="map">select * from user where id=#{id}
</select>
3-test
/*** 通过实体接受查询单个结果*/
@Test
public void queryUserInfoByMap(){System.out.println("通过Map接受返回结果");Map<String, Object> stringObjectMap = queryMapper.queryReciverByMap(1);System.out.println(stringObjectMap.toString());}
4-测试结果

        

3)通过List集合

        虽然理论上可以这样做,但实际上如果查询结果是单条记录的话,使用 List 返回一个元素的列表并不常见,也不推荐。但在某些情况下,比如需要统一处理单条或多条记录的情况,可能会用到这种方法。

1-mapper
/*** 单个返回结果* 通过List集合接受返回结果*/
List<User> queryReciverByList(@Param("id") Integer id);
2-mapper.xml
<!--通过List<User>接受查询结果-->
<select id="queryReciverByList" resultType="org.xiji.enty.User">select * from user where id=#{id}
</select>
3-test
/*** 通过List集合接受单个查询结果*/
@Test
public void queryUserInfoByList(){System.out.println("通过List集合接受查询结果");List<User> users = queryMapper.queryReciverByList(1);System.out.println(users.toString());
}

4-测试结果

        

2.查询数据为多条数据时

1)可以通过实体类型的list集合接受

        这是处理多条记录最常见的方法之一。mapper 方法返回一个实体类类型的 List,而 mapper.xml 中的 resultType 设置为实体类的全限定名。

1-mapper
/*** 通过list实体集合接受返回结果*/
List<User> queryReciverByListEnty();
2-mapper.xml
<!--通过List<Enty> 接受查询结果-->
<select id="queryReciverByListEnty" resultType="org.xiji.enty.User">select * from user
</select>
3-test
/*** 通过List实体集合接受多个返回结果*/
@Test
public void queryUserInfoByListUser(){System.out.println("通过List实体集合接受多个返回结果");List<User> users = queryMapper.queryReciverByListEnty();for (int i = 0; i < users.size(); i++) {}System.out.println(users.toString());
}

4-测试结果

        

2)可以通过@MapKey注解+map的list集合接受

        这种方法允许你返回一个包含多个 Map 对象的 List,每个 Map 对应一条记录。如果希望以某个字段(如主键)作为 Map 键的话,可以在 mapper 接口的方法上加上 @MapKey 注解来指定键字段。

1-mapper
/*** 通过List Map集合接受返回结果*/
@MapKey("id")
List<Map<String,Object>> queryReciverByListMap();
2-mapper.xml
<!--通过List<Map>接受查询结果-->
<select id="queryReciverByListMap" resultType="map">select * from user
</select>
3-test
/*** 通过ListMap接受多个返回结果*/
@Test
public void queryUserInfoByListMap(){System.out.println("通过ListMap接受多个返回结果");List<Map<String, Object>> maps = queryMapper.queryReciverByListMap();for (Map<String, Object> map : maps) {System.out.println(map.toString());}
}
4-测试结果

        

3)可以通过@MapKey注解+map,选择表中唯一表示作为主键

        这种方法适用于当你希望将多条记录按照唯一的标识字段(如主键)来存储的情况。这里 mapper 接口方法返回的是一个 Map,其键是由 @MapKey 指定的字段值,而值则是整个记录的映射。

1-mapper
/*** 通过Map接受返回结果*/
@MapKey("id")
Map<String,Object> queryReciverByMapList();
2-mapper.xml
<!--通过map集合 接受查询结果-->
<select id="queryReciverByMapList" resultType="map">select * from user
</select>
3-test
/*** 通过@MapKey注解接受多个结果*/
@Test
public void queryUserInfoByMapKey(){System.out.println("通过@MapKey注解接受多个结果");Map<String, Object> stringObjectMap = queryMapper.queryReciverByMapList();System.out.println(stringObjectMap.toString());
}
4-测试结果

        

附:

1)QueryMapper代码

package org.xiji.mapper;import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.xiji.enty.User;import java.util.List;
import java.util.Map;@Mapper
public interface QueryMapper {/*** 单个返回结果* 实体接受查询结果*/User queryReciverByEnty(@Param("id") Integer id);/*** 单个返回结果* map接受查询结果*/@MapKey("id")Map<String,Object> queryReciverByMap(@Param("id") Integer id);/*** 单个返回结果* 通过List集合接受返回结果*/List<User> queryReciverByList(@Param("id") Integer id);/*** 通过list实体集合接受返回结果*/List<User> queryReciverByListEnty();/*** 通过List Map集合接受返回结果*/@MapKey("id")List<Map<String,Object>> queryReciverByListMap();/*** 通过Map接受返回结果*/@MapKey("id")Map<String,Object> queryReciverByMapList();}

2)QueryMapper.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.xiji.mapper.QueryMapper"><!--查询结果的情况--><!--/*** 单个返回结果* 实体接受查询结果*/--><select id="queryReciverByEnty" resultType="org.xiji.enty.User">select * from user where id=#{id}</select><!--通过map接受查询结果--><select id="queryReciverByMap" resultType="map">select * from user where id=#{id}</select><!--通过List<User>接受查询结果--><select id="queryReciverByList" resultType="org.xiji.enty.User">select * from user where id=#{id}</select><!--多个查询结果--><!--通过List<Enty> 接受查询结果--><select id="queryReciverByListEnty" resultType="org.xiji.enty.User">select * from user</select><!--通过List<Map>接受查询结果--><select id="queryReciverByListMap" resultType="map">select * from user</select><!--通过map集合 接受查询结果--><select id="queryReciverByMapList" resultType="map">select * from user</select></mapper>

3)QueryMapTest代码

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.xiji.enty.User;
import org.xiji.mapper.QueryMapper;import java.util.List;
import java.util.Map;@SpringJUnitConfig(locations = {"classpath:springConfig.xml"})
public class QueryMapperTest {@Autowiredprivate QueryMapper queryMapper;@Testpublic void queryUserInfoByMap(){System.out.println("通过Map接受返回结果");Map<String, Object> stringObjectMap = queryMapper.queryReciverByMap(1);System.out.println(stringObjectMap.toString());}/*** 通过实体接受查询单个结果*/@Testpublic void queryUserInfoByUser(){System.out.println("通过实体接受参数");User user = queryMapper.queryReciverByEnty(1);System.out.println(user.toString());}/*** 通过List集合接受单个查询结果*/@Testpublic void queryUserInfoByList(){System.out.println("通过List集合接受查询结果");List<User> users = queryMapper.queryReciverByList(1);System.out.println(users.toString());}/*** 通过List实体集合接受多个返回结果*/@Testpublic void queryUserInfoByListUser(){System.out.println("通过List实体集合接受多个返回结果");List<User> users = queryMapper.queryReciverByListEnty();for (int i = 0; i < users.size(); i++) {}System.out.println(users.toString());}/*** 通过ListMap接受多个返回结果*/@Testpublic void queryUserInfoByListMap(){System.out.println("通过ListMap接受多个返回结果");List<Map<String, Object>> maps = queryMapper.queryReciverByListMap();for (Map<String, Object> map : maps) {System.out.println(map.toString());}}/*** 通过@MapKey注解接受多个结果*/@Testpublic void queryUserInfoByMapKey(){System.out.println("通过@MapKey注解接受多个结果");Map<String, Object> stringObjectMap = queryMapper.queryReciverByMapList();System.out.println(stringObjectMap.toString());}
}


http://www.mrgr.cn/news/27034.html

相关文章:

  • 闯入清洁家电“诸神之战”的萤石,凭什么立足?
  • 最强AI照片说话Windows一体包下载地址,口型合成音频驱动图片,免安装,下载即用
  • Docker 安装配置和基本命令详解以及案例示范
  • UART 16550的使用
  • 【25.3】C++智能交友系统
  • 【C++知识扫盲】-----初识迭代器
  • 蔚来充换电站数字化运维系统案例分享
  • Linux环境
  • [Linux入门]---使用exec函数实现简易shell
  • Python基础语法(3)上
  • MySQL 表的增删改查
  • 多模态学习
  • 概述03 A/B test
  • 从关键新闻和最新技术看AI行业发展(第三十一期2024.8.26-9.8) |【WeThinkIn老实人报】
  • Linux——进程状态
  • Linux内核编译并移植至ARM平台
  • 跨平台互斥体封装
  • 数据结构:单链表
  • 前端必知必会-响应式网页设计Viewport和GridView
  • 家庭理财管理系统