博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JPA Example查询使用实例
阅读量:3920 次
发布时间:2019-05-23

本文共 1369 字,大约阅读时间需要 4 分钟。

//自定义条件查询    @Test    public void test05(){        int page = 0;        int size = 5;        Pageable pageable = PageRequest.of(page, size);        //条件值对象(定义一个查询实体,给相对应的属性赋值表示要查询的条件, 对象属性为空时代表全部查询)        CmsPage cmsPage = new CmsPage();//        cmsPage.setPageId("5a754adf6abb500ad05688d9"); //要查询 PageId 为 5ada939168db524a909d30a8 的页面//        cmsPage.setSiteId("5a751fab6abb5044e0d19ea1");//        cmsPage.setTemplateId("5a962bf8b00ffc514038fafa");        cmsPage.setPageAliase("轮播");        //表示创建一个空的条件匹配器(初始化)(进行模糊查询或等其它查询配置)        ExampleMatcher exampleMatcher = ExampleMatcher.matching()                //创建匹配条件, 加入匹配条件的对应实体属性和查询规则(查询pageAliase属性包含×××的数据)               .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());//        ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字//        ExampleMatcher.GenericPropertyMatchers.startsWith() 前缀匹配//        ExampleMatcher.GenericPropertyMatchers.ignoreCase() 忽略大小写        //定义查询条件 Example
是JPA中装载查询条件的实体类 Example
example = Example.of(cmsPage, exampleMatcher); //第一个参数是查询实体的查询条件, 第二个参数是条件匹配器(模糊查询等) Page
all = cmsPageRepository.findAll(example, pageable); System.out.println(all.getContent()); }

1、配合ExampleMatcher(查询方法)Example(查询条件)和 Pageable(分页)可方便多条件查询

2、

对于非字符串属性的只能精确匹配,比如想查询在某个时间段内注册的用户信息,就不能通过Example来查询

转载地址:http://hfern.baihongyu.com/

你可能感兴趣的文章
Leetcode 67. 二进制求和
查看>>
Leetcode 69. x 的平方根
查看>>
进程、线程与协程
查看>>
Mysql中数据库引擎 INNODB
查看>>
模式1. 简单工厂模式-Java
查看>>
Leetcode 97. 交错字符串
查看>>
Leetcode 26. 删除有序数组中的重复项
查看>>
Leetcode 80. 删除有序数组中的重复项 II
查看>>
python字典赋初值
查看>>
error while loading shared libraries: lib.so.5000“ 错误的原因和解决办法
查看>>
shell 替换文件中的某个字符串
查看>>
ubuntu安装apache2
查看>>
网站实现高并发的方案
查看>>
消息队列知识储备
查看>>
初识docker
查看>>
Leetcode 621. 任务调度器
查看>>
Leetcode 312. 戳气球
查看>>
Leetcode 207. 课程表
查看>>
Mysql主从复制
查看>>
Mysql分库分表
查看>>