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();}
对于查询出的数据只有一条,可以通过:
- 实体类对象结构
- List集合接收
- Map集合接收(结果如:{id=1,username=test…})
如果查询出的数据有多条,一定不能用实体类对象接收,会抛异常TooManyResultsException,可以通过 - 实体类类型的List集合接收
- Map类型的List集合接收
- 在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'}