整合了一大堆 ORM,是时候增加一个连接池了,此处选用了 druid 作为连接池,druid 是 alibaba 开源平台上的一个数据库连接池实现,对比 c3p0,dbcp 加入了对数据库的监控,不知道甩出几条街的距离,个人推为数据库连接池的首选(手动摊手)
这里仍然使用 jpa+druid
首先先来看看引入的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///test spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=30000
spring.datasource.druid.validation-query=select 'x'
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.exception-sorter=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
spring.datasource.druid.use-global-data-source-stat=true
spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
|
关于 jpa 的搭建的话,建议查看这个SpringBoot 整合 JPA
如果是按照我这种方式的话,那么现在就已经整合完毕了
接来下访问一下 druid 的监控页druid
再看看连接池的配置成功没有
以及 sql 的监控
还记得上面说的 druid 的两种配置方式吗,上一种的话,只需要我们写好配置文件就 OK 了,而下一种就麻烦一点,我在这里还是把代码贴上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.priv.gabriel;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-10 * @Desciption: */
@Configuration
public class DruidConfig {
@Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid(){ return new DruidDataSource(); }
@Bean public ServletRegistrationBean druidServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); bean.addInitParameter("allow","127.0.0.1"); bean.addInitParameter("resetEnable","false"); bean.addInitParameter("loginUsername","admin"); bean.addInitParameter("loginPassword","admin"); return bean; }
@Bean public FilterRegistrationBean statFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter()); bean.addUrlPatterns("/*"); bean.addInitParameter("exclusions","*.js,*.gif,/druid/*"); return bean; } }
|
这种方式需要我们手动的写上一个 servlet 以及 filter,*注意:*这里必须要手动的注入一个 DruidDataSource 而且指定去读取文件,不然它是不会初始化数据源的 [这么个问题搞了半个多小时]
好了,现在 druid 的已经整合完毕啦。