SpringBoot配置Druid
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。关于详细介绍可查看http://www.iteye.com/magazines/90
SpringBoot中集成Druid非常简单
一:yml中添加配置链接
spring: #MySQL数据库 datasource: url: jdbc:mysql://182.254.221.58:3306/test?useSSL=false&&characterEncoding=utf8 username: root password: Zoulu19900206@ driver-class-name: com.mysql.jdbc.Driver #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,wall用于防火墙 filters: stat,wall,log4j #最大活跃数 maxActive: 20 #初始化数量 initialSize: 1 #最大连接等待超时时间 maxWait: 60000 #打开PSCache,并且指定每个连接PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 #通过connectionProperties属性打开mergeSql功能;慢sql记录 connectionProperties: druid.stat.mergeSql=true;druif.stat.slowSqlMillis=5000 minldle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableTimeMillis: 300000 validationQuery: select 1 from dual testWhileldle: true testOnBorrow: false testOnReturn: false #JPA jpa: properties: hibernate: hbm2ddl.auto: true dialect: org.hibernate.dialect.MySQL5InnoDBDialect show-sql: true
二:添加DruidConfiguration配置类
@Configuration public class DruidConfiguration { @Bean public ServletRegistrationBean statViewServlet(){ //创建servlet注册实体 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //设置ip白名单 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow servletRegistrationBean.addInitParameter("deny","192.168.0.19"); //设置控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername","xiaopotian"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否可以重置数据 servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ //创建过滤器 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //设置过滤器过滤路径 filterRegistrationBean.addUrlPatterns("/*"); //忽略过滤的形式 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource(){ return new DruidDataSource(); } }
三:运行http://localhost:8080/druid进入登陆界面,用上面配置的用户名密码登录即可
非常的简单
最后附上demo地址 https://github.com/xiaopotian1990/SpringBootDemo