處理tk.mybatis 和 mybatisplus共存不能分頁


緣由:

  新搭建了一個springboot的單體架構,在融合原項目時,由於原項目使用tk.mybatis,新項目使用mybatisplus,所以導致,將老項目遷至新項目時,老項目中的分頁失效。

展示一下模塊架構

 

 

 在infrastructure中使用分頁:

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(AnimalAdminTaskRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<AnimalAdminTaskRecord> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), AnimalAdminTaskRecordDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<AnimalAdminTaskRecord> queryAll(AnimalAdminTaskRecordQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(AnimalAdminTaskRecord.class, criteria));
    }

 在domain和application中使用(分頁不生效

    public ResponseVO page(Page page, Map<String, Object> param) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<UTag> uTagList = uTagMapper.selectBySelective(param);
        Page pages = new Page(page.getPage(), page.getLimit(), uTagMapper.countBySelective(param));
        pages.setItems(uTagList);
        return ResponseVO.success(pages);
    }

添加分頁配置

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MyBatisPlusConfig {
    /*
     * 分頁插件,自動識別數據庫類型
     * 多租戶,請參考官網【插件擴展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /*處理tk.mybatis 和 mybatisplus共存不能分頁的情況*/
    @Bean
    PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        pageInterceptor.setProperties(properties);  // 由此可進入源碼,
        return pageInterceptor;
    }
} 

 重啟項目,問題解決

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM