boot2
核心依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/>
</parent>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
配置
spring:
datasource:
fun:
username: root
password: 123456
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
# 配置Druid的其他参数,以下配置必须增加一个配置文件才能有效
type: com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
initialSize: 1
minIdle: 1
maxActive: 20
# 获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat, wall
# 打开PSCache,并且指定每个连接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize: 20
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
DruidConfig
@Slf4j
@Configuration
@MapperScan(basePackages = {"org.study.mybatis.dao.mapper"},sqlSessionTemplateRef = "funSqlSessionTemplate")
public class DruidConfig {
@Autowired
private Environment env;
@Autowired
private DruidConfigProperties druidProperties;
@Bean(name = "fun")
public DataSource druid() {
DruidDataSource dds = new DruidDataSource();
dds.setUrl(druidProperties.getUrl());
dds.setUsername(druidProperties.getUsername());
dds.setPassword(druidProperties.getPassword());
dds.setDriverClassName(druidProperties.getDriverClassName());
dds.setInitialSize(druidProperties.getInitialSize());
dds.setMinIdle(druidProperties.getMinIdle());
dds.setMaxActive(druidProperties.getMaxActive());
dds.setMaxWait(druidProperties.getMaxWait());
dds.setTimeBetweenEvictionRunsMillis(druidProperties.getTimeBetweenEvictionRunsMillis());
dds.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis());
dds.setValidationQuery(druidProperties.getValidationQuery());
dds.setTestWhileIdle(druidProperties.getTestWhileIdle());
dds.setTestOnBorrow(druidProperties.getTestOnBorrow());
dds.setTestOnReturn(druidProperties.getTestOnReturn());
dds.setPoolPreparedStatements(druidProperties.getPoolPreparedStatements());
dds.setMaxPoolPreparedStatementPerConnectionSize(druidProperties.getMaxPoolPreparedStatementPerConnectionSize());
try {
dds.setFilters(druidProperties.getFilters());
dds.init();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return dds;
}
/**
* 根据数据源创建SqlSessionFactory
*/
@Bean("funSqlSessionFactory")
public SqlSessionFactory funSqlSessionFactory(@Qualifier("fun") DataSource dataSource) throws Exception {
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(dataSource);// 指定数据源(这个必须有,否则报错)
fb.setTypeAliasesPackage(env.getProperty("mybatis.type.aliases.package.fun"));// 指定基包
fb.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(env.getProperty("mybatis.mapper.locations.fun")));
return fb.getObject();
}
/**
* 配置事务管理器
*/
@Bean(name = "funTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("fun") DataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
/**
* session连接模板
*/
@Bean(name = "funSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("funSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","192.168.15.21");
bean.setInitParameters(initParams);
return bean;
}
//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
启动项目后,访问http://localhost:8080/druid/index.html
用户名使用配置的admin,123456

看sql监控和url监控

代码 https://github.com/mingwulipo/mybatis-demo
本文介绍如何在Spring Boot项目中配置和使用Druid数据库连接池,包括依赖引入、核心配置、监控配置及代码实现,以提高数据库访问效率。

2519

被折叠的 条评论
为什么被折叠?



