Spring Boot 整合mybatis 使用多数据源

时间:2023-03-09 18:59:53
Spring Boot 整合mybatis 使用多数据源

本人想要实现一个项目里面多个数据库源连接,所以就尝试写一个demo,不多说,先贴结构,再贴代码,可以根据以下的顺序,直接copy解决问题。

Spring Boot 整合mybatis 使用多数据源

首先,dao和resource下的mappers可以用mybatis-generator-maven-plugin插件自动生成,用法可以自己查看,这里不说了。

1.Application.java源码

@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

exlude实现不自动注入spring.mybatis默认的配置信息

2.config包

a.DataSourceConfig.java

@Configuration
public class DataSourceConfig {
@Bean(name = "HTTags")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.HTTags")
public DataSource dataSourceA() {
return DataSourceBuilder.create().build();
} @Bean(name = "DBHTNews")
@ConfigurationProperties(prefix = "spring.datasource.DBHTNews")
public DataSource dataSourceB() {
return DataSourceBuilder.create().build();
}
}

我这里需要实现两个不同的库之间的操作,所以建立这个DataSourceConfig,从application.properties里面读取不同配置信息,返回新的DataSource。

b.MybatisDataBaseAConfig.java

@Configuration
@MapperScan(basePackages = {"com.htsec.sync.tag.manager.dao.httags"}, sqlSessionFactoryRef = "sqlSessionFactory1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MybatisDataBaseAConfig { @Autowired
@Qualifier("HTTags")
private DataSource ds1; /**
* //参数传入:@Qualifier("httags") DataSource ds1
*
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):异常
factoryBean.setMapperLocations(resolver.getResources("classpath:mappers/httags/*.xml"));
return factoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* //参数传入:@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory
*
* @return
* @throws Exception
*/
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory1());
return sessionTemplate;
}
}

此处注意有坑,一定把mappersan写好,还要注意resource下的mappers地址配置。

c.MybatisDataBaseBConfig.java

@Configuration
@MapperScan(basePackages = {"com.htsec.sync.tag.manager.dao.htnews"}, sqlSessionFactoryRef = "sqlSessionFactory2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MybatisDataBaseBConfig { @Autowired
@Qualifier("DBHTNews")
private DataSource ds1; /**
* //参数传入:@Qualifier("httags") DataSource ds1
*
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
factoryBean.setMapperLocations(resolver.getResources("classpath:mappers/htnews/*.xml"));
return factoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* //参数传入:@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory
*
* @return
* @throws Exception
*/
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory2());
return sessionTemplate;
}
}

sqlSessionFactory和sqlSessionTemplate命名一定要和上面那个不一样。

3.application.properties

# HTTags
spring.datasource.HTTags.url=jdbc:sqlserver://x.x.x.x:1433;DatabaseName=HTTags
spring.datasource.HTTags.username=sa
spring.datasource.HTTags.password=password
spring.datasource.HTTags.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver # DBHTNews
spring.datasource.DBHTNews.url=jdbc:sqlserver://x.x.x.x:1433;DatabaseName=HTNews
spring.datasource.DBHTNews.username=sa
spring.datasource.DBHTNews.password=password
spring.datasource.DBHTNews.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

4.pom.xml

<dependencies>
<!-- springframework -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <!-- spring boot test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency> <!-- org.mybatis.spring.boot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency> <!-- sql server jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>

就这么多吧。