SpringBoot整合Mybatis多数据源
在一个项目中有多个数据源(不同库JDBC):最多可以有无限多个。具体多少根据内存大小。
在一个项目多数据源如何划分?
分包名(根据业务):原理使用根据包名,加载不同的数据源
或者根据注解方式。
配置文件中新增两个数据源
###datasource1
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = 123456
###datasource2
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = 123456
新增两个数据源配置
DataSource01 数据源
**
* 读取 DataSource01 数据源
*/
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.xiaoming.test01", sqlSessionFactoryRef = "test1SqlSessionFactory") //扫包范围
public class DataSource1Config {
/**
* 功能描述:(配置test1数据库)
* @return
*/
@Bean(name = "test1DataSource") //注入到 spring 容器中
@ConfigurationProperties(prefix = "spring.datasource.test1") //以这个开始到配置文件中去读
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* (test1 sql会话工厂)
*/
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//加载 mapper 配置文件
// bean.setMapperLocations(
// new
// PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
/**
* 功能描述:(test1 事物管理)
*/
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSource2
//DataSource2
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.xiaoming.test02", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
/**
* 配置test2数据库
*/
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* test2 sql会话工厂
*/
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(
// new
// PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
return bean.getObject();
}
/**
* test2 事物管理
*/
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
创建分包Mapper
public interface UserMapperTest01 {
/**
* 查询
* @param name
* @return
*/
@Select("SELECT * FROM USER WHERE NAME = #{name}")
User findByName(@Param("name") String name);
/**
* 添加
* @param name
* @param age
* @return
*/
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
}
启动项目
@SpringBootApplication
//@MapperScan(basePackages = {"com.xiaoming.mapper"}) //通过反射读取该包下所有的类,装入到容器中
@MapperScan(basePackages = {"com.xiaoming.*.mapper"}) //通过反射读取该包下所有的类,装入到容器中
public class MyBaitsApp {
public static void main(String[] args) {
SpringApplication.run(MyBaitsApp.class, args);
}
}
多数据源事务注意事项
在多数据源的情况下,使用@Transactional注解时,应该指定事务管理者
@Transactional(transactionManager = "test2TransactionManager")
可能会出现的异常
No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: test1DataSource,test2DataSource
加上@Primary即可。
Springboot1.5的时候 没有默认指向数据源 会报错
Springboot2.0的时候 不报错
There was an unexpected error (type=Internal Server Error, status=500).
No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available: expected single matching bean but found 2: test1TransactionManager,test2TransactionManager
指定事务管理器