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是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...
随机推荐
- [日常工作] SUSE设置上网ip地址
1. 同事搜到的命令 ifconfig eth0 10.24.25.8 netmask 255.255.0.0 up route add default gw 10.24.255.254 2. 修改 ...
- BZOJ1022[SHOI2008]小约翰的游戏——anti-SG(反尼姆博弈)
题目描述 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到 ...
- BZOJ4699 树上的最短路(最短路径+dfs序+线段树+堆+并查集)
首先一般化的将下水道和塌陷看成一个东西.注意到在从源点出发的所有需要使用某条下水道的最短路径中,该下水道只会被使用一次,该下水道第一个被访问的点相同,且只会在第一个访问的点使用该下水道.这个第一个访问 ...
- BZOJ2282 SDOI2011消防/NOIP2007树网的核(二分答案+树形dp)
要求最大值最小容易想到二分答案.首先对每个点求出子树中与其最远的距离是多少,二分答案后就可以标记上一些必须在所选择路径中的点,并且这些点是不应存在祖先关系的.那么如果剩下的点数量>=3,显然该答 ...
- MVC 多submit
直接上代码 3种情况: 第一种,由于form已经可以支持多个了.所以我们分成2个form来提交,submit会根据自己所在的form来提交当前表单的内容 @using (Html.BeginForm( ...
- bzoj 2141 : 排队 (cdq分治+bit)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2141 思路: 其实就是求动态逆序对...cdq降维,用树状数组前后求两遍逆序对就好了 切水 ...
- Codeforces Round #431 (Div. 2) B. Tell Your World
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- day11 filter函数
场景模拟:我想判断某个列表里面的某个元素怎么怎么样 基础方法,如果需要判断多次则重复代码 ret = [] move_peole = ["alex","sb_wupeiq ...
- java监控工具VisualVM
java监控工具VisualVM https://visualvm.github.io/ https://visualvm.github.io/documentation.html https://h ...
- startSSL 申请免费的SSL证书
打开网址https://www.startssl.com/?app=12,选择Sign-up注册. 输入个人注册信息 需注意以下几点:(1)地址必须详细,否则你会收到这样的邮件: Please pro ...