注:本文介绍的mysql版本是5.x ,如果mysql升级到8 有几个地方需要改动
https://blog.csdn.net/a704397849/article/details/108396563
上一篇介绍了springboot mybatis多数据库源码demo,本章在之前的基础上增加Druid连接池配置
https://blog.csdn.net/a704397849/article/details/93667782
druid 的maven依赖
com.alibaba
druid
1.1.0
application.properties 新增druid 的配置 ,注意配置中数据库连接的ip prot和账号密码填自己的
#mybatis(地址端口账号密码填自己的mysql连接信息,dbtest是一个自定义的数据库连接对象名,后面配置创建数据库连接会用到)
spring.datasource.dbtest.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.dbtest.username=xxxxxx
spring.datasource.dbtest.password=xxxxxx
spring.datasource.dbtest.driver-class-name=com.mysql.jdbc.Driver
#第二个数据源(地址端口账号密码填自己的mysql连接信息,dbtest2是一个自定义的数据库连接对象名,后面配置创建数据库连接会用到)
spring.datasource.dbtest2.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.dbtest2.username=xxxxxx
spring.datasource.dbtest2.password=xxxxxx
spring.datasource.dbtest2.driver-class-name=com.mysql.jdbc.Driver
#连接池druid的配置信息
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall
上面配置中的2个数据库test 和 test2 的 config文件修改masterDataSource方法,原本返回默认的DataSource , 更改为返回创建的 DruidDataSource
DataSourceDbtestConfig
package com.example.mybatis_demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.mybatis_demo.mapper.dbtest", sqlSessionFactoryRef = "dbtestSqlSessionFactory")
public class DataSourceDbtestConfig {
/**
* 数据库连接配置
*/
@Value("${spring.datasource.dbtest.jdbcUrl}")
private String url;
@Value("${spring.datasource.dbtest.username}")
private String user;
@Value("${spring.datasource.dbtest.password}")
private String password;
@Value("${spring.datasource.dbtest.driver-class-name}")
private String driverClass;
/**
* druid配置
*/
@Value("${spring.datasource.druid.maxActive}")
private Integer maxActive;
@Value("${spring.datasource.druid.minIdle}")
private Integer minIdle;
@Value("${spring.datasource.druid.initialSize}")
private Integer initialSize;
@Value("${spring.datasource.druid.maxWait}")
private Long maxWait;
@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
private Long timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
private Long minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testWhileIdle}")
private Boolean testWhileIdle;
@Value("${spring.datasource.druid.testWhileIdle}")
private Boolean testOnBorrow;
@Value("${spring.datasource.druid.testOnBorrow}")
private Boolean testOnReturn;
@Value("${spring.datasource.druid.poolPreparedStatements}")
private Boolean poolPreparedStatements;
@Value("${spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize}")
private Integer maxPoolPreparedStatementPerConnectionSize;
@Primary
@Bean(name = "dbtestDataSource")
public DataSource masterDataSource() {
// return DataSourceBuilder.create().build();
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
//连接池配置
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minIdle);
dataSource.setInitialSize(initialSize);
dataSource.setMaxWait(maxWait);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setValidationQuery(validationQuery);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return dataSource;
}
@Primary
@Bean(name = "dbtestSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dbtestDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
//注意: com/example/mybatis_demo/mapper/dbtest/ 下必须至少要有一个文件,否则启动会失败
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/mybatis_demo/mapper/dbtest/*.xml"));
return sessionFactoryBean.getObject();
}
}
DataSourceDbtest2Config
package com.example.mybatis_demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.mybatis_demo.mapper.dbtest2", sqlSessionFactoryRef = "dbtest2SqlSessionFactory")
public class DataSourceDbtest2Config {
/**
* 数据库连接配置
*/
@Value("${spring.datasource.dbtest2.jdbcUrl}")
private String url;
@Value("${spring.datasource.dbtest2.username}")
private String user;
@Value("${spring.datasource.dbtest2.password}")
private String password;
@Value("${spring.datasource.dbtest2.driver-class-name}")
private String driverClass;
/**
* druid配置
*/
@Value("${spring.datasource.druid.maxActive}")
private Integer maxActive;
@Value("${spring.datasource.druid.minIdle}")
private Integer minIdle;
@Value("${spring.datasource.druid.initialSize}")
private Integer initialSize;
@Value("${spring.datasource.druid.maxWait}")
private Long maxWait;
@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
private Long timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
private Long minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testWhileIdle}")
private Boolean testWhileIdle;
@Value("${spring.datasource.druid.testWhileIdle}")
private Boolean testOnBorrow;
@Value("${spring.datasource.druid.testOnBorrow}")
private Boolean testOnReturn;
@Value("${spring.datasource.druid.poolPreparedStatements}")
private Boolean poolPreparedStatements;
@Value("${spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize}")
private Integer maxPoolPreparedStatementPerConnectionSize;
@Bean(name = "dbtest2DataSource")
public DataSource masterDataSource() {
// return DataSourceBuilder.create().build();
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
//连接池配置
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minIdle);
dataSource.setInitialSize(initialSize);
dataSource.setMaxWait(maxWait);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setValidationQuery(validationQuery);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return dataSource;
}
@Bean(name = "dbtest2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dbtest2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
//注意: com/example/mybatis_demo/mapper/dbtest2/ 下必须至少要有一个文件,否则启动会失败
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/mybatis_demo/mapper/dbtest2/*.xml"));
return sessionFactoryBean.getObject();
}
}
链接:https://pan.baidu.com/s/1PvA7_eNtpcKa9r3R8ibwJQ
提取码:7rya
注:本篇是在上一篇springboot集成mybatis多数据库 的基础上增加druid连接 https://blog.csdn.net/a704397849/article/details/93667782
下一篇介绍springboot maven 打包jar 运行 https://blog.csdn.net/a704397849/article/details/94859617