com.jn.sqlhelper.mybatisplus.plugins.pagination.CustomMybatisPlus3_0_0ParameterHandler Maven / Gradle / Ivy
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the LGPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jn.sqlhelper.mybatisplus.plugins.pagination;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jn.langx.util.Objs;
import com.jn.langx.util.Strings;
import com.jn.langx.util.reflect.Reflects;
import com.jn.langx.util.reflect.type.Primitives;
import com.jn.sqlhelper.mybatis.plugins.CustomMybatisParameterHandler;
import com.jn.sqlhelper.mybatisplus.tableinfo.TableInfoHelpers;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.reflection.MetaObject;
import java.util.*;
/**
* 适用mybatis plus 版本: [3.0.0, 3.2.x]
*/
public class CustomMybatisPlus3_0_0ParameterHandler extends CustomMybatisParameterHandler {
public CustomMybatisPlus3_0_0ParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
super(mappedStatement, parameterObject, boundSql);
}
@Override
protected Object processParameter(Object parameterObject) {
return processBatch(this.mappedStatement,parameterObject);
}
/*
* 此逻辑自定义全局参数填充器 mybatis-plus的逻辑自定义完以后会修改父类的parameterObject.故必须重写setParameters中获取parameterObject方法
* e.g.
* 如果每个表都有一些公共字段创建.就可以使用此逻辑
* 实现MetaObjectHandler 接口即可
*/
public static Object processBatch(MappedStatement ms, Object parameterObject) {
if (null != parameterObject && !Primitives.isPrimitiveOrPrimitiveWrapperType(parameterObject.getClass()) && parameterObject.getClass() != String.class) {
GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(ms.getConfiguration());
MetaObjectHandler metaObjectHandler = globalConfig.getMetaObjectHandler();
boolean isFill = false;
boolean isInsert = false;
if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
isFill = true;
isInsert = true;
} else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler != null && metaObjectHandler.openUpdateFill()) {
isFill = true;
}
if (isFill) {
Collection