Java知识分享网 - 轻松学习从此开始!    

Java知识分享网

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus权限系统实战课程 震撼发布        

最新Java全栈就业实战课程(免费)

springcloud分布式电商秒杀实战课程

IDEA永久激活

66套java实战课程无套路领取

锋哥开始收Java学员啦!

Python学习路线图

锋哥开始收Java学员啦!
当前位置: 主页 > Java文档 > JavaWeb技术相关 >

SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询 PDF 下载


分享到:
时间:2020-10-09 09:09来源:http://www.java1234.com 作者:转载  侵权举报
SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询 PDF 下载
失效链接处理
SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询 PDF 下载


本站整理下载:
提取码:gsh5 
 
 
相关截图:
 
主要内容:

SpringBoot整合mybatis-plus实现多数据源的动态切换且支持分页查询
1、引入Maven依赖
2、添加application.properties配置文件
3、添加DataSourceConfig配置类
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.16</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.6</version> </dependency> #mybatis-plus配置 mybatis-plus.mapper-locations=classpath:mapper/*Mapper.xml mybatis-plus.type-aliases-package=com.example.domain #多数据源的配置 # master DB properties: spring.datasource.url=jdbc:postgresql://localhost:5432/postgres spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver #slave DB properties spring.datasource.master.url=jdbc:oracle:thin:@127.0.0.1:1521/orcl spring.datasource.master.username=system spring.datasource.master.password=system spring.datasource.master.driver-class-name=oracle.jdbc.OracleDriver
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.github.pagehelper.PageInterceptor; import com.hikvision.insurance.datasource.DynamicDataSource; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; @Configuration @MapperScan(basePackages = {"com.example.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory") public class DataSourceConfig { @Value("${spring.datasource.url}") private String defaultDBUrl; @Value("${spring.datasource.username}") private String defaultDBUser; @Value("${spring.datasource.password}") private String defaultDBPassword; @Value("${spring.datasource.driver-class-name}") private String defaultDBDreiverName; @Value("${spring.datasource.master.url}") private String masterDBUrl; @Value("${spring.datasource.master.username}") private String masterDBUser; @Value("${spring.datasource.master.password}") private String masterDBPassword; @Value("${spring.datasource.master.driver-class-name}") private String masterDBDreiverName; @Bean public DynamicDataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance(); HikariDataSource defaultDataSource = new HikariDataSource(); defaultDataSource.setJdbcUrl(defaultDBUrl); defaultDataSource.setUsername(defaultDBUser); defaultDataSource.setPassword(defaultDBPassword); defaultDataSource.setDriverClassName(defaultDBDreiverName); HikariDataSource masterDataSource = new HikariDataSource(); masterDataSource.setJdbcUrl(masterDBUrl); masterDataSource.setUsername(masterDBUser); masterDataSource.setPassword(masterDBPassword); masterDataSource.setDriverClassName(masterDBDreiverName); Map<Object,Object> map = new HashMap<>(); map.put("default", defaultDataSource); map.put("master", masterDataSource); dynamicDataSource.setTargetDataSources(map);
4、添加DynamicDataSource配置类
dynamicDataSource.setDefaultTargetDataSource(defaultDataSource); return dynamicDataSource; } //mybatis-plus分页插件 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } //pagehelper分页插件 @Bean public PageInterceptor pageInterceptor(){ return new PageInterceptor(); }@Bean public SqlSessionFactory sqlSessionFactory(DataSource dynamicDataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dynamicDataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml ")); bean.setPlugins(paginationInterceptor(),pageInterceptor()); return bean.getObject(); } }

 

------分隔线----------------------------

锋哥公众号


锋哥微信


关注公众号
【Java资料站】
回复 666
获取 
66套java
从菜鸡到大神
项目实战课程

锋哥推荐