JPA Example 基本使用使用实例
一、相关接口方法

public interface QueryByExampleExecutor<T> {
<S extends T> S findOne(Example<S> example); //根据“实例”查找一个对象。
<S extends T> Iterable<S> findAll(Example<S> example); //根据“实例”查找一批对象
<S extends T> Iterable<S> findAll(Example<S> example, Sort sort); //根据“实例”查找一批对象,且排序
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable); //根据“实例”查找一批对象,且排序和分页
<S extends T> long count(Example<S> example); //根据“实例”查找,返回符合条件的对象个数
<S extends T> boolean exists(Example<S> example); //根据“实例”判断是否有符合条件的对象
}

@NoRepositoryBean public interface JpaRepository<T, ID extends Serializable>
extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
......
@Override
<S extends T> List<S> findAll(Example<S> example); //根据实例查询
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);//根据实例查询,并排序。
}

返回单一对象精准匹配:
ProductCategory productCategory = new ProductCategory(); productCategory.setCategoryId(111);
//将匹配对象封装成Example对象
Example<ProductCategory> example =Example.of(productCategory);
//根据id:111精准匹配对象,id必须是唯一主键,查出2条会报错
Optional<ProductCategory> one = repository.findOne(example);
多条件,返回集合: ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("喜欢");
//创建匹配器,即如何使用查询条件
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("categoryName",,ExampleMatcher.GenericPropertyMatchers.endsWith())//endsWith是categoryName 结尾为喜欢的数据
.withMatcher("categoryName",ExampleMatcher.GenericPropertyMatchers.startsWith()) //
.withIgnorePaths("isFace");//isFace字段不参与匹配
//创建实例
Example<ProductCategory> example =Example.of(productCategory,exampleMatcher); //查询
List<ProductCategory> one = repository.findAll(example);
System.out.println(one);
JPA Example 基本使用使用实例的更多相关文章
- JPA + SpringData 操作数据库--Helloworld实例
前言:谈起操作数据库,大致可以分为几个阶段:首先是 JDBC 阶段,初学 JDBC 可能会使用原生的 JDBC 的 API,再然后可能会使用数据库连接池,比如:c3p0.dbcp,还有一些第三方工具, ...
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- SpringBoot(四)_Spring Data JPA的使用
JPA 绝对是简化数据库操作的一大利器. 概念 首先了解 JPA 是什么? JPA(Java Persistence API)是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供 ...
- spring boot快速入门 4: jpa数据库操作 实现增删改查
spring boot jpa逆向生成表 简单实例: 第一步:pom文件: <?xml version="1.0" encoding="UTF-8"?&g ...
- 高效构建Web应用 教你玩转Play框架 http://www.anool.net/?p=577
Play 框架是一个完整的Web应用开发框架,覆盖了Web应用开发的各个方面.Play 框架在设计的时候借鉴了流行的 Ruby on Rails 和 Grails 等框架,又有自己独有的优势.使用 P ...
- play框架概述
今天是来百度实习的第四天,在项目过程中mentor说会用到play框架,当时就一个晕啊.Java还有一个叫play框架,作为一个从大三开始用java的重度javaer,居然不知道这个框架的存在,内心一 ...
- Liferay7 BPM门户开发之9: 流程表单数据动态映射体系
设计目的: 每个流程表单涉及不同的表单变量.比如请假流程有3个任务节点,分别是 Task1:开始流程,填写请假人.请假原因.请假开始时间.请假结束时间: Task2:上级审批,填写是否同意,审批意见: ...
- spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化
SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
随机推荐
- vue实现动态异步组件
https://segmentfault.com/a/1190000015080442 https://www.jianshu.com/p/40a364b5e964 <component :is ...
- python数据可视化
1.安装matplotlib 在 cmd 中键入 python -m pip install matplotlib,系统将自动安装,需要等一段时间,待完成后 python -m pip list ,显 ...
- 帝国cms中当调用当前信息不足时,继续取其他数据
<?php$sql=$empire->query("select * from table1 order by id limit 20"); $num = mysql_ ...
- 小白的python之路10/30 vim编辑器
1.vim进入命令行之后的编辑过程
- Archery:开源漏洞评估和管理工具
Archery:开源漏洞评估和管理工具
- 操作日志的设计小结by大熊
一.首先由同事的操作日志说起 同事做了一个这样的操作日志,他定义系统所有发的json加入这两个字段,module和msg,然后在service里面用注解@Log拦截,即可记录对应的操作日志. { mo ...
- tpot ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
机器学习训练的时候报出这个问题 是因为dataframe中的数据类型有一个是‘object’,把它转成int,或float 就行,如下 df['A'] = df['A‘].astype(int) 参考 ...
- 【webpack学习笔记】a02-管理资源
在webpack 中,各种资源要引入,要用到module配置,比如css/图片/字体等等. 例如: module.exports = { entry: './src/app.js', //这是入口文件 ...
- Shadow Properties之美(二)【Microsoft Entity Framework Core随笔】
接着上一篇Shadow Properties之美(一),我们来继续举一个有点啰嗦的栗子. 先看简单需求:某HR系统,需要记录员工资料.需要记录的资料有: 员工号(规则:分公司所在城市拼音首字母,加上三 ...
- python 语法
Python基础语法举例# 1 缩进和空格表示代码块# 2()连接多行print("()连接多行")str = ("uhfjfjkfj" " ...