Spring boot+JPA+Druid

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.yginsight</groupId>
  6. <artifactId>pm_admin</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>pm_admin</name>
  10. <description>Power Manager Admin</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.2.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>com.alibaba</groupId>
  34. <artifactId>druid</artifactId>
  35. <version>1.0.26</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-configuration-processor</artifactId>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-data-jpa</artifactId>
  45. </dependency>
  46. <!-- postgresql连接 -->
  47. <dependency>
  48. <groupId>org.postgresql</groupId>
  49. <artifactId>postgresql</artifactId>
  50. <version>9.4-1201-jdbc41</version>
  51. </dependency>
  52. <!-- Springboot 热部署 -->
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-devtools</artifactId>
  56. <optional>true</optional>
  57. </dependency>
  58. </dependencies>
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-maven-plugin</artifactId>
  64. </plugin>
  65. </plugins>
  66. </build>
  67. </project>

application.properties

  1. # 数据库访问配置
  2. # 主数据源,默认的
  3. #spring.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.datasource.druid.driver-class-name=org.postgresql.Driver
  5. spring.datasource.druid.url=jdbc:postgresql://127.0.0.1/powermanager
  6. spring.datasource.druid.username=postgres
  7. spring.datasource.druid.password=root
  8. # 下面为连接池的补充设置,应用到上面所有数据源中
  9. # 初始化大小,最小,最大
  10. spring.datasource.druid.initial-size=5
  11. spring.datasource.druid.minIdle=5
  12. spring.datasource.druid.maxActive=20
  13. # 配置获取连接等待超时的时间
  14. spring.datasource.druid.maxWait=60000
  15. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  16. spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
  17. # 配置一个连接在池中最小生存的时间,单位是毫秒
  18. spring.datasource.druid.minEvictableIdleTimeMillis=300000
  19. spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
  20. spring.datasource.druid.testWhileIdle=true
  21. spring.datasource.druid.testOnBorrow=false
  22. spring.datasource.druid.testOnReturn=false
  23. # 打开PSCache,并且指定每个连接上PSCache的大小
  24. spring.datasource.druid.poolPreparedStatements=true
  25. spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
  26. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
  27. spring.datasource.druid.filters=stat,wall,log4j
  28. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  29. spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  30. # 合并多个DruidDataSource的监控数据
  31. #spring.datasource.druid.useGlobalDataSourceStat=true

由于上述配置文件中,Spring.datasource.druid无法被识别,需要自行增加配置项

增加两个配置相关类

  1. package com.yginsight.druid;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.support.http.StatViewServlet;
  4. import com.alibaba.druid.support.http.WebStatFilter;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  7. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  9. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import javax.sql.DataSource;
  13. import java.sql.SQLException;
  14. /**
  15. * Created by dingshuo on 2017/4/13.
  16. */
  17. @Configuration
  18. @EnableConfigurationProperties({DruidDataSourceProperties.class})
  19. public class DruidConfiguration {
  20. @Autowired
  21. private DruidDataSourceProperties properties;
  22. @Bean
  23. @ConditionalOnMissingBean
  24. public DataSource druidDataSource() {
  25. DruidDataSource druidDataSource = new DruidDataSource();
  26. druidDataSource.setDriverClassName(properties.getDriverClassName());
  27. druidDataSource.setUrl(properties.getUrl());
  28. druidDataSource.setUsername(properties.getUsername());
  29. druidDataSource.setPassword(properties.getPassword());
  30. druidDataSource.setInitialSize(properties.getInitialSize());
  31. druidDataSource.setMinIdle(properties.getMinIdle());
  32. druidDataSource.setMaxActive(properties.getMaxActive());
  33. druidDataSource.setMaxWait(properties.getMaxWait());
  34. druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
  35. druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
  36. druidDataSource.setValidationQuery(properties.getValidationQuery());
  37. druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
  38. druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
  39. druidDataSource.setTestOnReturn(properties.isTestOnReturn());
  40. druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
  41. druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
  42. druidDataSource.setConnectionProperties(properties.getConnectionProperties());
  43. try {
  44. druidDataSource.setFilters(properties.getFilters());
  45. druidDataSource.init();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. return druidDataSource;
  50. }
  51. @Bean
  52. @ConditionalOnMissingBean
  53. public ServletRegistrationBean druidServlet() {
  54. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  55. //添加初始化参数:initParams
  56. //白名单:
  57. //servletRegistrationBean.addInitParameter("allow","127.0.0.1");
  58. //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
  59. //servletRegistrationBean.addInitParameter("deny","192.168.1.73");
  60. //登录查看信息的账号密码.
  61. servletRegistrationBean.addInitParameter("loginUsername", "admin");
  62. servletRegistrationBean.addInitParameter("loginPassword", "admin");
  63. //是否能够重置数据.
  64. servletRegistrationBean.addInitParameter("resetEnable", "true");
  65. return servletRegistrationBean;
  66. }
  67. @Bean
  68. @ConditionalOnMissingBean
  69. public FilterRegistrationBean filterRegistrationBean() {
  70. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  71. filterRegistrationBean.setFilter(new WebStatFilter());
  72. filterRegistrationBean.addUrlPatterns("/*");
  73. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  74. return filterRegistrationBean;
  75. }
  76. }
  1. package com.yginsight.druid;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. /**
  4. * Created by dingshuo on 2017/4/13.
  5. */
  6. @ConfigurationProperties(prefix = "spring.datasource.druid")
  7. public class DruidDataSourceProperties {
  8. private String driverClassName;
  9. private String url;
  10. private String username;
  11. private String password;
  12. private int initialSize;
  13. private int minIdle;
  14. private int maxActive;
  15. private long maxWait;
  16. private long timeBetweenEvictionRunsMillis;
  17. private long minEvictableIdleTimeMillis;
  18. private String validationQuery;
  19. private boolean testWhileIdle;
  20. private boolean testOnBorrow;
  21. private boolean testOnReturn;
  22. private boolean poolPreparedStatements;
  23. private int maxPoolPreparedStatementPerConnectionSize;
  24. private String filters;
  25. private String connectionProperties;
  26. public int getInitialSize() {
  27. return initialSize;
  28. }
  29. public void setInitialSize(int initialSize) {
  30. this.initialSize = initialSize;
  31. }
  32. public int getMinIdle() {
  33. return minIdle;
  34. }
  35. public void setMinIdle(int minIdle) {
  36. this.minIdle = minIdle;
  37. }
  38. public int getMaxActive() {
  39. return maxActive;
  40. }
  41. public void setMaxActive(int maxActive) {
  42. this.maxActive = maxActive;
  43. }
  44. public long getMaxWait() {
  45. return maxWait;
  46. }
  47. public void setMaxWait(long maxWait) {
  48. this.maxWait = maxWait;
  49. }
  50. public long getTimeBetweenEvictionRunsMillis() {
  51. return timeBetweenEvictionRunsMillis;
  52. }
  53. public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  54. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  55. }
  56. public long getMinEvictableIdleTimeMillis() {
  57. return minEvictableIdleTimeMillis;
  58. }
  59. public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
  60. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  61. }
  62. public String getValidationQuery() {
  63. return validationQuery;
  64. }
  65. public void setValidationQuery(String validationQuery) {
  66. this.validationQuery = validationQuery;
  67. }
  68. public boolean isTestWhileIdle() {
  69. return testWhileIdle;
  70. }
  71. public void setTestWhileIdle(boolean testWhileIdle) {
  72. this.testWhileIdle = testWhileIdle;
  73. }
  74. public boolean isTestOnBorrow() {
  75. return testOnBorrow;
  76. }
  77. public void setTestOnBorrow(boolean testOnBorrow) {
  78. this.testOnBorrow = testOnBorrow;
  79. }
  80. public boolean isTestOnReturn() {
  81. return testOnReturn;
  82. }
  83. public void setTestOnReturn(boolean testOnReturn) {
  84. this.testOnReturn = testOnReturn;
  85. }
  86. public boolean isPoolPreparedStatements() {
  87. return poolPreparedStatements;
  88. }
  89. public void setPoolPreparedStatements(boolean poolPreparedStatements) {
  90. this.poolPreparedStatements = poolPreparedStatements;
  91. }
  92. public int getMaxPoolPreparedStatementPerConnectionSize() {
  93. return maxPoolPreparedStatementPerConnectionSize;
  94. }
  95. public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
  96. this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
  97. }
  98. public String getFilters() {
  99. return filters;
  100. }
  101. public void setFilters(String filters) {
  102. this.filters = filters;
  103. }
  104. public String getConnectionProperties() {
  105. return connectionProperties;
  106. }
  107. public void setConnectionProperties(String connectionProperties) {
  108. this.connectionProperties = connectionProperties;
  109. }
  110. public String getDriverClassName() {
  111. return driverClassName;
  112. }
  113. public void setDriverClassName(String driverClassName) {
  114. this.driverClassName = driverClassName;
  115. }
  116. public String getUrl() {
  117. return url;
  118. }
  119. public void setUrl(String url) {
  120. this.url = url;
  121. }
  122. public String getUsername() {
  123. return username;
  124. }
  125. public void setUsername(String username) {
  126. this.username = username;
  127. }
  128. public String getPassword() {
  129. return password;
  130. }
  131. public void setPassword(String password) {
  132. this.password = password;
  133. }
  134. }
时间: 2024-10-13 16:11:24

Spring boot+JPA+Druid的相关文章

Spring Boot [使用 Druid 数据库连接池]

导读 最近一段时间比较忙,以至于很久没有更新Spring Boot系列文章,恰好最近用到Druid, 就将Spring Boot 使用 Druid作为数据源做一个简单的介绍. Druid介绍: Druid是阿里巴巴开源的数据库连接池,Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能,Druid的官方地址 了解更多: JDBC连接池.监控组件 Druid (oschina) 快速上手: 下面来说明如何在 spring Boot 中配置使用Druid ,本例使用的持

Spring Boot JPA 连接数据库

本文将介绍如何在Spring Boot 工程中添加JPA作为持久化方式. 修改 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 修改为 spring-boot-starter-data-jpa 即可,当然数据库驱动包也是不可少的,如下: <!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-conne

自建生成代码工具(暂支持Spring boot +JPA)(更新中)

我们努力的方向就是用更少的时间写出更高质量的代码,因此我们都需要借助一些工具来提高自己的开发效率.写代码有些年头了,慢慢的可以写一些东西来提高工作效率.主要会从Java后台和Android开始,暂时叫“Auto coding”,大家如果有更好的想法麻烦告诉我,谢谢. 1. 生成后台和管理端代码  往往我们做项目会先把表建好再进行编码,管理端有多相似的地方,所以通过表SQL利用AutoCoding所以大部分代码,再对部分部分地方手动调整就能先快速完成一个功能模块. Spring boot JPA

Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口

问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '***.***.***Dao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency

Spring Boot Jpa 的使用

使用 Spring Boot Jpa 开发时,发现国内对 Spring Boot Jpa 全面介绍的文章比较少案例也比较零碎,因此写文章总结一下Spring Data JPA 参考指南! Spring Boot Jpa 介绍 首先了解 Jpa 是什么? Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据.它的出现主要是为了简化现有的持久化开发工作和整合 ORM

Spring Boot 配置Druid数据源

Spring Boot 配置Druid数据源 Druid是阿里巴巴的一个开源项目,使用这个数据源的好处就是可以使用监控功能,提供了一个简单的后台,可以监控数据源的数据 配置方式 导入坐标 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</art

[转] Druid简介(Spring Boot + Mybatis + Druid数据源【自己定制】)

Druid的简介Druid是一个非常优秀的数据库连接池.在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss DataSource. Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验. Druid是一个JDBC组件,它包括三个部分: 基于Filter-Chain模式的插件体系. DruidDataSource 高效可管理的数据库连接池. SQLParser Druid的功能兼容DBCPDruid提

Spring Boot + Jpa(Hibernate) 架构基本配置

本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 不废话,直接上代码 一.maven的pom文件 <?xml version="1.0" encoding="UTF-8"?> <project

spring boot + mybatis + druid

因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能时数据库连接的问题,所以我打算引入数据池,引入数据池的时候找来找去,比较了当前两个最火的数据池,阿里的druid和HikariCP,比来比去选了阿里的druid,虽然spring boot默认不支持druid,而是支持HikariCP,而且HikariCP的性能更好,但是阿里功能多,就是想支持国产 实际配置: 1.首先现在下载一