MybatisPlus 之 PaginationInterceptor 扩展配置bean

package com.dcits.branch.cloud.tran.configuration;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.dcits.branch.cloud.components.context.ContextManager;
import com.dcits.branch.cloud.components.context.ContextManagerImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.wrapper.BeanWrapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 类功能:MybatisPlus 扩展配置bean
 * oracle 数据源切换 mysql,报错 MySQLSyntaxErrorException: Every derived table must have its own alias
 * 设置注意 paginationInterceptor.setDialectType(DbType.MYSQL.getDb());
 * 
* * @author dcits.xupengfei 2019/3/9 * @desc: MybatisPlus 扩展配置bean * @modify dcits.xupengfei 2020/3/9 初始创建 */ @MapperScan({"com.dcits.branch.cloud.tran.order.dao.mapper", "com.dcits.branch.cloud.tran.prdbusi.dao.mapper"}) @Configuration @Slf4j public class MybatisPlusConfig { private final static String MPTBL_CM_TRAN_JOURNAL = "CM_TRAN_JOURNAL"; @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); List sqlParserList = new ArrayList<>(); // --- 动态表名映射 DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser(); ITableNameHandler myTableNameHandler = (metaObject, sql, tableName) -> { Object objectWrapper = metaObject.getObjectWrapper(); if (objectWrapper instanceof BeanWrapper) { BeanWrapper beanWrapper = (BeanWrapper) metaObject.getObjectWrapper(); if (MPTBL_CM_TRAN_JOURNAL.equals(tableName)) { log.debug("mapping table name :{}", tableName); /* ContextManager contextManager = ContextManagerImpl.getInstanse(); TableNameMapping tableNameMapping = contextManager.getContext(TableNameMapping.class); String suffix = tableNameMapping.getSuffix(tableName);*/ //因update时 ParameterObject对象和insert时不一致,因此需要放弃使用mp的对象,用自定义对象获取表名后缀 /* String delegate = beanWrapper.findProperty("delegate", true); PropertyTokenizer prop2 = new PropertyTokenizer("delegate"); Object obj2 = beanWrapper.get(prop2); PreparedStatementHandler ph = (PreparedStatementHandler) obj2; Object bean = ph.getParameterHandler().getParameterObject(); //获取对象中分表属性 CmTranJournal cmTranJournal = (CmTranJournal) bean; String suffix = cmTranJournal.getBusiType(); */ log.debug("mapping table name result:{}", tableName); return tableName; } } //other table/bean not mapping tablename return null; }; //defined which table to mapping Map tableNameHandlerMap = new HashMap<>(); tableNameHandlerMap.put(MPTBL_CM_TRAN_JOURNAL, myTableNameHandler); dynamicTableNameParser.setTableNameHandlerMap(tableNameHandlerMap); sqlParserList.add(dynamicTableNameParser); paginationInterceptor.setSqlParserList(sqlParserList); paginationInterceptor.setDialectType(DbType.MYSQL.getDb()); log.info("mybatis_plus_extend_plugin(paginationInterceptor): {}" + paginationInterceptor); return paginationInterceptor; } }

 

你可能感兴趣的:(Mybatis)