• MyBatis的各种查询功能


    1、查询:

    查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射关系

    resultType:自动映射,用于属性名和表中字段名一致的情况

    resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况

    2、查询一个实体类的对象
    1. User getUserById(@Param("id") Integer id);
    2. //查询方法
    3. //测试类
    4. @Test
    5. public void testGetUserById(){
    6. User user = mapper.getUserById(7);
    7. System.out.println(user);
    8. }
    3、查询一个list集合

    当查询的数据为多条记录时,不能使用实体类作为返回值,否则会抛出异常:TooManyResultsException,但是如果查询的数据只有一条,可以使用实体类或集合作为返回值

    1. /**
    2. * 查询所有用户信息
    3. * @return
    4. */
    5. List getAllUser();
    6. @Test
    7. public void testGetAllUser(){
    8. List users = mapper.getAllUser();
    9. System.out.println(users);
    10. System.out.println("--------------------------------");
    11. for(User user:users){
    12. System.out.println(user);
    13. }
    14. System.out.println("--------------------------------");
    15. users.forEach(System.out::println);
    16. }
    遍历User集合的几种方法
    1. //法1 增强for循环
    2. for(User user:users){
    3. System.out.println(user);
    4. }
    5. //法2 forEach
    6. users.forEach(System.out::println);
    7. //法3
    8. //利用lambda表达式遍历users
    9. users.forEach(user -> System.out.println(user));
    10. //法4
    11. for(int i=0;i< users.size();i++){
    12. System.out.println(users.get(i));
    13. }
    4、查询单个数据
    1. /**
    2. * 查询用户的总数量
    3. * @return
    4. */
    5. Integer getCount();
    6. @Test
    7. public void testGetCount(){
    8. Integer countusers = mapper.getCount();
    9. System.out.println("用户总数:"+countusers);
    10. }

    5、查询一条数据为Map集合
    1. /**
    2. * 根据id查询用户信息为Map集合
    3. * @param id
    4. * @return
    5. */
    6. Map getUserByIdToMap(@Param("id") Integer id);
    7. @Test
    8. public void testGetUserByIdToMap(){
    9. Map map = mapper.getUserByIdToMap(6);
    10. System.out.println(map);
    11. }
    6、查询多条数据为Map集合
    ⑴方式一

    将表中的数据以map集合的方式查询,一条数据对应一个map;如果有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取

    错误写法

    1. Map getAllUserToMap();
    2. @Test
    3. public void testGetAllUserToMap(){
    4. Map map = mapper.getAllUserToMap();
    5. System.out.println(map);
    6. }
    7. 报错:TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 10

     该方法只能有一条数据结果

    正确写法:

    1. /**
    2. * 查询所有用户信息为Map集合,采用将map数据放入list集中的方式
    3. * @return
    4. */
    5. List> getAllUserToMap();
    6. public void testGetAllUserToMap(){
    7. List> list = mapper.getAllUserToMap();
    8. System.out.println(list);
    9. }
    10. 结果:
    11. [{password=123, sex=男, id=1, age=12, email=123@qq.com, username=admin},
    12. {password=123, sex=男, id=2, age=12, email=123@qq.com, username=admin},
    13. {password=456, sex=男, id=5, age=12, email=123@qq.com, username=qqq}]

    ⑵方式二

    如果有多条数据,可以将每条数据转换的map集合放在一个大的map集合中,但是必须要通过@MapKey注解来完成。将查询的某个字段的值作为大的Map集合的键(key)

    1. /**
    2. * 查询所有用户信息为Map集合,采用注解的方式
    3. * @return
    4. */
    5. @MapKey("id")
    6. Map getAllUserToMap2();
    7. @Test
    8. public void testGetAllUserToMap2(){
    9. Map map = mapper.getAllUserToMap2();
    10. System.out.println(map);
    11. }
    12. 结果:()前边会写对应的key
    13. {1={password=123, sex=男, id=1, age=12, email=123@qq.com, username=admin},
    14. 2={password=123, sex=男, id=2, age=12, email=123@qq.com, username=admin},
    15. 5={password=456, sex=男, id=5, age=12, email=123@qq.com, username=qqq}}

    当查询的key值为age时

    返回结果为

    因为age所给值都是相同的,而key是唯一的,一个个查询时后一个会将前一个给覆盖,所以最后显示的只有最后一条(真正条数是对的,但是显示只有一个,因为其他都被覆盖了)

    如果按照username查询,其结果会按照首字母排序,不同于数据库的顺序

  • 相关阅读:
    【强化学习】03 ——马尔可夫决策过程
    【从零开始学习 SystemVerilog】2.13、SystemVerilog 数据类型—— Structure(结构体)
    Python Selenium 浏览器打印预览
    ElasticSearch深度分页详解
    .NET分布式Orleans - 9 - 贪吃蛇项目演示
    python各种编辑器、APP、软件下载
    GPT-4V:AI在医疗领域的应用
    2022年双循环行业研究报告
    PEG-siRNA-PCL|siRNA-PEG-LHRH|MPEG-siRNA 甲氧基聚乙二醇修饰核酸
    数据结构与算法:判断单链表中是否存在环
  • 原文地址:https://blog.csdn.net/2301_79659699/article/details/139426445