boot2
Druid的使用詳解。核心依賴
<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: rootpassword: 123456driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false# 配置Druid的其他參數,以下配置必須增加一個配置文件才能有效type: com.alibaba.druid.pool.DruidDataSource# 初始化大小,最小,最大initialSize: 1minIdle: 1maxActive: 20# 獲取連接等待超時的時間maxWait: 60000# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一個連接在池中最小生存的時間,單位是毫秒minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: 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 {@Autowiredprivate Environment env;@Autowiredprivate 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); // 使用上面配置的Factoryreturn template;}//配置Druid的監控//1、配置一個管理后臺的Servlet@Beanpublic 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@Beanpublic 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
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态