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是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...
随机推荐
- Jquery ajax 数据更新
$(function(){ var $personWifePs=$("#wife-money tbody tr"); var $personWife=$("#wife-m ...
- ComboBox中如何嵌套TreeView控件
在ComboBox中嵌套TreeView控件,有时候我们在设计界面的时候,由于界面设计的需要,我们需要将TreeView控件嵌套在ComboBox中,因为TreeView控件实在是太占用地方了,要 ...
- C1考试科目一知识总结
第二 交通信号 交通信号灯 机动车信号灯(红灯停,路灯走,黄灯等) 车道信号灯(绿色箭头表示该车道通行,红色箭头和红叉表示该车道禁止通行) 方向指示信号灯(红色箭头表示该方向禁止通行,绿色箭头表示该方 ...
- multi_index_container 多索引容器
multi_index_container是c++ boost库中的一个多索引的容器.因工作中用到了,特来测试试用. #include "stdafx.h" #include &q ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- day24 异常处理
程序一旦发生错误,就从错误的位置停下不在执行后面的内容一般可能预估但是无法处理的问题可以用异常处理进行操作异常处理后会继续执行后面的代码 try: # 写在try中的语句是一定执行的 ret = in ...
- 洛谷 P3225 [HNOI2012]矿场搭建 解题报告
P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...
- Elasticsearch中使用groovy脚本处理boolean字段的一个问题
Elasticsearch中使用groovy脚本获取文档的bool字段值时,得到的值是字符的 'T' 或者 'F' ,而不是bool值 true 和 false . 比如文档中有一个字段是 { &qu ...
- poj 2785(折半枚举+二分搜索)
传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...
- 窗体监听事件WindowListener
EXIT_ON_CLOSE:结束窗口所在的应用程序.在窗口被关闭的时候会退出JVM. DISPOSE_ON_CLOSE:隐藏当前窗口,并释放此窗体占有的资源.如果程序没有其他线程在运行,当所有窗口都被 ...