在我们编写代码的过程中,spring data jpa为我们的持久层提供的极大的方便,但有时spring data jpa提供的repository并不能完全满足我们开发的需求,因此就需要进行扩展。spring data jpa的repository的扩展可以对对单个repository的扩展,也可以对全局的扩展,本博客考虑的是对全局的扩展。 对单个repository的扩展可以考虑看这篇文章。
思路:(大致参考spring data jpa的官方文档,链接如下:http://docs.spring.io/spring-data/jpa/docs/1.11.1.RELEASE/reference/html/#repositories.custom-behaviour-for-all-repositories)
1、自己写一个接口继承spring data jpa 提供的Repository接口
2、编写一个自己的接口的实现类并继承SimpleJpaRepository接口
3、使用@EnableJpaRepositories注解的repositoryBaseClass属性指定我们上一步的实现类
注意事项:
1、自定义的接口上需要加上@NoRepositoryBean注解,这个注解表示我们自定义的这个接口不让spring 帮我们自动创建代理类。(为什么需要加这个注解,可以看这个。)
2、我自定义的接口中返回的是Map,这个依赖jpa的底层是hibernate的实现,如果是别的实现代码需要修改。
3、代码采用的技术是spring boot + spring data jpa
代码:
1、引入pom依赖(引入spring io,这个可以帮我们管理好jar的版本,防止jar包冲突)
<dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Brussels-SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
2、编写自定义的Repository接口(接口上需要加上@NoRepositoryBean这个注解)
/** * <pre> * sql repository用于执行sql语句 * @NoRepositoryBean 表示spring data jpa不为这个注解标注的接口创建实现了类 * </pre> * * @描述 * @作者 huan * @时间 2017年5月21日 - 下午7:26:01 */ @NoRepositoryBean public interface SqlRepository<T, ID extends Serializable> extends JpaRepository<T, Serializable> { Map<String, Object> executeDynamicSelectOneSql(String sql); }
3、编写接口的实现(返回Map使用了hibernate的一个特性)
/** * sql repository的基础实现 * * @描述 * @作者 huan * @时间 2017年5月21日 - 下午7:27:24 */ public class SqlRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, Serializable> implements SqlRepository<T, Serializable> { private EntityManager entityManager; public SqlRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); this.entityManager = entityManager; } @Override @SuppressWarnings("unchecked") public Map<String, Object> executeDynamicSelectOneSql(String sql) { Query query = entityManager.createNativeQuery(sql); return (Map<String, Object>) query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult(); } }
4、在spring boot的启动类上加上@EnableJpaRepositories注解,启用我们自己写的Repository
@SpringBootApplication @EnableJpaRepositories(repositoryBaseClass = SqlRepositoryImpl.class) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5、application.yml中jpa的配置
server: port: 80 context-path: /bs spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/huan?useUnicode=true&characterEncoding=utf-8 username: root password: root jpa: generate-ddl: true # show-sql: true properties: hibernate.format_sql : true
6、测试 (这个SqlRepository中的泛型Book是我的一个实体类,在这个例子中没有影响)
相关推荐
'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观...
第一章:Spring Data JPA入门 包括:是什么、能干什么、有什么、HelloWorld等 第二章:JpaRepository基本功能 包括:代码示例JpaRepository提供的CRUD功能,还有翻页、排序等功能 第三章:JpaRepository的查询 ...
SpringDataJpa开发--继承JpaRepository实现简单条件查询示例代码
NULL 博文链接:https://forlan.iteye.com/blog/2413050
主要介绍了Spring Data Jpa实现自定义repository转DTO,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1. 尚硅谷_SpringData_概述 ...9. 尚硅谷_SpringData_JpaRepository接口 10. 尚硅谷_SpringData_JpaSpecificationExecutor接口 11. 尚硅谷_SpringData_自定义Repository 方法 12. 尚硅谷_SpringData_源代码和PPT
本次课程以SpringData为中心,重点讲解了其JPA组件,扩展讲解了redis,mongDB,ES组件,并且对部分组件做了必要的源码分析。而且在课程的最后部分加入了一个综合案例,可以将前面章节所学知识点应用到一个项目中,帮助...
此项目介绍了Spring Data JPA的使用,包含3个自定义Repository的实现示例
0. SpringData_源代码和PPT · 1. SpringData_概述 · 2. SpringData_HelloWorld · 3... SpringData_JpaRepository接口 · 10. SpringData_JpaSpecificationExecutor接口 · 11. SpringData_自定义Repository 方法
创建一个Repository接口,继承自JpaRepository或者PagingAndSortingRepository,这样就可以使用框架提供的一些默认实现方法,例如findAll、save等。同时,也可以定义一些自定义的查询方法,例如findAllByName(String...
Spring Data Repository的核心接口是Repository(好像也没什么好惊讶的)。这个接口需要领域类(Domain Class)跟领域类的ID类型作为参数。这个接口主要是让你能知道继承这个类的接 口的类型。CrudRepository提供了对被...
弹簧动力jpa Spring Dynamic JPA将使使用JpaRepository轻松实现动态查询变得容易。如何使用? 添加依赖implementation ' ...
Spring Data JPA对象依赖关系图【svg】 根据关系图更容易阅读源码,学习其实现原理。便于后期应用。
JPA的使用 关于基于Hibernate的JPA
Springboot + Spring MVC大大简化了Web应用的RESTful...Spring Data REST支持Spring Data JPA、Spring Data MongoDB、Spring Data Neo4j、Spring Data GenFire、Spring Data Cassandra,这里选择大家比较熟悉的JPA。
实现JPA基本数据库操作功能封装 实现基于ASM9,动态生成entity、repository、service、serviceImpl、controller相关.class 可根据库表,一键生成新增、修改删除、查询等接口 实现部分基于mybatis-plus,动态代码生成...
1. The Spring Data Project 2. Repositories: Convenient Data Access Layers 3. Type-Safe Querying Using Querydsl . . Part II. Relational Databases 4. JPA Repositories 5. Type-Safe JDBC Programming with ...
Springboot中使用JPA操作数据库,本资源对如何在Springboot中使用Spring-data-jpa对数据库进行操作进行详细举例。
这是一个简单的 Web 应用程序,展示了如何配置 spring-mvc 和 spring-data。 有一个简单的登录功能,如果用户凭据正确,用户将被转发到一个主页,其中所有订单的列表显示在 html 表中。 数据库很简单:用户和订单,...