MyBatis-Plus学习笔记(五) 查询操作

狂神说Java:https://www.bilibili.com/video/BV17E411N7KN?p=10学习笔记


一、查询一个用户

   @Test
    public void testSelectById(){
        User user = userMapper.selectById(2L);
        System.out.println(user);
    }

二、查询多个用户

    @Test
    public void testSelectByBatchId(){
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);
    }

三、条件查询(一)使用map操作

    @Test
    public void testSelect(){

        HashMap<String, Object> map = new HashMap<>();

        map.put("name","night");
        map.put("age",1);

        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }

四、分页查询

分页在网站广泛使用,如下途径可实现分页

  • 用原始的limit进行分页

  • 用pageHelper等第三方插件

  • Mybatis-Plus内置的分页插件

Mybatis-Plus分页插件用法:

1.配置拦截器组件(导入分页插件)

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {

        return new PaginationInterceptor();
    }

2.直接使用Page对象即可

    @Test
    public void testPage(){

        //参数一:当前页;参数二:页面大小
        Page<User> page = new Page<>(2, 3);

        userMapper.selectPage(page,null);

        page.getRecords().forEach(System.out::println);
        System.out.println(page.getTotal());
    }

猜你喜欢

转载自blog.csdn.net/qq_41694490/article/details/114273050