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

Mybatis学习之各种查询功能(五)

接口类综合代码

public interface SelectMapper {/*** 根据id查询用户信息*/User getUserById(@Param("id") Integer id);/*** 查询所有用户信息*/List<User> getAllUser();/*** 查询用户信息的总记录数*/Integer getCount();/*** 根据id查询用户信息为一个map集合*/Map<String, Object> getUserByIdToMap(Integer id);/*** 查询所有用户信息为map集合*/
//    List<Map<String, Object>> getAllUserToMap();@MapKey("id")Map<String, Object> getAllUserToMap();}

对于查询出的数据只有一条,可以通过:

  1. 实体类对象结构
  2. List集合接收
  3. Map集合接收(结果如:{id=1,username=test…})
    如果查询出的数据有多条,一定不能用实体类对象接收,会抛异常TooManyResultsException,可以通过
  4. 实体类类型的List集合接收
  5. Map类型的List集合接收
  6. 在mapper接口的方法上添加@MapKey注解

一、查询一个实体类对象

根据用户id查询用户信息

User getUserById(@Param("id") Integer id);

映射文件代码:

 <select id="getUserById" resultType="User">select * from t_user where id = #{id}</select>

测试代码:

@Testpublic void testGetUserById() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);User userById = mapper.getUserById(4);System.out.println(userById);}

二、查询一个list集合

查询所有用户信息

	List<User> getAllUser();

映射文件:

	<select id="getAllUser" resultType="User">select * from t_user</select>

测试代码:

    @Testpublic void testGetAllUser() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);List<User> allUser = mapper.getAllUser();allUser.forEach(user -> System.out.println(user));}

三、查询单个数据

查询用户的总记录数,在MyBatis中,对于Java中常用的类型都设置了类型别名,例如:

  • Java.lang.Integer -> int, integer
  • Map -> map
  • List -> list
 Integer getCount();

映射文件

 	<select id="getCount" resultType="integer">select count(*) from t_user</select>
@Testpublic void testGetCount() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);System.out.println(mapper.getCount());}

四、查询一条数据为map集合

如果没有实体类对象,就把它映射成map集合,从数据库中查询数据,将其因映射为map集合。
例如:传到网页端,就映射成json对象,所以转成map很常用。

Map<String, Object> getUserByIdToMap(Integer id);
<select id="getUserByIdToMap" resultType="map">select * from t_user where id = #{id}</select>
 @Testpublic void testGetUserByIdToMap() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);System.out.println(mapper.getUserByIdToMap(4));}

五、查询多条数据为map集合

方式一:用List接收结果

查询所有的用户信息为map集合

	List<Map<String, Object>> getAllUserToMap();
	<select id="getAllUserToMap" resultType="map">select * from t_user</select>
    @Testpublic void testGetAllUserToMap() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);List<Map<String, Object>> list =mapper.getAllUserToMap();list.stream().forEach(map->{map.forEach((key, value) -> {// 处理每个键值对的逻辑System.out.println(key + ":" + value);});});}

方式二:添加@MapKey 注解

@MapKey 是 MyBatis 提供的一个注解,用于将查询结果集转换为 Map<K, V> 形式时指定键(Key)的来源。
告诉 MyBatis 使用结果集中的哪个字段作为 Map 的键(Key),其他字段(包括该Key)会被封装为值(Value)。
常用于将多行记录按某个字段分组,转换为一个键对应多个值的 Map 结构。

常见的应用场景

  • 统计数据:例如统计每个用户的订单数量,返回 Map<用户ID, 订单数量>。
  • 分组查询:将查询结果按某个字段分组,例如 Map<部门ID, 员工列表>。
  • 数据缓存:将数据库查询结果缓存为键值对形式,方便快速查找。
	 @MapKey("id")Map<Integer, User> getAllUserToMapByMapKey();
    <select id="getAllUserToMapByMapKey" resultType="User">select * from t_user</select>
    @Testpublic void testGetAllUserToMapByMapKey() throws IOException {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);Map<Integer, User> map =mapper.getAllUserToMapByMapKey();for (Map.Entry<Integer, User> entry : map.entrySet()) {Integer id = entry.getKey();User user = entry.getValue();// 处理每个键值对的逻辑System.out.println(id + ":" + user);}}

查询的结果:

1:User{id=1, username='test', password='123456', age=23, gender='f', email='123456@qq.com'}
2:User{id=2, username='test1', password='123456', age=24, gender='f', email='123456@qq.com'}
3:User{id=3, username='test2', password='123456', age=25, gender='f', email='123456@qq.com'}
4:User{id=4, username='test3', password='123456', age=26, gender='f', email='123456@qq.com'}
5:User{id=5, username='test4', password='123456', age=27, gender='f', email='123456@qq.com'}
6:User{id=6, username='test5', password='123456', age=28, gender='f', email='123456@qq.com'}
7:User{id=7, username='test6', password='123456', age=29, gender='f', email='123456@qq.com'}
8:User{id=8, username='test7', password='123456', age=34, gender='f', email='153456@qq.com'}
9:User{id=9, username='test8', password='123456', age=34, gender='f', email='153456@qq.com'}
10:User{id=10, username='test9', password='123456', age=34, gender='f', email='153456@qq.com'}
11:User{id=11, username='Demo', password='111111', age=66, gender='m', email='1111@gmail.com'}
http://www.lryc.cn/news/608372.html

相关文章:

  • Web 开发 10
  • stm32F407 实现有感BLDC 六步换相 cubemx配置及源代码(二)
  • sqli-labs:Less-20关卡详细解析
  • 沿街晾晒识别准确率↑32%:陌讯多模态融合算法实战解析
  • Linux网络-------4.传输层协议UDP/TCP-----原理
  • QUdpSocket 详解:从协议基础、通信模式、数据传输特点、应用场景、调用方式到实战应用全面解析
  • kong网关集成Safeline WAF 插件
  • 力扣刷题日常(11-12)
  • [硬件电路-122]:模拟电路 - 信号处理电路 - 模拟电路与数字电路、各自的面临的难题对比?
  • 面试实战,问题二十二,Java JDK 17 有哪些新特性,怎么回答
  • 【0基础PS】PS工具详解--图案图章工具
  • 二叉树算法之【Z字型层序遍历】
  • ctfshow_源码压缩包泄露
  • AIGC系列:本地部署大模型
  • Rust进阶-part2-泛型
  • Flutter基础知识
  • 在线问诊系统源码解析:图文+视频双模式架构开发全攻略
  • CH32V单片机启用 FPU 速度测试
  • 江协科技STM32 13-1 PWR电源控制
  • 从零打造大语言模型--处理文本数据
  • FFmpeg+javacpp中纯音频播放
  • 互联网医院系统,互联网医院好处有哪些?
  • 音视频学习(四十八):PCM和WAV
  • CatBoost 完整解析:类别特征友好的梯度提升框架
  • 基于单片机智能雨刷器/汽车刮水器设计
  • zset 中特殊的操作
  • nodejs读写文件
  • 【redis】基于工业界技术分享的内容总结
  • C++ 模板初阶
  • 阿里云:Ubuntu系统部署宝塔