23. Spring Boot JPA BaseDao 配置 文章
参考文献:(早期JPA版本的描述)
https://blog.csdn.net/yingxiake/article/details/51017797
https://www.jianshu.com/p/73f48095a7bf
https://www.cnblogs.com/ityouknow/p/5891443.html
https://www.jianshu.com/p/da4f584d6e14
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
spring:
datasource:
jpa:
show-sql: true #控制台显示SQL
hibernate:
ddl-auto: update #更新或创建表
package com.example.jdbc.model; import javax.persistence.*; @Entity
public class User { @Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id; @Column
private String name; @Column
private Integer age; @Column
private Integer sex; get() set()......
}
package com.example.jdbc.dao; import com.example.jdbc.model.User;
import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<User,Integer> { }
自己拓展JPA,不光定义接口,还要有实现类和自定义的方法(名称随意起)
package com.example.jdbc.dao; /**
* Created with IDEA
* author:Guchunchao
* Date:2018/12/3
* Time:下午6:02
*/ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable;
import java.util.List; @NoRepositoryBean
public interface BaseDao<T,ID extends Serializable> extends JpaRepository<T,ID>, JpaSpecificationExecutor<T> { public List<Object[]> selectBySQL(String sql); }
package com.example.jdbc.dao; /**
* Created with IDEA
* author:Guchunchao
* Date:2018/12/3
* Time:下午6:03
*/
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaEntityInformationSupport;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.List; /**
* 解决方法
* This is a bug in IntelliJ that is fixed in 13.1. http://youtrack.jetbrains.com/issue/IDEA-120977
* Class doesn't contain matching constructor for autowiring
*/
public class BaseDaoImpl<T, ID extends Serializable> extends SimpleJpaRepository<T,ID>
implements BaseDao<T,ID> { private EntityManager entityManager; public BaseDaoImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
} //父类没有不带参数的构造方法,这里手动构造父类
public BaseDaoImpl(Class<T> domainClass, EntityManager entityManager) {
this(JpaEntityInformationSupport.getEntityInformation(domainClass, entityManager), entityManager);
this.entityManager = entityManager;
} //通过EntityManager来完成自定义的查询
@Override
public List<Object[]> selectBySQL(String sql) {
return entityManager.createNativeQuery(sql).getResultList();
}
}
package com.example.jdbc.dao; 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.JpaRepositoryImplementation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport; import javax.persistence.EntityManager;
import java.io.Serializable; public class BaseDaoFactoryBean<R extends JpaRepository<T, ID>, T,
ID extends Serializable> extends JpaRepositoryFactoryBean<R, T, ID> { public BaseDaoFactoryBean(Class<? extends R> repositoryInterface) {
super(repositoryInterface);
} @Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
return new BaseDaoFactory(em);
} //创建一个内部类,该类不用在外部访问
private static class BaseDaoFactory<T, I extends Serializable>
extends JpaRepositoryFactory { private final EntityManager em; public BaseDaoFactory(EntityManager em) {
super(em);
this.em = em;
} //此方法以经在父类方法中实现了,所以不用再自定义了
// @Override
// protected JpaRepositoryImplementation getTargetRepository(RepositoryInformation information) {
// return super(information, em);
// } //设置具体的实现类的class
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseDaoImpl.class;
}
} }
package com.example.jdbc; import com.example.jdbc.dao.BaseDaoFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableJpaRepositories(repositoryFactoryBeanClass = BaseDaoFactoryBean.class, basePackages ="com.example.jdbc.dao")
@EnableTransactionManagement
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringBootJdbcApplication { public static void main(String[] args) {
SpringApplication.run(SpringBootJdbcApplication.class, args);
}
}
注意:这是SpringBoot 2.1.1版本集成的JPA的实现,与上面的引用文章内容有出入。早起版本请参见引用文章
23. Spring Boot JPA BaseDao 配置 文章的更多相关文章
- Spring Boot JPA 连接数据库
本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Spring Boot(五):Spring Boot Jpa 的使用
在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...
- (转)Spring Boot(五):Spring Boot Jpa 的使用
http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...
- Spring Boot多数据源配置(二)MongoDB
在Spring Boot多数据源配置(一)durid.mysql.jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源.本篇文章讲一下Spring Boot如何配置mongoDB多 ...
- Spring Boot Jpa 表名小写转大写
今天在使用SpringBoot整合Hibernate后创建表,表名为小写,而在linux下,mysql的表名是区分大小写的,因此在我的数据表中,就出现了两个一样的表 act_id_user 和 AC ...
- 自定义spring boot的自动配置
文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...
随机推荐
- 关于Laravel中使用response()方法调用json()返回数据unicode编码转换的问题解决
在网上找了好久没有找到,之后一步一步测试,发现了Laravel还是很强大的,解决方案如下: public function response(){ // 返回json数据 $data = [ 'err ...
- apache自带压力测试工具ab的使用及解析
当你搭建了apache服务器并在上面部署了web网站,在网站运行前,为了使apache服务器的性能得到更好的应用,我们可以先对其进行压力测试.进行压力测试其实非常简单,我们也不用再额外下载安装什么测试 ...
- python学习笔记三——控制语句
2.5 运算符与表达式 2.5.1 算术运算符和算术表达式 算术运算符包括四则运算符.求模运算符和求幂运算符. 算术运算符 加减乘除:+ - * / 表达式:x+y x-y x*y x/y ...
- Visual Studio 2017各版本安装包离线下载、安装全解析
关于Visual Studio 2017各版本安装包离线下载.更新和安装的方法以及通过已下载版本减少下载量的办法 微软最近发布了正式版Visual Studio 2017并公开了其下载方式,不过由于V ...
- JUC-Condition线程通信
1,Codition接口描述了可能会与锁有关联的条件变量.这些变量在用法上与使用Object.wait访问的隐式监视器类似. 但提供了更强大的功能,需要指出的是,单个lock可能与多个conditio ...
- ImportError: cannot import name descriptor_pb2
重新编译protobuf 下载地址:https://github.com/google/protobuf $cd /path/protobuf/python $python setup.py buil ...
- semantic segmentation with deeplearning
a 2017 guide to semantic segmentation with deep learning paper: http://blog.qure.ai/notes/semantic-s ...
- ceph 安装ceph问题汇总
1.在不同节点安装ceph时,出现以下异常: 参考这里 ceph deploy RuntimeError: NoSectionError: No section: 'ceph' 解决方法: 在报错的机 ...
- linux 运维常用的一些命令收集
1.删除0字节文件find -type f -size 0 -exec rm -rf {} ; 2.查看进程按内存从大到小排列ps -e -o “%C : %p : %z : %a”|sort ...
- BZOJ3524[Poi2014]Couriers——主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...