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. 简介 协议 下载 源码 必要的软件 ...
随机推荐
- 获取上一页面url
console.log(window.location.host); #js获取当前域名 console.log(window.location.href); #js获取当前url console.l ...
- oracle分析性能问题实例
摘录于SAP有关分析ORACLE数据性能事件的文档. 1.A check for the distribution of relevant Oracle server time revealed: 有 ...
- 苹果笔记本安装windows正版操作系统
http://vip.dzzysm.cn/mac/ http://www.windows7en.com/Win7/20439.html windows 7之家的网友很多,其中不缺乏土豪网友购买苹果的M ...
- .net core webapi 使用ValidationAttribute对比同一对象的多个参数
众所周知,在使用DataAnnotations数据验证的时候,特别是针对同一个InputDto的多个属性进行对比的时候,例如起始日期不能大于结束日期,我们需要在Attribute中知道当前InputD ...
- idea使用错误及技巧总结合集(一)
--- Cannot start process, the working directory 'E:\algorithm\algorithm' does not exist 1.点击run后再点击e ...
- DCOMP——分散式计算
新型网络DCOMP 据国外媒体2017年11月7日报道,曾经发明互联网的秘密机构“美国国防部高级研究计划局(DARPA)”如今正致力于研发能够完胜互联网的新网络,并且启动了一项链接物联网.智能手机.智 ...
- oralce执行计划
看懂Oracle执行计划 最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing… 一:什么是Oracle执行计划? 执行计划是一条查询语句在 ...
- java的http请求实例
package vqmp.data.pull.vqmpull.common.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory ...
- Oracle数据库 Synonym和DBLink
1.1数据库创建同义词(synonym) Oracle的同义词(synonyms)从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系.它可以节省大量的数据库空间,对不同用户的操作同一张表没 ...
- Ubuntu16.04安装MongoDB的Ruby驱动
背景: 1. ruby的安装方式:sudo apt-get install ruby 2. mongod服务已开启 3. ruby版本:2.3, gem已安装 安装驱动的步骤: 1. gem inst ...