目录
前言
多数据源在企业级应用中常见的一种配置,常常用到,mybatis又是常常和springboot 集成进行配置的,所以mybatis 与 springboot 集成配置的多数据源就比较实用。
xml依赖引入
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis 集成到springboot2.x中只需要引入此依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--数据库连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
properties 配置文件配置多数据源
#应用起个名字
spring.application.name=datasource_config
#应用启动端口号
server.port=8080
## 数据库
# 数据源类型 这里用SpringBoot2.x默认HikariDataSource
spring.datasource.webmgr.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.webmgr.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.webmgr.jdbc-url=jdbc:mysql:///datasourceone
spring.datasource.webmgr.username=root
spring.datasource.webmgr.password=root
spring.datasource.presentation.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.presentation.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.presentation.jdbc-url=jdbc:mysql:///datasourceone
spring.datasource.presentation.username=root
spring.datasource.presentation.password=root
创建表
-- 放到datasourceone库
CREATE TABLE `testone` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`desc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of testone
-- ----------------------------
INSERT INTO `testone` VALUES (1, 'one数据源', 'desc数据源');
-- 放到datasourcetwo库
CREATE TABLE `testone` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`des` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of testone
-- ----------------------------
INSERT INTO `testone` VALUES (1, 'two数据源', 'des数据源');
配置多数据源配置
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.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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @author: ligangan
* @Date: 2021/11/22
* @Time: 16:39
*/
// 配置注解
@Configuration
// Mapper扫描路径 One数据源,sqlSession工厂
@MapperScan(basePackages = "com.lxn.mapper.datasourceOne",sqlSessionFactoryRef = "oneSqlSessionFactory")
public class DataSourceOneConfig {
// 生成数据源
@Bean(name = "oneDataSource")
@ConfigurationProperties(prefix = "spring.datasource.webmgr")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Qualifier
@Bean(name = "oneSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("oneDataSource")DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/mapper/one/*.xml";
bean.setDataSource(dataSource);
bean.setMapperLocations(resolver.getResources(packageSearchPath));
return bean.getObject();
}
@Bean("oneSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
}
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.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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @author: ligangan
* @Date: 2021/11/22
* @Time: 16:39
*/
@Configuration
@MapperScan(basePackages = "com.lxn.mapper.datasourceTwo",sqlSessionFactoryRef = "twoSqlSessionFactory")
public class DataSourceTwoConfig {
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.presentation")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Qualifier
@Bean(name = "twoSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("twoDataSource")DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/mapper/two/*.xml";
bean.setMapperLocations(resolver.getResources(packageSearchPath));
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean("twoSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
}
配置mapper
import com.lxn.entity.OneEntity;
import org.apache.ibatis.annotations.Param;
public interface OneMapper {
OneEntity getOne(@Param("id") Long id);
}
import com.lxn.entity.OneEntity;
import org.apache.ibatis.annotations.Param;
/**
* @author: ligangan
* @Date: 2021/11/22
* @Time: 16:45
*/
public interface TwoMapper {
OneEntity getOne(@Param("id") Long id);
}
增加controller 测试
@RestController
@RequestMapping("/datasource")
public class TestDataSourceRevert {
@Autowired
private TestDataSourceDao testDataSourceDao;
@GetMapping("/one")
public OneEntity one(Long id){
return testDataSourceDao.getOne(id);
}
@GetMapping("/two")
public OneEntity two(Long id){
return testDataSourceDao.getTwo(id);
}
}
文件目录
【示例】
:https://gitee.com/lixiaoningsll/multi-data-source-mybatis