spring boot 2.0+mybatis 实现多数据源切换
程序员文章站
2022-03-26 17:43:41
1、我的项目目录2、application.yml数据配置3、实现类package com.example.supermarket.project.config;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybati...
说明一下:
需要的spring boot 2.0 + mybatis需要自己搭建,
这里是已配置好的项目,实现多数据源切换。
1、我的项目目录
2、数据库连接配置
application.yml
server:
port: 8080
servlet:
context-path: /
# Spring配置
spring:
datasource:
druid:
text1:
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
username: root
password: ******
text2:
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
username: root
password: ******
text3:
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
username: root
password: ******
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
# 文件上传
servlet:
multipart:
# 单个文件大小
max-file-size: 10MB
# 设置总上传的文件大小
max-request-size: 20MB
mybatis:
mapper-locations: classpath:mapping/*/*Mapper.xml
type-aliases-package: com.example.supermarket.project.domain
configuration:
map-underscore-to-camel-case: true
3、实现配置信息类 (其他的数据源方法类似,需要加载几个数据源,就写几个这样的实体类)
Test1DataSourceConfig.java
package com.example.supermarket.project.config;
import com.alibaba.druid.pool.DruidDataSource;
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.beans.factory.annotation.Value;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.supermarket.project.dao.test1",sqlSessionFactoryRef = "test1SqlSessionFactory")
public class Test1DataSourceConfig {
//${spring.datasource.druid.**} 获取需要的数据库信息
@Value("${spring.datasource.druid.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.druid.fishlog.url}")
private String url;
@Value("${spring.datasource.druid.fishlog.username}")
private String username;
@Value("${spring.datasource.druid.fishlog.password}")
private String password;
@Bean(name = "test1DataSource")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
System.out.println(this.driverClassName+this.url+this.username+this.password);
return dataSource;
}
//classpath*:mapping/test1/*Mapper.xml 数据库对应的指定Mapper,根据你的路径进行配置
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/test1/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
本文地址:https://blog.csdn.net/Ghost_hell/article/details/109550749
上一篇: 设计一个BankAccount类
推荐阅读
-
spring boot多数据源动态切换代码实例
-
Spring Boot 整合 Mybatis 实现 Druid 多数据源详解 springjavajdbc
-
spring boot 2.0+mybatis 实现多数据源切换
-
Spring Boot 整合mybatis 使用多数据源的实现方法
-
通过Spring Boot配置动态数据源访问多个数据库的实现代码
-
spring boot微服务架构mybatis多数据源切换
-
spring boot多数据源动态切换代码实例
-
Spring与Mybatis相结合实现多数据源切换功能
-
Maven 多模块父子工程的实现(含Spring Boot示例)
-
使用spring profile实现多环境切换的简单实现