文章目录
- 一、架构
- 二、实现过程
- 2.1 第一步:引入依赖pom
- 2.2 第二步:创建application.yml配置
- 2.3 第三步:创建架构的文件夹
- MybatisPlusConfig
- FirstDataSourceConfig
- SecondDataSourceConfig
实现功能,在不同的文件夹使用不同的库
一、架构
二、实现过程
2.1 第一步:引入依赖pom
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
添加扫描的xml
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/*.txt</include>
</includes>
</resource>
</resources>
2.2 第二步:创建application.yml配置
server:
port: 8082
spring:
banner:
location:
classpath: banner.txt
datasource:
first:
username: root
password: root
jdbc-url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/db?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
second:
username: root
password: root
jdbc-url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/db?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
# 配置打印日志common/config/FirstDataSourceConfig.java和SecondDataSourceConfig.java也需要改成StdOutImpl
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
map-underscore-to-camel-case: true
jdbc-type-for-null: 'null'
cache-enabled: false
global-config:
db-config:
id-type: auto
2.3 第三步:创建架构的文件夹
MybatisPlusConfig
package com.admin.ssb_wav.common.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/***
* 分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
FirstDataSourceConfig
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Title: FirstDataSourceConfig
* @Author 代码浪人
* @Date 2024/9/19 17:14
* @description:
*/
@Configuration
@MapperScan(basePackages = "基础包的.mapper.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class FirstDataSourceConfig {
@Autowired
private MybatisPlusConfig mybatisPlusConfig;
@Bean("firstDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource getFirstDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 使用Mybati-plus自带的方法时时必须使用MybatisSqlSessionFactoryBean,用SqlSessionFactoryBean会报错
* Invalid bound statement (not found): com.wyc.cloudblog.mapper.cloudblog.CategoryMapper.selectList
*/
@Bean("firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//添加mybatisplus分页插件,不然分页查询返回值中total为0
bean.setPlugins(mybatisPlusConfig.paginationInterceptor());
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/first/*.xml"));
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
// 配置打印sql语句,如果不配置,多数据源下不会打印
//开发控制台打印
// configuration.setLogImpl(StdOutImpl.class);
configuration.setLogImpl(Log4j2Impl.class);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "firstTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("firstSqlSessionTemplate")
@Primary
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SecondDataSourceConfig
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Title: SecondDataSourceConfig
* @Author 代码浪人
* @Date 2024/9/19 17:14
* @description:
*/
@Configuration
@MapperScan(basePackages = "基础包的.mapper.second", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {
@Autowired
private MybatisPlusConfig mybatisPlusConfig;
@Bean("secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource getSecondDataSource(){
return DataSourceBuilder.create().build();
}
/**
* 使用Mybati-plus自带的方法时时必须使用MybatisSqlSessionFactoryBean,用SqlSessionFactoryBean会报错
* Invalid bound statement (not found): com.wyc.cloudblog.mapper.cloudblog.CategoryMapper.selectList
*/
@Bean("secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//添加mybatisplus分页插件,不然分页查询返回值中total为0
bean.setPlugins(mybatisPlusConfig.paginationInterceptor());
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/second/*.xml"));
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
// 配置打印sql语句,如果不配置,多数据源下不会打印
//开发控制台打印
// configuration.setLogImpl(StdOutImpl.class);
configuration.setLogImpl(Log4j2Impl.class);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}