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;
正在阅读:
d211邮政营业员高级技师答案12-22
我为人人,人人为我04-09
读电子书的滋味作文500字07-03
电子书的现状和未来发展10-07
冰风谷攻略电子书 - 图文 12-06
关于电子书包发展推广企划方案09-13
生本作文:主体发展的精神旅程05-24
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 图文
- Spring
- Dtata
- JPA
- 哈工大应用统计学练习题
- 2012-2013第二学期VB期末考试卷(A卷)终极版
- 中考模拟2017江苏省句容市2017届九年级英语5月调研试题 - 图文
- 2017年除螨仪现状及发展趋势分析(目录)
- 数控机床课程教学大纲
- 个人精心制作新译林英语5BUNIT7单元知识点汇总及复习题(三套)
- SPSS上机练习题
- 2018朝阳中考一模语文试题及答案 - 图文
- 论文以浙江省中小民营企业国际化发展的进程
- 上海英语中级口译完全自学通过心得
- 建档立卡学生 帮扶 总结
- 计算方法上机实习题大作业(实验报告)
- 钢铁企业管理职位岗位职责说明书
- 浙价房〔1999〕411号
- PR上机实验3 - 图文
- 装维管理办法
- 四、师资队伍建设分析报告
- 素质训练九年级体育课教案
- 部编九年级语文标点符号专项训练题
- 尽职免责