springboot+mybatis-plus+hikari多数据源配置
.ymal文件针对mysql举例# databasespring:application:name: dipper-webflyway:enabled: falsedatasource:portal:driver-class-name: com.mysql.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql
ymal文件针对mysql举例
# database
spring:
application:
name: testDBs
flyway:
enabled: false
datasource:
data1:
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
jdbc-url: jdbc:mysql:#########
username: #########
password: #########
data2:
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
jdbc-url: jdbc:mysql:###########
username: ########
password: ###########
hikari:
pool-name: Hikari
auto-commit: false
两个DataSource配置:
第一个:
package com.****.****.****.****.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
//@ConditionalOnProperty(prefix = "spring.datasource.apidb", name = "enabled", havingValue = "true")//注解@ConditionalOnProperty,这个注解能够控制某个configuration是否生效,这里的配置不要动,修改在properties文件中,往上看
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.*****.****.*****.****.dao.mapper.data1", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
public class DataSource1Config {
@Value(value = "${mybatis-plus.sql.show}")
private boolean isshowSql;
@Bean("slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.data1")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置slave数据源mapper文件路径
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/data1/*.xml"));
MybatisConfiguration config = new MybatisConfiguration();
// 开启javaBean 成员变量名映射为映射到数据库列名的时候,驼峰命名法到下划线分割命名法则的自动转换功能()
config.setMapUnderscoreToCamelCase(true);
//增加对mybatis自带分页的支持
config.addInterceptor(new PaginationInterceptor());
config.setCacheEnabled(false);
bean.setConfiguration(config);
if(isshowSql) {
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setLogImpl(StdOutImpl.class);
}
return bean.getObject();
}
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
第二个:
DataSource配置和第一个一样,调整下@MapperScan(basePackages = “com..****...dao.mapper.data1", sqlSessionTemplateRef = “slaveSqlSessionTemplate”)中basePackages="com.*****..*****.**.dao.mapper.data2" 为数据源二的mapper路径, 调整xml路径为:bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/data2/.xml”));
重要:
代码中
{MybatisConfiguration config = new MybatisConfiguration();
// 开启javaBean 成员变量名映射为映射到数据库列名的时候,驼峰命名法到下划线分割命名法则的自动转换功能()
config.setMapUnderscoreToCamelCase(true);
config.setCacheEnabled(false);
bean.setConfiguration(config);}
**极其重要!!!!!!!!!!!!**不加该配置 ,牵扯到驼峰命名的table信息都会初始化失败,报错MybatisPlusException: can not find lambda cache;网上说加:TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), “”), XxxxDO.class); 个人认为牵扯到很多表实体类的时候很麻烦,而且加了该处理之后,使用QueryWrapper/LambdaQueryWrapper的原生方法(selectList、selectById、update等) 时会报错“Invalid bound statement (not found)的异常”,加上该配置就OK了
最后,对应要连不同数据源的mapper和xml放在对应的数据源扫描路径下就可以了。
增加对mybatis自带分页的支持(已被坑)
config.addInterceptor(new PaginationInterceptor());
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
第二种方式:
pom文件:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
然后数据库配置:
datasource:
dynamic:
primary: data1
datasource:
data1:
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
url: #########
username: #########
password: ################
hikari:
pool-name: Hikari
auto-commit: false
maximum-pool-size: 20
data2:
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
url: #########
username: #########
password: ################
hikari:
pool-name: Hikari
auto-commit: false
maximum-pool-size: 20
默认的主数据库是data1,在使用data2时只需要在mapper类加上@DS(“data2”)注解即可,比第一种方式更加简洁,且不用单独处理mybatis自带的一些配置,但是第一种方式配置更加自由。
更多推荐
所有评论(0)