1、查询:
查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射关系
resultType:自动映射,用于属性名和表中字段名一致的情况
resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况
- User getUserById(@Param("id") Integer id);
-
- //查询方法
- select * from user where id = #{id};
-
- //测试类
- @Test
- public void testGetUserById(){
- User user = mapper.getUserById(7);
- System.out.println(user);
- }
当查询的数据为多条记录时,不能使用实体类作为返回值,否则会抛出异常:TooManyResultsException,但是如果查询的数据只有一条,可以使用实体类或集合作为返回值
- /**
- * 查询所有用户信息
- * @return
- */
- List
getAllUser(); -
- select * from user;
-
- @Test
- public void testGetAllUser(){
- List
users = mapper.getAllUser(); - System.out.println(users);
- System.out.println("--------------------------------");
- for(User user:users){
- System.out.println(user);
- }
- System.out.println("--------------------------------");
- users.forEach(System.out::println);
- }
- //法1 增强for循环
- for(User user:users){
- System.out.println(user);
- }
-
- //法2 forEach
- users.forEach(System.out::println);
-
- //法3
- //利用lambda表达式遍历users
- users.forEach(user -> System.out.println(user));
-
- //法4
- for(int i=0;i< users.size();i++){
- System.out.println(users.get(i));
- }
- /**
- * 查询用户的总数量
- * @return
- */
- Integer getCount();
-
- select count(*) from user;
-
- @Test
- public void testGetCount(){
- Integer countusers = mapper.getCount();
- System.out.println("用户总数:"+countusers);
- }
- /**
- * 根据id查询用户信息为Map集合
- * @param id
- * @return
- */
- Map
getUserByIdToMap(@Param("id") Integer id); -
- select * from user where id = #{id};
-
- @Test
- public void testGetUserByIdToMap(){
- Map
map = mapper.getUserByIdToMap(6); - System.out.println(map);
- }
将表中的数据以map集合的方式查询,一条数据对应一个map;如果有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取
错误写法:
- Map
getAllUserToMap(); - select * from user
-
- @Test
- public void testGetAllUserToMap(){
- Map
map = mapper.getAllUserToMap(); - System.out.println(map);
- }
-
- 报错:TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 10
该方法只能有一条数据结果

正确写法:
- /**
- * 查询所有用户信息为Map集合,采用将map数据放入list集中的方式
- * @return
- */
- List
-
- select * from user
-
- public void testGetAllUserToMap(){
- List
- System.out.println(list);
- }
-
- 结果:
- [{password=123, sex=男, id=1, age=12, email=123@qq.com, username=admin},
- {password=123, sex=男, id=2, age=12, email=123@qq.com, username=admin},
- {password=456, sex=男, id=5, age=12, email=123@qq.com, username=qqq}]

如果有多条数据,可以将每条数据转换的map集合放在一个大的map集合中,但是必须要通过@MapKey注解来完成。将查询的某个字段的值作为大的Map集合的键(key)
- /**
- * 查询所有用户信息为Map集合,采用注解的方式
- * @return
- */
- @MapKey("id")
- Map
getAllUserToMap2(); -
- select * from user
-
- @Test
- public void testGetAllUserToMap2(){
- Map
map = mapper.getAllUserToMap2(); - System.out.println(map);
- }
- 结果:()前边会写对应的key
- {1={password=123, sex=男, id=1, age=12, email=123@qq.com, username=admin},
- 2={password=123, sex=男, id=2, age=12, email=123@qq.com, username=admin},
- 5={password=456, sex=男, id=5, age=12, email=123@qq.com, username=qqq}}

当查询的key值为age时

返回结果为

因为age所给值都是相同的,而key是唯一的,一个个查询时后一个会将前一个给覆盖,所以最后显示的只有最后一条(真正条数是对的,但是显示只有一个,因为其他都被覆盖了)
如果按照username查询,其结果会按照首字母排序,不同于数据库的顺序