pom.xml

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>

application.properties

#d1数据库连接
spring.datasource.d1.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.d1.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.d1.username = root
spring.datasource.d1.password = root #d1数据库连接池配置
spring.datasource.d1.type=com.alibaba.druid.pool.DruidDataSource
#初始连接数
spring.datasource.d1.initialSize=1
#最小连接数
spring.datasource.d1.minIdle=3
#最大连接数
spring.datasource.d1.maxActive=20
#配置获取连接等待超时的时间
spring.datasource.d1.maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.d1.timeBetweenEvictionRunsMillis=60000
#配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.d1.minEvictableIdleTimeMillis=30000
spring.datasource.d1.validationQuery=SELECT 1 FROM DUAL
spring.datasource.d1.testWhileIdle=true
spring.datasource.d1.testOnBorrow=false
spring.datasource.d1.testOnReturn=false
spring.datasource.d1.poolPreparedStatements=true
#配置监控统计拦截的filters,stat:监控统计、slf4j:日志记录、wall:防御sql注入
spring.datasource.d1.filters=stat,wall,slf4j
spring.datasource.d1.maxPoolPreparedStatementPerConnectionSize=20

多数据源配置d1连接配置类

@Configuration
@MapperScan(basePackages = "com.test.dao.d1.mapper", sqlSessionTemplateRef = "d1SqlSessionTemplate")
public class D1DatabaseConfig {
@Bean(name = "d1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.d1")
public DataSource d1DataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
} @Bean(name = "d1SqlSessionFactory")
public SqlSessionFactory d1SqlSessionFactory(@Qualifier("d1DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/test/dao/d1/mapper/*.xml"));
// 指明实体扫描(多个package用逗号或者分号分隔)
MybatisConfiguration configuration = new MybatisConfiguration();
bean.setTypeAliasesPackage("com.test.dao.d1");
// 导入mybatis配置
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
// 配置打印sql语句
configuration.setLogImpl(StdOutImpl.class);
bean.setConfiguration(configuration);
return bean.getObject();
} @Bean(name = "d1TransactionManager")
public DataSourceTransactionManager d1TransactionManager(@Qualifier("d1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "d1SqlSessionTemplate")
public SqlSessionTemplate d1SqlSessionTemplate(@Qualifier("d1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
} @Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}

Druid监控配置

/**
* 配置 Druid 监控管理后台的Servlet;
* 内置 Servler 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet 方式
* @return
*/
@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");
//后台允许谁(ip)可以访问,为空或者为null时,表示允许所有访问
initParams.put("allow", "");
//后台拒绝谁访问
//initParams.put("kuangshen", "192.168.1.20");表示禁止此ip访问
//设置初始化参数
bean.setInitParameters(initParams);
return bean;
}
/**
* 配置 Druid 监控 之 web 监控的 filter
* WebStatFilter:用于配置Web和Druid数据源之间的管理关联监控统计
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
//exclusions:设置哪些请求进行过滤排除掉,从而不进行统计
Map<String, String> initParams = new HashMap<>();
initParams.put("exclusions", "*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
//"/*" 表示过滤所有请求
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}

测试

//注入数据源
@Autowired
@Qualifier("d1DataSource")
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
//看一下默认数据源
System.out.println(dataSource.getClass());
//获得连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
DruidDataSource druidDataSource = (DruidDataSource) dataSource;
System.out.println("druidDataSource 数据源最大连接数:" + druidDataSource.getMaxActive());
System.out.println("druidDataSource 数据源初始化连接数:" + druidDataSource.getInitialSize());
//关闭连接
connection.close();
}

springboot整合Druid(德鲁伊)配置多数据源数据库连接池的更多相关文章

  1. SpringBoot整合Druid(阿里巴巴)数据源

    (1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  2. springboot整合druid和配置资源监控

    1.添加依赖,在maven repository中搜索 <dependency> <groupId>com.alibaba</groupId> <artifa ...

  3. springboot整合druid监控配置

    方式一:直接引入druid 1.maven坐标 <dependency> <groupId>com.alibaba</groupId> <artifactId ...

  4. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  5. SpringBoot ---yml 整合 Druid(1.1.23) 数据源

    SpringBoot ---yml 整合 Druid(1.1.23) 数据源 搜了一下,网络上有在配置类写 @Bean 配置的,也有 yml 配置的. 笔者尝试过用配置类配置 @Bean 的方法,结果 ...

  6. 6_2.springboot2.x整合Druid和配置数据源监控

    简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss Data ...

  7. SpringBoot整合Druid数据连接池

    SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ...

  8. 【SpringBoot | Druid】SpringBoot整合Druid

    SpringBoot整合Druid Druid是个十分强大的后端管理工具,具体的功能和用途请问阿里爸爸 1. 在pom.xml中导入包 <!-- alibaba 的druid数据库连接池 --& ...

  9. Springboot整合druid

    目录 Springboot整合druid application.yml DruidConfig 数据监控地址:http://localhost:8080/druid Springboot整合drui ...

随机推荐

  1. .NETCore微服务探寻(二) - 认证与授权

    前言 一直以来对于.NETCore微服务相关的技术栈都处于一个浅尝辄止的了解阶段,在现实工作中也对于微服务也一直没有使用的业务环境,所以一直也没有整合过一个完整的基于.NETCore技术栈的微服务项目 ...

  2. MySql数据库GROUP BY使用过程中的那些坑

    MySql数据库GROUP BY使用过程中的那些坑 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. 特别注意: group by 有一个原则,就是 select 后面的所有 ...

  3. 微信小程序预览Word文档

    <view data-url="https://xxxcom/attachment/word.docx" data-type="docx" catchta ...

  4. Scala创建SparkStreaming获取Kafka数据代码过程

    正文 首先打开spark官网,找一个自己用版本我选的是1.6.3的,然后进入SparkStreaming   ,通过搜索这个位置找到Kafka, 点击过去会找到一段Scala的代码 import or ...

  5. Centos7上设置zookeeper自启动

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/zhouzhiwengang/artic ...

  6. 洛谷 P1692 【部落卫队】

    啊这道题其实暴力就行了,算是一道搜索入门题吧. 搜索变量就应该是当前到哪一位了,然后进行枚举,当前的一位加或者不加,然后知道搜完为止. 判断当前一位可不可以加的时候本来想用vector的,但是没调出来 ...

  7. Linux中清空docker容器日志

    新建文件docker-clear-log,放在/usr/local/bin/目录下,文件内容如下: #!/bin/bash -e if [[ -z $ ]]; then echo "No c ...

  8. 一个JSON解构赋值给另一个字段不同的JSON

    往数据里添加JSON字符串 // 往数据里添加JSON字符串 var arr = []; var json ={"name":"liruilong"," ...

  9. 状压DP之LGTB 与序列

    题目 思路 这道题竟然是状压DP,本人以为是数论,看都没看就去打下一题的暴力了,哭 \(A_i\)<=30,所以我们只需要考虑1-58个数,再往后选的话还不如选1更优,注意,1是可以重复选取的, ...

  10. TallestCow

    简单解说 建立差分数组. 以最高的牛为高度基点,假设牛A和牛B能相互看见,就把牛A和牛B中间的牛高度都-1 最后对每头牛直接计算输出即可. 需要注意的是他给出的关系中:两头牛的顺序可能是颠倒的,而且关 ...