您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實現(xiàn)方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實現(xiàn)方法是什么”吧!
有一個這樣子的需求,線上正在跑的業(yè)務(wù),由于業(yè)務(wù)發(fā)展需要,需重新開發(fā)一套新系統(tǒng),等新系統(tǒng)開發(fā)完成后,需要無縫對接切換,當(dāng)初具體設(shè)計見草圖。
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <!--scope=provided,說明它是在編譯階段生效,不需要打入包中,Lombok在編譯期將帶Lombok注解的Java文件正確編譯為完整的Class文件--> <scope>provided</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis plus和spring boot整合--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
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
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); //配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(60000); //默認(rèn)的testWhileIdle=true,testOnBorrow=false,testOnReturn=false dataSource.setValidationQuery("SELECT 1"); //申請連接時執(zhí)行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(); } }
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(); } }
注意
master和slave掃描不同的mapper包路徑?。。。。?!
如果需要指定.xml文件,需這樣配置!?。?/p>
@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分頁插件設(shè)置
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 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(); }
@SpringBootApplication( exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class} )
啟動類上排查,自動裝配,使用我們自定義的多數(shù)據(jù)源?。?!
多個數(shù)據(jù)源,同時也是支持事務(wù)的
感謝各位的閱讀,以上就是“SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實現(xiàn)方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實現(xiàn)方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。