Spring Dtata JPA - 图文
更新时间:2023-10-19 07:15:01 阅读量: 综合文库 文档下载
- spring推荐度:
- 相关推荐
Spring Data JPA
最近项目中使用了Spring Data JPA这套基于持久化层的一套查询规范( 即基于ORM和JPA )。今天自己整理一下这套“框架”的使用说明
JPA:JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
使用Spring Data Jpa要引入相应的jar 文件。使用此规范只要实现几个重要的接口即可,首先看下这几个接口的关系
那了解了接口之后该如何使用呢:
public interface JPATests extends JpaRepository
//如上面代码: jpaTests 是我自己创建的一个接口,该接口继承了
JpaRepository
那这个接口都实现了哪些方法呢?你可以去Spring Data Jpa的源码中看,该接口有个实现里面就是它方法的实现逻辑算法: 下面我贴出代码:
@Transactional(readOnly = true) public class SimpleJpaRepository
JpaSpecificationExecutor
private final JpaEntityInformation
private final PersistenceProvider provider;
private LockMetadataProvider lockMetadataProvider;
/** * Creates a new {@link SimpleJpaRepository} to manage objects of the given {@link JpaEntityInformation}. *
* @param entityInformation must not be {@literal null}. * @param entityManager must not be {@literal null}. */
public SimpleJpaRepository(JpaEntityInformation
Assert.notNull(entityInformation); Assert.notNull(entityManager);
this.entityInformation = entityInformation; this.em = entityManager; this.provider =
PersistenceProvider.fromEntityManager(entityManager); }
/** * Creates a new {@link SimpleJpaRepository} to manage objects of the given domain type. *
* @param domainClass must not be {@literal null}. * @param em must not be {@literal null}.
*/
public SimpleJpaRepository(Class
this(JpaEntityInformationSupport.getMetadata(domainClass, em), em); }
/**
* Configures a custom {@link LockMetadataProvider} to be used to detect {@link LockModeType}s to be applied to * queries. *
* @param lockMetadataProvider */
public void setLockMetadataProvider(LockMetadataProvider lockMetadataProvider) {
this.lockMetadataProvider = lockMetadataProvider; }
private Class
return entityInformation.getJavaType(); }
private String getDeleteAllQueryString() {
return getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()); }
private String getCountQueryString() {
String countQuery = String.format(COUNT_QUERY_STRING, provider.getCountQueryPlaceholder(), \); return getQueryString(countQuery, entityInformation.getEntityName()); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#delete(java.io.Serializable) */
@Transactional
public void delete(ID id) {
Assert.notNull(id, \);
if (!exists(id)) { throw new
EmptyResultDataAccessException(String.format(\exists!\,
entityInformation.getJavaType(), id), 1); }
delete(findOne(id)); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#delete(java.lang.Object) */
@Transactional
public void delete(T entity) {
Assert.notNull(entity, \);
em.remove(em.contains(entity) ? entity : em.merge(entity)); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable) */
@Transactional
public void delete(Iterable extends T> entities) {
Assert.notNull(entities, \null!\);
for (T entity : entities) { delete(entity); } }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#deleteInBatch(java.lang.Iterable) */
@Transactional
public void deleteInBatch(Iterable
Assert.notNull(entities, \null!\);
if (!entities.iterator().hasNext()) { return; }
applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, em) .executeUpdate(); }
/*
* (non-Javadoc) * @see org.springframework.data.repository.Repository#deleteAll() */
@Transactional
public void deleteAll() {
for (T element : findAll()) { delete(element); } }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#deleteAllInBatch() */
@Transactional
public void deleteAllInBatch() {
em.createQuery(getDeleteAllQueryString()).executeUpdate(); }
/*
* (non-Javadoc) *
* @see *
org.springframework.data.repository.Repository#readById(java.io.Serializable * ) */
public T findOne(ID id) {
Assert.notNull(id, \); return em.find(getDomainClass(), id); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#exists(java.io.Serializable) */
public boolean exists(ID id) {
Assert.notNull(id, \);
if (entityInformation.getIdAttribute() != null) {
String placeholder = provider.getCountQueryPlaceholder(); String entityName = entityInformation.getEntityName(); Iterable
QueryUtils.getExistsQueryString(entityName, placeholder, idAttributeNames);
TypedQuery
if (entityInformation.hasCompositeId()) {
for (String idAttributeName : idAttributeNames) { query.setParameter(idAttributeName,
entityInformation.getCompositeIdAttributeValue(id, idAttributeName)); } } else {
query.setParameter(idAttributeNames.iterator().next(),
id);
}
return query.getSingleResult() == 1L; } else {
return findOne(id) != null; } }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#findAll() */
public List
return getQuery(null, (Sort) null).getResultList(); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#findAll(ID[]) */
public List
return getQuery(new Specification
public Predicate toPredicate(Root
root.get(entityInformation.getIdAttribute());
return path.in(cb.parameter(Iterable.class, \)); }
}, (Sort) null).setParameter(\, ids).getResultList(); }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#findAll(org.springframework.data.domain.Sort) */
public List
return getQuery(null, sort).getResultList(); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable) */
public Page
if (null == pageable) {
return new PageImpl
return findAll(null, pageable); }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaSpecificationExecutor#findOne(org.springframework.data.jpa.domain.Specification) */
public T findOne(Specification
try {
return getQuery(spec, (Sort) null).getSingleResult(); } catch (NoResultException e) { return null; } }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaSpecificationExecutor#findAll(org.springframework.data.jpa.domain.Specification) */
public List
return getQuery(spec, (Sort) null).getResultList(); }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaSpecificationExecutor#findAll(org.springframework.data.jpa.domain.Specification, org.springframework.data.domain.Pageable) */
public Page
TypedQuery
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaSpecificationExecutor#findAll(org.springframework.data.jpa.domain.Specification, org.springframework.data.domain.Sort) */
public List
return getQuery(spec, sort).getResultList(); }
/*
* (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#count() */
public long count() {
return em.createQuery(getCountQueryString(), Long.class).getSingleResult(); }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaSpecificationExecutor#count(org.springframework.data.jpa.domain.Specification) */
public long count(Specification
return getCountQuery(spec).getSingleResult(); }
/*
* (non-Javadoc) * @see
org.springframework.data.repository.CrudRepository#save(java.lang.Object) */
@Transactional
public S save(S entity) {
if (entityInformation.isNew(entity)) { em.persist(entity); return entity; } else {
return em.merge(entity); } }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#saveAndFlush(java.lang.Object) */
@Transactional
public T saveAndFlush(T entity) {
T result = save(entity); flush();
return result; }
/*
* (non-Javadoc) * @see
org.springframework.data.jpa.repository.JpaRepository#save(java.lang.Iterable) */
@Transactional
public List save(Iterable entities) {
List result = new ArrayList();
if (entities == null) { return result;
正在阅读:
房屋租赁合同09-28
那个人让我难忘作文500字06-14
磨练意志的格言02-06
人寿保险中心支公司筹备及未来三年经营计划书 - 图文09-18
PQ分解法计算潮流04-30
Morse跌倒评分表02-03
listen_to_this_3词汇表03-08
MATLAB实验(1)11-28
- 二年级下册音乐测试题
- 浙江财经大学中微题库答案
- 小升初常考古诗填空练习(80首古诗 含答案)
- 全国导基 第十章 中国旅游诗词、楹联、游记鉴赏 练习题 及答案
- 华师大版七年级科学(生物)下册5.1《种群和群落》导学案(含答
- 人教版七年级语文上册练习:《我的老师》课时训练(附答案)-精
- NOIP2015浙江省复赛普及组成绩
- 长虹公司的应收账款管理
- 快递行业同业竞争对手调查报告
- “十三五”重点项目-牦牛骨髓粉项目节能评估报告(节能专篇)
- 钢结构生产制造部各岗位职责及任职要求
- 对H企业应收账款管理与核算现状的调查报告
- 中国化学会第24届全国高中学生化学竞赛(省级赛区)试题、标准答
- 本科成本会计
- “众包”创新模式在我国潜在的风险的探讨
- 语文基础全套复习资料(有他足够了
- 中外合作出版合同(1)
- STM32-GPIO及EXTI初始化详解
- 2018年中国控制技术市场现状调研与发展前景分析报告目录
- 大学物理试题第四章 冲量和动量
- 图文
- Spring
- Dtata
- JPA
- 哈工大应用统计学练习题
- 2012-2013第二学期VB期末考试卷(A卷)终极版
- 中考模拟2017江苏省句容市2017届九年级英语5月调研试题 - 图文
- 2017年除螨仪现状及发展趋势分析(目录)
- 数控机床课程教学大纲
- 个人精心制作新译林英语5BUNIT7单元知识点汇总及复习题(三套)
- SPSS上机练习题
- 2018朝阳中考一模语文试题及答案 - 图文
- 论文以浙江省中小民营企业国际化发展的进程
- 上海英语中级口译完全自学通过心得
- 建档立卡学生 帮扶 总结
- 计算方法上机实习题大作业(实验报告)
- 钢铁企业管理职位岗位职责说明书
- 浙价房〔1999〕411号
- PR上机实验3 - 图文
- 装维管理办法
- 四、师资队伍建设分析报告
- 素质训练九年级体育课教案
- 部编九年级语文标点符号专项训练题
- 尽职免责