首页 > 其他 > 详细

jpa使用example对象查询和分页

时间:2021-03-14 23:56:38      阅读:45      评论:0      收藏:0      [点我收藏+]

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());

    }

jpa使用example对象查询和分页

原文:https://www.cnblogs.com/HiLzd/p/14533973.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!