学习Spring-Data-Jpa(十四)---自定义Repository
有些时候,我们需要自定义Repository实现一些特殊的业务场景。
1、自定义单个Repository
1.1、首先提供一个片段接口和实现(接口的实现默认要使用Impl为后缀,实现本身不依赖spring-data,可以是常规的spring-bean,所以可以注入其他的bean,例如JdbcTemplate)。
/**
* @author caofanqi
*/
public interface StudentRepositoryCustomJdbc { List<Student> findStudentByJdbcName(String name); } /**
* 默认要以Impl结尾
* @author caofanqi
*/
public class StudentRepositoryCustomJdbcImpl implements StudentRepositoryCustomJdbc { @Resource
private JdbcTemplate jdbcTemplate; @Override
public List<Student> findStudentByJdbcName(String name) {
String sql = "SELECT * FROM cfq_jpa_student WHERE name = " + "'" + name + "'";
return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Student.class));
} }
1.2、自己的Repository继承自定义接口,就可以使用拓展的功能了。
/**
* 继承jpa的repository,和自己自定义的扩展
* @author caofanqi
*/
public interface StudentRepository extends JpaRepositoryImplementation<Student,Long>, StudentRepositoryCustomJdbc {
}
1.3、测试如下:
@BeforeEach
void setup(){
Student s1 = Student.builder().name("张三").age(23).build();
Student s2 = Student.builder().name("李四").age(24).build();
Student s3 = Student.builder().name("王五").age(25).build(); studentRepository.saveAll(Lists.newArrayList(s1,s2,s3));
} @Test
void testFindStudentByJdbcName(){
List<Student> list = studentRepository.findStudentByJdbcName("张三");
list.forEach(s -> System.out.println(s.getName()));
}
1.4、控制台打印:
Hibernate: insert into cfq_jpa_student (age, name) values (?, ?)
Hibernate: insert into cfq_jpa_student (age, name) values (?, ?)
Hibernate: insert into cfq_jpa_student (age, name) values (?, ?)
张三
1.5、自定义扩展可以有多个。自定义的优先级高于spring-data为我们提供的。
/**
* 继承jpa的repository,和自己自定义的扩展
* @author caofanqi
*/
public interface StudentRepository extends JpaRepositoryImplementation<Student,Long>, StudentRepositoryCustomJdbc,StudentRepositoryCustom<Student,Long> {
}
/**
* 自定义student功能
*
* @author caofanqi
*/
public interface StudentRepositoryCustom<T,ID> { Optional<T> findById(ID id); } /**
* 自定义实现repository功能
*
* @author caofnqi
*/
@Slf4j
public class StudentRepositoryCustomImpl<T,ID> implements StudentRepositoryCustom<T,ID> { @PersistenceContext
private EntityManager entityManager; @Override
public Optional<T> findById(ID id) {
log.info("自定义的findById");
T t = (T) entityManager.find(Student.class, id);
return Optional.of(t);
}
}

1.6、可以通过@EnableJpaRepositories的repositoryImplementationPostfix属性自定义后缀,默认是Impl。
/**
* 启动类
* @author caofanqi
*/
@SpringBootApplication
@EnableAsync
@EnableJpaRepositories(repositoryImplementationPostfix = "MyPostfix")
public class StudySpringDataJpaApplication { public static void main(String[] args) {
SpringApplication.run(StudySpringDataJpaApplication.class, args);
} }
2、自定义BaseRepository
2.1、SpringDataJpa为我们提供的代理类其实是SimpleJpaRepository。

2.2、如果我们想要对所有的Repository的保存操作都进行记录日志,我们可以自定义BaseRepository,来充当代理类。(还可以是逻辑删除等场景)
2.2.1、自定义baseRepository
/**
* 自定义base Repository
*
* @author caofanqi
*/
@Slf4j
public class MyRepositoryImpl<T,ID> extends SimpleJpaRepository<T,ID> { private final EntityManager entityManager; MyRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
} @Override
public <S extends T> S save(S entity) {
S save = super.save(entity);
log.info("保存了:{}",save);
return save;
} }
2.2.2、告知Spring-Data-Jpa使用我们自定义的baseRepository
/**
* 启动类
* @author caofanqi
*/
@SpringBootApplication
@EnableAsync
@EnableJpaRepositories(
/*queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND*/
/* ,repositoryImplementationPostfix = "MyPostfix",*/
repositoryBaseClass = MyRepositoryImpl.class)
public class StudySpringDataJpaApplication { public static void main(String[] args) {
SpringApplication.run(StudySpringDataJpaApplication.class, args);
} }
2.2.3、再次测试testSave方法,如下

3、entityManager执行原生复杂sql返回DTO
使用方法派生查询和@Query注解查询时,我们可以使用投影来面向对象编程,但是使用entityManager执行原生sql复杂sql时,怎么返回实体呢?如下:
3.1、DTO
/**
* entityManager使用的结果映射,需要一个无参构造函数与set方法,这一点与投影不一样
* @author caofanqi
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StudentAgeAndAgeCountDTO { private Integer age; private Long ageCount; }
3.2、查询方法
public List<StudentAgeAndAgeCountDTO> findCountGroupByAge(){
/*
*sql可以是更复杂的
*/
String sql = "SELECT age,count(*) AS ageCount FROM cfq_jpa_student GROUP BY age ";
Query nativeQuery = entityManager.createNativeQuery(sql);
nativeQuery.unwrap(NativeQuery.class)
//设置类型
.addScalar("age", StandardBasicTypes.INTEGER)
.addScalar("ageCount",StandardBasicTypes.LONG)
//设置返回bean
.setResultTransformer(Transformers.aliasToBean(StudentAgeAndAgeCountDTO.class));
return nativeQuery.getResultList();
}
3.3、测试及结果

源码地址:https://github.com/caofanqi/study-spring-data-jpa
学习Spring-Data-Jpa(十四)---自定义Repository的更多相关文章
- 学习Spring Data JPA
简介 Spring Data 是spring的一个子项目,在官网上是这样解释的: Spring Data 是为数据访问提供一种熟悉且一致的基于Spring的编程模型,同时仍然保留底层数据存储的特殊 ...
- Spring Data Jpa的四种查询方式
一.调用接口的方式 1.基本介绍 通过调用接口里的方法查询,需要我们自定义的接口继承Spring Data Jpa规定的接口 public interface UserDao extends JpaR ...
- Spring Data - Spring Data JPA 提供的各种Repository接口
Spring Data Jpa 最近博主越来越懒了,深知这样不行.还是决定努力奋斗,如此一来,就有了一下一波复习 演示代码都基于Spring Boot + Spring Data JPA 传送门: 博 ...
- Spring Data JPA 提供的各种Repository接口作用
各种Repository接口继承关系: Repository : public interface UserRepository extends Repository<User, Integer ...
- 学习-spring data jpa
spring data jpa对照表 Keyword Sample JPQL snippet And findByLastnameAndFirstname - where x.lastname = ? ...
- Spring Data Jpa 查询返回自定义对象
转载请注明出处:http://www.wangyongkui.com/java-jpa-query. 今天使用Jpa遇到一个问题,发现查询多个字段时返回对象不能自动转换成自定义对象.代码如下: //U ...
- 在spring data jpa中使用自定义转换器之使用枚举转换
转载请注明http://www.cnblogs.com/majianming/p/8553217.html 在项目中,经常会出现这样的情况,一个实体的字段名是枚举类型的 我们在把它存放到数据库中是需要 ...
- Spring Data JPA 教程(翻译)
写那些数据挖掘之类的博文 写的比较累了,现在翻译一下关于spring data jpa的文章,觉得轻松多了. 翻译正文: 你有木有注意到,使用Java持久化的API的数据访问代码包含了很多不必要的模式 ...
- Spring Data JPA —— 快速入门
一.概述 JPA : Java Persistence API, Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. Spring D ...
随机推荐
- JAVA 8 的新特性
Java8的新特性有:默认方法.函数式接口.lambda表达式.方法引用.Stream.Optional.日期API. 一.默认方法: 接口中的方法可以有方法体,但是必须要有static或者defau ...
- Java开发笔记(一百五十一)Druid连接池的用法
C3P0连接池自诞生以来在Java Web领域反响甚好,业已成为hibenate框架推荐的连接池.谁知人红是非多,C3P0在大型应用场合中暴露了越来越多的局限性,包括但不限于下列几点:1.C3P0管理 ...
- 用LabVIEW做声源定位系统
前一阵子,研发部举办了为期三天的第一届Innovation Day,让大家用3天时间去完成工作之外的一些创意.有人做微信小程序,有人继续研究一些工作中用到的Tool,有人把一直想解决而没时间解决的老b ...
- 华为云·寻找黑马程序员#【代码重构之路】如何“消除”if/else【华为云技术分享】
1. 背景 if/else是高级编程语言中最基础的功能,虽然 if/else 是必须的,但滥用 if/else,特别是各种大量的if/else嵌套,会对代码的可读性.可维护性造成很大伤害,对于阅读代码 ...
- 两个div并排显示,当浏览器界面缩小时会出现换行
解决:规定两个子div的父div的宽 <div id="showDataDiv" style="width: 1000px"> <div st ...
- python程序打包exe文件
第一步:安装依赖 pip install pyinstaller 第二步:移动文件 把python文件和一个exe拿来当图标的ico文件移动到同一文件中 注意:文件路径不用用中文!!!!!!!! 第三 ...
- 2019 华云数据java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.华云数据等公司offer,岗位是Java后端开发,最终选择去了华云数据. 面试了很多家公司,感觉大部分公司考察的点 ...
- 一个简单实现的string类
为了复习c++知识,简单的实现一个string类,类名为CMyString 环境说明:windows 7 64位 和 CentOS Linux release 7.6.1810 (Core) 开发工具 ...
- kubernetes第十二章--监控
- JUC - Monitor监控ThreadPoolExecutor
JUC - Monitor监控ThreadPoolExecutor 一个自定义Monitor监控ThreadPoolExecutor的执行情况 TASK WokerTask class WorkerT ...