基于项目业务需求,需对接第三方公共库并执行相关操作。为避免直接使用原生JDBC带来的复杂性,决定采用MyBatis框架进行多数据源配置。
网络上关于纯MyBatis多数据源的资料较为丰富,但本项目前期为提升开发效率引入了tk-mybatis。由于官方未提供tk-mybatis多数据源的直接支持方案,最终采取混合策略:即原生MyBatis与tk-mybatis并存,各自管理不同的数据源以实现功能。具体实现代码如下所示。
一、配置连接信息
#开发运行环境数据库
spring.datasource.local.url=jdbc:mysql://0.0.0.0:3306/zhdd
spring.datasource.local.username=root
spring.datasource.local.password=
spring.datasource.local.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.local.druid.initial-size=1
spring.datasource.local.druid.min-idle=1
spring.datasource.local.druid.max-active=20
spring.datasource.local.druid.test-on-borrow=true
spring.datasource.local.druid.stat-view-servlet.allow=true
#第三方对接通用数据库
spring.datasource.public.url=jdbc:mysql://0.0.0.0:3306/zhdd
spring.datasource.public.username=root
spring.datasource.public.password=
spring.datasource.public.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.public.druid.initial-size=1
spring.datasource.public.druid.min-idle=1
spring.datasource.public.druid.max-active=20
spring.datasource.public.druid.test-on-borrow=true
spring.datasource.public.druid.stat-view-servlet.allow=true
二、配置DataSource数据源
在配置多个数据源时,必须有一个被标记为主数据源,这需要添加@Primary注解,且该注解有且仅能应用于一个数据源实例。对于超过两个数据源的场景,本文未做深入探讨,但理论上可通过类似方式扩展MyBatis配置来实现。
1.数据源1配置
package com.hiynn.core.mybatis;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
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 javax.sql.DataSource;
/**
* 开发运行环境数据库
*/
@Configuration
public class LocalDataSourceConfig {
@Bean(name = "localDataSource")
@ConfigurationProperties(prefix = "spring.datasource.local")
@Primary
public DataSource localDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "localTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(localDataSource());
}
@Bean(name = "localSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return sessionFactory.getObject();
}
}
2.数据源2配置
package com.hiynn.core.mybatis;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;
/**
* 公共通用数据库
*/
@Configuration
public class PublicDataSourceConfig {
@Bean(name = "publicDataSource")
@ConfigurationProperties(prefix = "spring.datasource.public")
public DataSource publicDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "publicTransactionManager")
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(publicDataSource());
}
@Bean(name = "publicSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("publicDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper2/*.xml"));
return sessionFactory.getObject();
}
}
三、Application配置
package com.hiynn.core;
//特别注意,下面的是 tk.MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @author zz
* @date 2026-7-2
*/
@ServletComponentScan
@SpringBootApplication
@EnableTransactionManagement
@EnableSwagger2
@MapperScan(basePackages = "com.hiynn.core.**.mapper",sqlSessionFactoryRef = "localSqlSessionFactory")
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.hiynn.core.**.mapper2",sqlSessionFactoryRef = "publicSqlSessionFactory")
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四、Mapper代码结构

补充:
为解决MyBatis数据库字段到Java对象的驼峰命名自动映射问题,以及开启SQL语句打印以便调试,需通过读取配置文件进行设置。这两项配置对两个数据源均生效。
@Bean(name = "localSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//添加mybatis配置文件读取功能更
sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("mybatis-config.xml"));
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return sessionFactory.getObject();
}
<?xml version="1.0" encoding="UTF-8"?>
二、配置PageHelper分页插件,在application.properties统一配置
数据库方言 指定分页插件使用哪种数据库方言
pagehelper.helperDialect=mysql
#分页参数合理化 pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
到此这篇关于tk-mybatis整合springBoot使用两个数据源的方法的文章就介绍到这了,更多相关tk-mybatis springBoot数据源内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!