配置druid
spring-boot目前支持的连接池有dbcp,dbcp2,hikari,tomcat的jdbc(这也是默认使用的),并不默认支持druid。
Mybatis-Generator:
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 具体的Mybatis-Generator的介绍请各位自己找度娘。
yml配置
/server:
port:8081
spring:
datasource:
# 主数据源配置信息
primaryDataSource:
url: jdbc:mysql://XXX.XXX.XXX.XXX:3306/db1?characterEncoding=utf-8
username: username
password: password
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
type-aliases-package: com.entity.database
mapper-locations: classpath:mapper/database/*.xml
configLocation: classpath:mapper/mybatis-config.xml
# 数据源二配置信息
secondDataSource:
url:jdbc:mysql://XXX.XXX.XXX.XXX:3306/db2?characterEncoding=utf-8
username: username
password: password
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
validationQuery: select 'x'
testWhileIdle:true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements:true
maxOpenPreparedStatements:20
type-aliases-package: com.entity.second
mapper-locations: classpath:mapper//second/*.xml
configLocation: classpath:mapper/mybatis-config.xml
#pagehelper
pagehelper:
helperDialect:mysql
reasonable:true
supportMethodsArguments:true
params: count=countSql
增加两个配置文件
数据源配置一:PrimaryMybatisConfiguration
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages = { "com.dao.database" },sqlSessionTemplateRef="primarySqlSessionTemplate")
public class PrimaryMybatisConfiguration {
private static Log logger = LogFactory.getLog(PrimaryMybatisConfiguration.class);
@Value("${spring.datasource.primaryDataSource.url}")
private String url;
@Value("${spring.datasource.primaryDataSource.username}")
private String username;
@Value("${spring.datasource.primaryDataSource.password}")
private String password;
@Bean(name = "primaryDataSource")
@Primary
public DataSource testDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setTestWhileIdle(true);
dataSource.setValidationQuery("select 1");
return dataSource;
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
#如果生成的mybatis是xml形式,则下面需要加上.xml包所在的地址
#如果生成的mybatis使用的是ANNOTATEDMAPPER模式,则下面的一句可以注释掉
//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/database/*.xml"));
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
数据源配置二:
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages = "com.dao.second", sqlSessionTemplateRef = "assetSqlSessionTemplate")
public class SecondMybatisConfiguration {
@Value("${spring.datasource.secondDataSource.url}")
private String url;
@Value("${spring.datasource.secondDataSource.username}")
private String username;
@Value("${spring.datasource.secondDataSource.password}")
private String password;
@Bean(name = "secondDataSource")
public DataSource testDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setTestWhileIdle(true);
dataSource.setValidationQuery("select 1");
return dataSource;
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
具体代码可以参考:
Mybatis是XML格式的多数据源配置:
Mybatis是java对象格式的多数据源配置: