springboot中使用Druid数据库连接池,监控服务请求和sql

首先引入相应的包


    com.alibaba
    druid-spring-boot-starter
    1.1.10

修改数据源配置类为:

@Configuration
//为了支持注解事务,增加了@EnableTransactionManagement注解,并且反回了一个PlatformTransactionManagerBean
@EnableTransactionManagement
@MapperScan(basePackages ="com.test.*.dao",sqlSessionFactoryRef = "primarySqlSessionFactory")
@Slf4j
public class PrimaryDataSourceConfig {
 
    @Bean(name = "primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource primaryDataSource() {
        return DruidDataSourceBuilder.create().build();
    }
 
    @Bean(name = "primaryTransactionManager")
    @Primary
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        log.info("-------------------- primaryTransactionManager init ---------------------");
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(primaryDataSource);
        
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:sqlmap/test/**/*.xml"));
 
        log.info("-------------------- primarySqlSessionFactory init ---------------------");
        return sessionFactory.getObject();
    }
 
    @Bean(name="primarySqlSessionTemplate")
    public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        log.info("-------------------- primarySqlSessionTemplate init ---------------------");
        return new SqlSessionTemplate(sqlSessionFactory);
    }

我们访问http://localhost:8081/druid/index.html即可查看监控页面,是不是很方便。

你可能感兴趣的:(springboot中使用Druid数据库连接池,监控服务请求和sql)