本篇文章为大家展示了IPage在MybatisPlus中出现失效如何解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
@Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }
我们随便自定义一个类即可,这里主要是将这个类作为一个bean交给spring容器管理。
@Bean(name="sessionFactory") public SqlSessionFactory sessionFactory( @Qualifier("bigDataDataSource") DataSource bigDataDataSource, @Qualifier("branchDataSource") DataSource branchDataSource, @Qualifier("basicDataSource") DataSource basicDataSource, org.apache.ibatis.session.Configuration config) throws Exception{ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); //构造方法,解决动态数据源循环依赖问题。 MybatisConfiguration configuration = new MybatisConfiguration(); configuration.addInterceptor(new PaginationInterceptor()); sessionFactoryBean.setConfiguration(configuration); sessionFactoryBean.setConfiguration(config); sessionFactoryBean.setDataSource(this.DataSource(bigDataDataSource,branchDataSource, basicDataSource)); return sessionFactoryBean.getObject(); }
至此,我们的mybatisplus的分页插件就好使了。下面给大家提供MyBatis多数据源的解决方案。
我们项目使用的是yml形式的配置文件,采用的是hikari的数据库连接池。第一步我们自然是配置多个数据库源头。
我们找到spring的datasource,在下方配置三个数据源。
spring: application: name: dynamicDatasource datasource: test1: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 test2: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 test3: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test3?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false username: root password: 123456 hikari: leak-detection-threshold: 2000
我们可以建立个datasourceBean文件夹专门管理数据源的实体类。
我们这里要建立三个实体类。分别对应test1,test2,test3
@Configuration public class Test1DataSourceBean { @Value("${spring.datasource.test1.driver-class-name}") private String test1Driver; @Value("${spring.datasource.test1.url}") private String test1Url; @Value("${spring.datasource.test1.username}") private String test1Username; @Value("${spring.datasource.test1.password}") private String test1Password; @Bean(name="test1DataSource") public DataSource test1DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test1Driver); dataSource.setJdbcUrl(test1Url); dataSource.setUsername(test1Username); dataSource.setPassword(test1Password); return dataSource; } } @Configuration public class Test2DataSourceBean { @Value("${spring.datasource.test2.driver-class-name}") private String test2Driver; @Value("${spring.datasource.test2.url}") private String test2Url; @Value("${spring.datasource.test2.username}") private String test2Username; @Value("${spring.datasource.test2.password}") private String test2Password; @Bean(name="test2DataSource") public DataSource test2DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test2Driver); dataSource.setJdbcUrl(test2Url); dataSource.setUsername(test2Username); dataSource.setPassword(test2Password); return dataSource; } } @Configuration public class Test3DataSourceBean { @Value("${spring.datasource.test3.driver-class-name}") private String test3Driver; @Value("${spring.datasource.test3.url}") private String test3Url; @Value("${spring.datasource.test3.username}") private String test3Username; @Value("${spring.datasource.test3.password}") private String test3Password; @Bean(name="test3DataSource") public DataSource test3DataSource() throws Exception{ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName(test3Driver); dataSource.setJdbcUrl(test3Url); dataSource.setUsername(test3Username); dataSource.setPassword(test3Password); return dataSource; } }
public enum DatabaseType { test1("test1", "test1"), test2("test2", "test2"), test3("test3","test3"); private String name; private String value; DatabaseType(String name, String value){ this.name = name; this.value = value; } public String getName(){ return name; } public String getValue(){ return value; } }
public class DatabaseContextHolder { private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<>(); public static void setDatabaseType(DatabaseType type){ contextHolder.set(type); } public static DatabaseType getDatabaseType(){ return contextHolder.get(); } }
public class DynamicDataSource extends AbstractRoutingDataSource{ protected Object determineCurrentLookupKey() { return DatabaseContextHolder.getDatabaseType(); } }
网上的很多文章配置出来都会产生数据源循环依赖的问题,这里解决了这个问题。
@Configuration @MapperScan(basePackages="cn.test.jichi", sqlSessionFactoryRef="sessionFactory") public class MybatisConfig { /** * @Description:设置动态数据源 */ @Bean(name="dynamicDataSource") @Primary public DynamicDataSource DataSource( @Qualifier("test1DataSource") DataSource test1DataSource, @Qualifier("test2DataSource") DataSource test2DataSource, @Qualifier("test3DataSource") DataSource test3DataSource){ Map<Object, Object> targetDataSource = new HashMap<>(); targetDataSource.put(DatabaseType.test1, test1DataSource); targetDataSource.put(DatabaseType.test2, test2DataSource); targetDataSource.put(DatabaseType.test3, test3DataSource); DynamicDataSource dataSource = new DynamicDataSource(); dataSource.setTargetDataSources(targetDataSource); dataSource.setDefaultTargetDataSource(test1DataSource); return dataSource; } /** * @Description:根据动态数据源创建sessionFactory */ @Bean(name="sessionFactory") public SqlSessionFactory sessionFactory( @Qualifier("test1DataSource") DataSource test1DataSource, @Qualifier("test2DataSource") DataSource test2DataSource, @Qualifier("test3DataSource") DataSource test3DataSource) throws Exception{ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); //构造方法,解决动态数据源循环依赖问题。 sessionFactoryBean.setDataSource(this.DataSource(test1DataSource,test2DataSource, test3DataSource)); return sessionFactoryBean.getObject(); } }
public void testDymnaicDatasource(){ //不切换数据源默认是自己的。 System.out.println("-----默认数据源"); DemoEntity totalCount = demoMapper.getTotalCount(); String nameCount1 = totalCount.getNameCount(); String ageCount2 = totalCount.getAgeCount(); System.out.println("nameCount:"+nameCount1); System.out.println("ageCount:"+ageCount2); //数据源切换为branch System.out.println("-----数据源为test2"); DynamicDataSourceUtils.chooseBranchDataSource(); Integer nameCount = demoMapper.getNameCount(); Integer ageCount = demoMapper.getAgeCount(); System.out.println("nameCount:"+nameCount); System.out.println("ageCount:"+ageCount); //数据源为basic System.out.println("-----数据源为test3"); DynamicDataSourceUtils.chooseBasicDataSource(); Integer ageCount1 = demoMapper.getAgeCount(); System.out.println("ageCount:"+ageCount1); }
上述内容就是IPage在MybatisPlus中出现失效如何解决,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。