您好,登錄后才能下訂單哦!
本篇文章為大家展示了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。