SpringBoot配置Mybatis多数据源
配置多数据源可以将springboot自动装配的数据源给关闭。
1、添加pom文件,只需要添加数据源驱动和mybatis包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2、添加配置fkSqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
//扫描mapper接口
@MapperScan(basePackages ="com.hua.mapper.fk",sqlSessionFactoryRef = "fkSqlSessionFactory")
public class FkMybatisConfig {
//配置数据源
@Bean("fkDataSource")
@ConfigurationProperties(prefix = "spring.datasource.fk")
public DataSource fkDataSource() {
return DataSourceBuilder.create().build();
}
//生成sqlSessionFactory
@Primary
@Bean(name = "fkSqlSessionFactory")
public SqlSessionFactory fkSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//扫描xml文件,相当于我们自动装配时候的
//mybatis.mapper-locations=classpath:/mapper/fk/*Mapper.xml
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:/mapper/fk/*Mapper.xml"));
//添加数据源
bean.setDataSource(fkDataSource());
bean.setVfs(SpringBootVFS.class);
return bean.getObject();
}
//生成sqlSessionTemplate
@Primary
@Bean("fk")
public SqlSessionTemplate fkSqlSessionTemplate(
@Qualifier("fkSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages ="com.hua.mapper.lk",sqlSessionFactoryRef = "lkSqlSessionFactory")
public class LkMybatisConfig {
@Bean("lkDataSource")
@ConfigurationProperties(prefix = "spring.datasource.lk")
public DataSource fkDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "lkSqlSessionFactory")
public SqlSessionFactory fkSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:/mapper/lk/*Mapper.xml"));
bean.setDataSource(fkDataSource());
bean.setVfs(SpringBootVFS.class);
return bean.getObject();
}
@Bean("lk")
public SqlSessionTemplate fkSqlSessionTemplate(
@Qualifier("lkSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
这两个配置文件,将com.hua.mapper.lk接口和classpath:/mapper/lk/*Mapper.xml的xml文件绑定,将com.hua.mapper.fk接口和classpath:/mapper/fk/*Mapper.xml的xml文件绑定,形成两个包下独立的数据源
3、新建mapper接口包
这个是mapper接口的包路径,根据我们的配置文件fk和lk分别对应不同的数据源
4、新建xml文件
这里是xml文件的包路径,和上面的接口对应
结语:这种多数据源一般加的不多这种方式还是比较方便的。如果很大型的项目,动态数据源可能更加方便一点