springBoot配置多数据源连接数据库
修改配置文件 application.yml
server:
port: 8001
spring:
#定义多数据源
datasource-test1:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
dbcp2:
#验证连接的有效性
test-while-idle: true
#验证语句
validation-query: SELECT 1
#空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
time-between-eviction-runs-millis: 300000
#连接池空闲连接的有效时间 ,设置30分钟
soft-min-evictable-idle-time-millis: 1800000
hikari:
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired)
# max-lifetime: 60000
#连接池中允许的最大连接数
maximum-pool-size: 15
#<!-- 生效超时 -->
validation-timeout: 3000
#连接只读数据库时配置为true, 保证安全
read-only: false
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException
connection-timeout: 60000
#一个连接idle状态的最大时长(毫秒),超时则被释放(retired)
idle-timeout: 60000
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
max-lifetime: 120000
datasource-test2:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
dbcp2:
#验证连接的有效性
test-while-idle: true
#验证语句
validation-query: SELECT 1
#空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
time-between-eviction-runs-millis: 300000
#连接池空闲连接的有效时间 ,设置30分钟
soft-min-evictable-idle-time-millis: 1800000
hikari:
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired)
# max-lifetime: 60000
#连接池中允许的最大连接数
maximum-pool-size: 15
#<!-- 生效超时 -->
validation-timeout: 3000
#连接只读数据库时配置为true, 保证安全
read-only: false
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException
connection-timeout: 60000
#一个连接idle状态的最大时长(毫秒),超时则被释放(retired)
idle-timeout: 60000
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
max-lifetime: 120000
#mybatis配置
mybatis:
# mapper-locations: classpath:mybatis/mapper/company/*.xml,mybatis/mapper/tinymeng/*.xml #配置多数据源以后在config里边定义了mapper的位置,此行失效
config-location: classpath:mybatis/mybatis-config.xml
创建config
文件夹
1.创建 DataSourceConfig
package com.majiameng.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* 数据源配置
*/
@Configuration
public class DataSourceConfig {
@Bean(name="datasourceTest1")
@ConfigurationProperties(prefix = "spring.datasource-test1")
public DataSource datasourceTest1() {
return DataSourceBuilder.create().build();
}
@Bean(name="datasourceTest2")
@ConfigurationProperties(prefix = "spring.datasource-test2")
public DataSource datasourceTest2() {
return DataSourceBuilder.create().build();
}
}
2.创建 MybatisTest1Config
package com.majiameng.config;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* 定义test1数据源
*/
@Configuration
@MapperScan(basePackages = {"com.majiameng.mapper.test1"},sqlSessionFactoryRef = "sqlSessionFactoryTest1")
public class MybatisTest1Config {
@Autowired
@Qualifier(value="datasourceTest1")
private DataSource test1;
@Bean
public SqlSessionFactory sqlSessionFactoryTest1() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(test1);
//指定mapper位置
factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return factory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateTest1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTest1());
return template;
}
@Bean(name = "transactionManagerTest1")
public PlatformTransactionManager transactionManagerTest1() {
return new DataSourceTransactionManager(test1);
}
}
1.创建 MybatisTest2Config
package com.majiameng.config;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* 定义test2数据源
*/
@Configuration
@MapperScan(basePackages = {"com.majiameng.mapper.test2"},sqlSessionFactoryRef = "sqlSessionFactoryTest2")
public class MybatisTest2Config {
@Autowired
@Qualifier(value="datasourceTest2")
private DataSource test2;
@Bean
public SqlSessionFactory sqlSessionFactoryTest2() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(test2);
//指定mapper位置
factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
return factory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateTest2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTest2());
return template;
}
@Bean(name = "transactionManagerTest2")
public PlatformTransactionManager transactionManagerTest2() {
return new DataSourceTransactionManager(test2);
}
}
两个数据源的配置创建好了
然后对应创建包
com.majiameng.mapper.test1;
com.majiameng.mapper.test2;
resources.mybatis.mapper.test1
resources.mybatis.mapper.test2
在启动类上加注解,启动事务
//@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册
@ServletComponentScan
//事务管理注解
@EnableTransactionManagement