In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the implementation method of SpringBoot integrating Mybatis Plus multiple data sources". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the implementation method of SpringBoot integrating Mybatis Plus multiple data sources"?
Guide reading
There is such a demand, the online running business, due to the needs of business development, need to redevelop a new system, and so on after the completion of the new system development, the need for seamless docking and switching, the original specific design see the sketch.
Add dependency 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.properties
Server.port=9999spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.master.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.master.username=rootspring.datasource.master.password=rootspring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.slave.jdbc-url=jdbc:mysql://127.0.0.1/test2?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.slave.username=rootspring.datasource.slave.password=root2 creates DataSource in different ways
Master configuration, using druid connection pooling
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")); / / configure initialization size, minimum, maximum dataSource.setMinIdle (10) / / configure initialization size, minimum, maximum dataSource.setMaxActive; / / configure initialization size, minimum, maximum dataSource.setInitialSize (10); / configure time to get connection wait timeout dataSource.setMaxWait (60000); / / configure minimum survival time for a connection in the pool, in millisecond dataSource.setMinEvictableIdleTimeMillis (300000) / / configure how often to detect idle connections that need to be closed in millisecond dataSource.setTimeBetweenEvictionRunsMillis (60000); / / default testWhileIdle=true,testOnBorrow=false,testOnReturn=false dataSource.setValidationQuery ("SELECT 1"); / / execute validationQuery to check whether the connection is valid when applying for a connection (false) / / it is recommended to be configured as true, which does not affect performance and ensures security. DataSource.setTestWhileIdle (true); / / whether to cache preparedStatement, that is, 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 configuration
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@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 ();}}
Be careful
Master and slave scan different mapper package paths!
If you need to specify an .xml file, you need to configure it like this!
@ 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 paging plug-in Settings
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 paging configuration * * @ Author:chenyanbin * / @ Configurationpublic class MybatisPlusPageConfig {/ * the new paging plug-in * * @ return * / @ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor mybatisPlusInterceptor = 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 ();} Startup class @ SpringBootApplication (exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
Start class troubleshooting, automatic assembly, and use our custom multi-data sources!
Demo
Multiple data sources, which also support transactions
Thank you for your reading, the above is the content of "what is the implementation method of SpringBoot integrating Mybatis Plus multiple data sources". After the study of this article, I believe you have a deeper understanding of what is the implementation method of SpringBoot integrating Mybatis Plus multiple data sources, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.