参考文献:(早期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 配置 文章的更多相关文章

  1. Spring Boot JPA 连接数据库

    本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...

  2. Spring Boot 2.0 配置图文教程

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...

  3. Spring Boot(五):Spring Boot Jpa 的使用

    在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...

  4. (转)Spring Boot(五):Spring Boot Jpa 的使用

    http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...

  5. Spring Boot多数据源配置(二)MongoDB

    在Spring Boot多数据源配置(一)durid.mysql.jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源.本篇文章讲一下Spring Boot如何配置mongoDB多 ...

  6. Spring Boot Jpa 表名小写转大写

    今天在使用SpringBoot整合Hibernate后创建表,表名为小写,而在linux下,mysql的表名是区分大小写的,因此在我的数据表中,就出现了两个一样的表 act_id_user 和  AC ...

  7. 自定义spring boot的自动配置

    文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...

  8. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  9. 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置

    在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...

随机推荐

  1. 关于封装了gevent的request grequest库的使用与讨论

    最近迷上了gevent所以研究很多gevent相关的东西. 但是我现在不想写相关gevent和greenlet的东西.因为这一块内容实在太多太大太杂,我自己也还没有完全弄明白,所以等我完全搞清楚测试也 ...

  2. python学习笔记八——字典的方法

    4.3.3 字典的方法 字典的常用方法可以极大地提高编程效率.keys()和values()分别返回字典的key列表和value列表.例: dict={"a":"appl ...

  3. ceph PG数量调整/PG的状态说明

    优化: PG Number PG和PGP数量一定要根据OSD的数量进行调整,计算公式如下,但是最后算出的结果一定要接近或者等于一个2的指数.调整PGP不会引起PG内的对象的分裂,但是会引起PG的分布的 ...

  4. 浅析python日志重复输出问题

    浅析python日志重复输出问题 问题起源: ​ 在学习了python的函数式编程后,又接触到了logging这样一个强大的日志模块.为了减少重复代码,应该不少同学和我一样便迫不及待的写了一个自己的日 ...

  5. Fail2ban 配置

    本例为wordpress管理员登陆限制安装rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.n ...

  6. google gson

    发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson. 经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实 ...

  7. A1029. Median

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  8. 【CH5302】金字塔 区间DP

    题目大意:给定一棵树,树上点有标记,给定一棵树的\(dfs\)序标记序列,求有多少种可能的子树形态.(子树之间有序) 这是一道区间计数类DP,涉及到树的\(dfs\)序. 这道题区间的划分点 \(k\ ...

  9. http和https的作用与区别

    PS: https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密.防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名.另外是加密,加密需要一个密钥交换算法, ...

  10. Word2010中的页眉怎样删除和添加横线

    http://jingyan.baidu.com/article/f79b7cb3bb3c629144023e05.html 我们在使用Word2010编辑文档中时,有时需要在页眉下方删除或添加一条横 ...