一 点睛

Spring Data Commons提供了CrudRepository和PagingAndSortingRepository,相关代码如下:

CrudRepository的定义

//Repository的子接口CrudRepository定义了和CRUD操作相关的内容。
package org.springframework.data.repository;import java.io.Serializable;@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {//关于创建或修改的函数<S extends T> S save(S entity);         <S extends T> Iterable<S> save(Iterable<S> entities);//关于获取的函数T findOne(ID id);boolean exists(ID id);Iterable<T> findAll();Iterable<T> findAll(Iterable<ID> ids);long count();//关于删除的函数void delete(ID id);void delete(T entity);void delete(Iterable<? extends T> entities);void deleteAll();
}

PagingAndSortingRepository的定义

//定义了分页和排序操作相关的内容
package org.springframework.data.repository;import java.io.Serializable;import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {Iterable<T> findAll(Sort sort);  //排序Page<T> findAll(Pageable pageable);  //分页
}

Spring Data JPA也提供了JpaRepsitory,相关代码如下:

//继承PagingAndSortingRepository,额外提供了一些其他的API
package org.springframework.data.jpa.repository;import java.io.Serializable;
import java.util.List;import javax.persistence.EntityManager;import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {List<T> findAll();List<T> findAll(Sort sort);List<T> findAll(Iterable<ID> ids);<S extends T> List<S> save(Iterable<S> entities);void flush();<S extends T> S saveAndFlush(S entity);void deleteInBatch(Iterable<T> entities);void deleteAllInBatch();T getOne(ID id);
}

我们是否也可以做类似的封装,定义自己常用的数据库操作呢?当然可以,实现方法如下。

二 实现方法

1 自定义Repository接口

package com.wisely.support;import java.io.Serializable;import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;@NoRepositoryBean  //指明当前这个接口不是领域类的接口
//我们自定义的CustomRepository继承了JpaRepository接口,具备JPA的能力
public interface CustomRepository<T, ID extends Serializable>extends JpaRepository<T, ID> ,JpaSpecificationExecutor<T>{//要定义的数据操作方法在接口中定义Page<T> findByAuto(T example,Pageable pageable);
}

2 定义接口的实现

package com.wisely.support;import java.io.Serializable;import javax.persistence.EntityManager;import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;import static com.wisely.specs.CustomerSpecs.*;//要实现 CustomRepository接口,继承SimpleJpaRepository类让我们可以使用其提供的方法(如findAll)
public class CustomRepositoryImpl <T, ID extends Serializable>extends SimpleJpaRepository<T, ID>  implements CustomRepository<T,ID> {private final EntityManager entityManager;//数据操作方法会用到entityManager//CustomRepositoryImpl的构造函数,需当前处理的领域类和entityManager作为构造参数,在这里也给entityManager赋值了public CustomRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {super(domainClass, entityManager);this.entityManager = entityManager;}@Overridepublic Page<T> findByAuto(T example, Pageable pageable) {//定义数据访问操作,如调用findAll方法并构造一些查询条件return findAll(byAuto(entityManager, example),pageable);}}

3 自定义CustomRepositoryFactoryBean

package com.wisely.support;import java.io.Serializable;import javax.persistence.EntityManager;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
//自定义CustomRepositoryFactoryBean,继承JpaRepositoryFactoryBean
public class CustomRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>extends JpaRepositoryFactoryBean<T, S, ID> {@Override//重写createRepositoryFactory方法,用当前的CustomRepositoryFactory创建实例protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {return new CustomRepositoryFactory(entityManager);  //该类见下面的定义}//定义一个私有的静态类,并继承JpaRepositoryFactoryprivate static class CustomRepositoryFactory extends JpaRepositoryFactory {//构造函数public CustomRepositoryFactory(EntityManager entityManager) {super(entityManager);}@Override@SuppressWarnings({"unchecked"})protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {// 获得当前自定义类的实现return new CustomRepositoryImpl<T, ID>((Class<T>) information.getDomainType(), entityManager);}@Overrideprotected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {// 获得当前自定义类的类型return CustomRepositoryImpl.class;}}
}

自定义Repository的实现方法相关推荐

  1. Spring Data JPA: 实现自定义Repository

    一.前言 由于项目中的 实体(entity)默认都是继承一个父类(包含一些公共的属性,比如创建时间,修改时间,是否删除,主键id).为了实现逻辑删除,一般会自己实现RepositoryFactoryB ...

  2. Spring Data JPA 自定义Repository接口与子接口

    上篇文章介绍了 Repository接口的使用(Spring Data JPA介绍与Spring的整合),接下来重点掌握 Repository的CrudRepository子接口下的子接口. 在dao ...

  3. php自定义控件,小程序自定义组件的实现方法(代码)

    本篇文章给大家带来的内容是关于小程序自定义组件的实现方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. File:threecolgrid.js// components/t ...

  4. android sqlite自定义函数,Android中自定义一个View的方法详解

    本文实例讲述了Android中自定义一个View的方法.分享给大家供大家参考,具体如下: Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到 ...

  5. 织梦 PHP 字段 调用,织梦dede网站内容模型普通文章调用自定义图片字段的方法(和调用...

    织梦DedeCMS网站内容模型普通文章调用自定义图片字段的方法: 我们首先需要创建一个自定义函数, 找到:/include/extend.func.php ,在 function GetOneImgU ...

  6. android 自定义搜索框edittext,Android编程自定义搜索框实现方法【附demo源码下载】...

    本文实例讲述了Android编程自定义搜索框实现方法.分享给大家供大家参考,具体如下: 先来看效果图吧~ 分析:这只是模拟了一个静态数据的删除与显示 用EditText+PopupWindow+lis ...

  7. Android自定义View的实现方法,带你一步步深入了解View(四)

    不知不觉中,带你一步步深入了解View系列的文章已经写到第四篇了,回顾一下,我们一共学习了LayoutInflater的原理分析.视图的绘制流程.视图的状态及重绘等知识,算是把View中很多重要的知识 ...

  8. wamp增加php,新版PHPWAMP自定义添加PHP版本方法步骤

    新版PHPWAMP自定义添加PHP版本方法步骤 发布时间:2020-05-08 10:19:50 来源:亿速云 阅读:210 作者:三月 本文主要给大家介绍新版PHPWAMP自定义添加PHP版本方法步 ...

  9. python权重初始值设置_pytorch自定义初始化权重的方法

    在常见的pytorch代码中,我们见到的初始化方式都是调用init类对每层所有参数进行初始化.但是,有时我们有些特殊需求,比如用某一层的权重取优化其它层,或者手动指定某些权重的初始值. 核心思想就是构 ...

最新文章

  1. oracle v$system_event,45.Oracle杂记——Oracle常用动态视图v$system_event
  2. mysql导入数据比原来多_Oracle和MySQL的数据导入,差别为什么这么大
  3. 基于Qt的光盘刻录开发
  4. 基本线程同步(三)在同步的类里安排独立属性
  5. html5中 save方法,如何HTML5画布另存为窗口8 Metro应用中的图像文件?(How to save html5 c...
  6. 如何制作一个塔防游戏 Cocos2d x 2 0 4
  7. 【航模】凤凰模拟器安装
  8. TCP-面向连接的、可靠的、基于字节流的 传输层通信协议
  9. The 2019 ACM-ICPC China Shannxi Provincial Programming Contest B. Product(杜教筛+约数)
  10. hadoop is not in the sudoers file. This incident will be reported.问题解决
  11. 什么软件可以搜索python答案_什的组词
  12. 计算机考试行高怎么设置,Excel隔行调整行高的四种有效方法
  13. 2017年研究生数学建模优秀论文汇总
  14. JavaScript内存分配及垃圾回收机制
  15. ubuntu E: Unable to locate package xxxxxxx解决办法
  16. 搞“两弹一星”也离不开计算尺
  17. 【糗事】ADSL 连接时出现815错误
  18. nuke导入序列的方式
  19. <CSS3练习> CSS动画综合案例 热点图(含素材)
  20. 任何播放器无法播放视频

热门文章

  1. 使用docker 出现 Exited (1) 4 minutes ago报错如何解决
  2. 自定义FlinkSource模拟生成数据
  3. LINK : fatal error LNK1104: 无法打开文件“opencv_world400d.lib”---VS2017+OpenCV配置过程
  4. android加载h5速度慢的解决方案,WebView优化提高H5加载速度方案
  5. 应用宝发布apk问题
  6. Scenario和Scenario Outline的区别
  7. 程序员沟通障碍之普遍缺乏同理心
  8. 2021/11/11
  9. 简单对比创建对象的三种方式
  10. 微信小程序的分包功能