java各种查询

查询所有的列表

    @Test
    public void testFindAll(){
        List<User> all = userRepository.findAll();
        System.out.println(all);
    }

分页查询

    @Test
    public void testFindPage(){
        //分页参数
        int page = 1;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);
        Page<User> all = userRepository.findAll(pageable);
        System.out.println(all);
    }

修改

    @Test
    public void testUpdate() {
        //查询对象
        Optional<User> optional = userRepository.findById("5a92141cb00ffc5a448ff1a0");
        if(optional.isPresent()){
            User user = optional.get();
            //设置要修改值
            user.setName("test01");
            //...
            //修改
            User save = userRepository.save(user);
            System.out.println(save);
        }

    }

根据条件查询一条记录

    //根据Unionid查询
    @Test
    public void testfindByUnionid(){
        User user = userRepository.findByUnionid("o8-ysvwSkjQWJIWbGbcarQytJyes");
        System.out.println(user);
    }

根据条件查询多条记录

    //根据Unionid查询
    @Test
    public void testfindByOpenid(){
        List<User> userList = userRepository.findByOpenid("666");
        System.out.println(userList);
    }

条件查询+分页

    //自定义条件查询测试
    //查询openid=666
    @Test
    public void testFindAllByExample() {
        //分页参数
        int page = 0;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);

        //条件值对象
        User user= new User();
        //要查询5a751fab6abb5044e0d19ea1站点的页面
        user.setOpenid("666");
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        //定义Example
        Example<User> example = Example.of(user,exampleMatcher);
        Page<User> all = userRepository.findAll(example, pageable);
        List<User> content = all.getContent();
        System.out.println(content);
    }

条件查询(包含关键字)+分页

    /**
     * 查询openid包含6记录(包含分页) 方法1
     */
    @Test
    public void testFindAllByExample2() {
        //分页参数
        int page = 0;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);

        //条件值对象
        User user= new User();
        //设置页面别名
        user.setOpenid("6");
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        exampleMatcher = exampleMatcher.withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
        //定义Example
        Example<User> example = Example.of(user,exampleMatcher);
        Page<User> all = userRepository.findAll(example, pageable);
        List<User> content = all.getContent();
        System.out.println(content);
    }

   /**
     * 查询openid包含6记录(包含分页) 方法2
     */
    @Test
    public void testFindAllByExample3() {
        //分页参数
        int page = 0;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);

        //条件值对象
        User user= new User();
        //设置页面别名
        user.setOpenid("6");
        //条件匹配器
//        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
//        exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());
        ExampleMatcher.GenericPropertyMatchers.contains();//包含关键字
        ExampleMatcher.GenericPropertyMatchers.startsWith();//前缀匹配
        //定义Example
        Example<User> example = Example.of(user,exampleMatcher);
        Page<User> all = userRepository.findAll(example, pageable);
        List<User> content = all.getContent();
        System.out.println(content);
    }

    /**
     * 查询openid以oK前缀记录(包含分页)
     */
    @Test
    public void testFindAllByExample4() {
        //分页参数
        int page = 0;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);

        //条件值对象
        User user= new User();
        //设置页面别名
        user.setOpenid("oK");
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.startsWith());//前缀匹配
        //ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字
//        ExampleMatcher.GenericPropertyMatchers.startsWith()//前缀匹配
        //定义Example
        Example<User> example = Example.of(user,exampleMatcher);
        Page<User> all = userRepository.findAll(example, pageable);
        List<User> content = all.getContent();
        System.out.println(content);
    }

    /**
     * 查询openid包含66记录,name=11,type=1的记录(包含分页)
     */
    @Test
    public void testFindAllByExample6() {
        //分页参数
        int page = 0;//从0开始
        int size = 10;
        Pageable pageable = PageRequest.of(page,size);
        //条件值对象
        User user= new User();
        //要查询5a751fab6abb5044e0d19ea1站点的页面
        user.setOpenid("66");
        //设置模板id条件
        user.setName("11");

        //设置页面别名
        user.setType("1");
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
        //定义Example
        Example<User> example = Example.of(user,exampleMatcher);
        Page<User> all = userRepository.findAll(example, pageable);
        List<User> content = all.getContent();
        System.out.println(content);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值