SpringBoot整合Mybatis Plus多数据源的实现示例

网友投稿 954 2022-11-25

SpringBoot整合Mybatis Plus多数据源的实现示例

SpringBoot整合Mybatis Plus多数据源的实现示例

目录导读添加依赖application.properties2种方式创建DataSourceMaster配置,使用druid连接池Slave配置启动类演示

导读

有一个这样子的需求,线上正在跑的业务,由于业务发展需要,需重新开发一套新系统,等新系统开发完成后,需要无缝对接切换,当初具体设计见草图。

添加依赖

org.projectlombok

lombok

1.18.16

provided

mysql

mysql-connector-java

com.baomidou

mybatis-plus-boot-starter

3.4.0

com.alibaba

druid

1.1.10

application.phttp://roperties

server.port=9999

spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.master.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.master.username=root

spring.datasource.master.password=root

spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.slave.jdbc-url=jdbc:mysql://127.0.0.1/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.slave.username=root

spring.datasource.slave.password=root

2种方式创建DataSource

Master配置,使用druid连接池

import com.alibaba.druid.pool.DruidDataSource;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.core.env.Environment;

import javax.sql.DataSource;

/**

* @Author:chenyanbin

*/

@Configuration

@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "masterSqlSessionFactory")

public class MasterDataSourceConfig {

@Autowired

private Environment env;

@Primary

@Bean(name = "masterDataSource")

// @ConfigurationProperties("spring.datasource.master")

public DataSource masterDataSource() {

// return DataSourceBuilder.create().build();

DruidDataSource dataSource = new DruidDataSource();

dataSource.setUrl(env.getProperty("spring.datasource.master.url"));

dataSource.setUsername(env.getProperty("spring.datasource.master.username"));

dataSource.setPassword(env.getProperty("spring.datasource.master.password"));

dataSource.setDriverClassName(env.getProperty("spring.datasource.master.driver-class-name"));

//配置初始化大小、最小、最大

dataSource.setMinIdle(10);

//配置初始化大小、最小、最大

dataSource.setMaxActive(200);

//配置初始化大小、最小、最大

dataSource.setInitialSize(10);

//配置获取连接等待超时的时间

dataSource.setMaxWait(60000);

//配置一个连接在池中最小生存的时间,单位是毫秒

dataSource.setMinEvictableIdleTimeMillis(300000);

//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

dataSource.setTimeBetweenEvictionRunsMillis(60000);

//默认的testWhileIdle=true,testOnBorrow=false,testOnReturn=false

dataSource.setValidationQuery("SELECT 1");

//申请连接时执行validationQuery检测连接是否有效

dataSource.setTestOnBorrow(false);

//建议配置为true,不影响性能,并且保证安全性。

dataSource.setTestWhileIdle(true);

//是否缓存preparedStatement,也就是PSCache

dataSource.setPoolPreparedStatements(false);

return dataSource;

}

@Bean(name = "masterSqlSessionFactory")

public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {

MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

return sessionFactoryBean.getObject();

}

}

Slave配置

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

import org.apache.ibatis.session.SqlSessionFactory;

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 javax.sql.DataSource;

/**

* @Author:chenyanbin

*/

@Configuration

http://@MapperScan(basePackages = "com.example.demo.mapper2",sqlSessionFactoryRef = "slaveSqlSessionFactory")

public class SlaveDataSourceConfig {

@Bean(name = "slaveDataSource")

@ConfigurationProperties("spring.datasource.slave")

public DataSource slaveDataSource(){

return DataSourceBuilder.create().build();

}

@Bean(name = "slaveSqlSessionFactory")

public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {

MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

return sessionFactoryBean.getObject();

}

}

注意

master和slave扫描不同的mapper包路径!!!!!!

如果需要指定.xml文件,需这样配置!!!

@Bean(name = "masterSqlSessionFactory")

public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {

MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources("classpath:mapper/**/*.xml"));

return sessionFactoryBean.getObject();

}

MybatisPlus分页插件设置

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* MybatisPlus分页配置

*

* @Author:chenyanbin

*/

@Configuration

public class MybatisPlusPageConfig {

/**

* 新版分页插件

*

* @return

*/

@Bean

public MybatisPlusInterceptor mybatisPlusInterceptor() {

MybatisPlusInterceptor mybatisPlusInterceptorarhgcBlnCv = new MybatisPlusInterceptor();

mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());

return mybatisPlusInterceptor;

}

}

@Bean(name = "masterSqlSessionFactory")

public SqlSessionFactory sqlSessionFactory(

@Qualifier("masterDataSource") DataSource dataSource,

@Qualifier("mybatisPlusInterceptor") MybatisPlusInterceptor mybatisPlusInterceptor

) throws Exception {

MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources("classpath:mapper/**/*.xml"));

sessionFactoryBean.setPlugins(mybatisPlusInterceptor);

return sessionFactoryBean.getObject();

}

启动类

@SpringBootApplication(

exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class}

)

启动类上排查,自动装配,使用我们自定义的多数据源!!!

演示

多个数据源,同时也是支持事务的

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用 content-visibility 优化渲染性能
下一篇:react中list的用户,多个元素横着排列同一行显示
相关文章

 发表评论

暂时没有评论,来抢沙发吧~