jpa使用example对象查询和分页

example构建对象时 ,如果有默认值 比如int类型 的 都会生成sql语句,在使用的时候要特别注意设置忽略查询的属性

ExampleMatcher 设置查询规则

案例:

   @Test
    public  void  testExampleQuery(){
        SysMenu menu= SysMenu.builder().menuName("测试").build();

        ExampleMatcher matcher=ExampleMatcher.matching()
                //设置默认的字符串查询为 like
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //设置忽略大小写 也可以设置负略的字段 withIgnoreCase(String... propertyPaths)
                .withIgnoreCase(true)
                //设置为开头模糊查询 如 : like %asas
                .withMatcher("menuName",ExampleMatcher.GenericPropertyMatchers.startsWith())
                //设置为结尾模糊模糊查询 如 : like asas%
                .withMatcher("menuIcon",ExampleMatcher.GenericPropertyMatchers.endsWith())
                //设置为全模糊 如 like %asas%
//                .withMatcher("menuName",ExampleMatcher.GenericPropertyMatchers.contains())
                // 設置查询忽略的属性
                .withIgnorePaths("orderNum","menuId","parentId");


        Example<SysMenu> example=Example.of(menu,matcher);
        //设置分页 排序
        Pageable pageable=PageRequest.of(0,1,Sort.by(Sort.Direction.DESC,"menuId"));
        Page<SysMenu> all = dao.findAll(example, pageable);
        System.out.println(all.getContent());
        System.out.println(all.getTotalPages());
        System.out.println(all.getTotalElements());

    }