All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jeffreyning.mybatisplus.base.UpdateByMultiIdMethod Maven / Gradle / Ivy

The newest version!
package com.github.jeffreyning.mybatisplus.base;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.jeffreyning.mybatisplus.anno.MppMultiId;
import com.github.jeffreyning.mybatisplus.util.CheckId;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ninghao
 */
public class UpdateByMultiIdMethod extends AbstractMethod {
    private static final Logger logger = LoggerFactory.getLogger(UpdateByMultiIdMethod.class);
    //for mp 5.3.5.1
    public UpdateByMultiIdMethod(){
        super("updateByMultiId");
    }


    private String getCol(List fieldList, String attrName){
        for(TableFieldInfo tableFieldInfo: fieldList){
            String prop=tableFieldInfo.getProperty();
            if(prop.equals(attrName)){
                return tableFieldInfo.getColumn();
            }
        }
        throw new RuntimeException("not found column for "+attrName);
    }

    private String createWhere(Class modelClass, TableInfo tableInfo){
        List fieldList=tableInfo.getFieldList();
        Map idMap=new HashMap();
        for(TableFieldInfo fieldInfo: fieldList){
            Field field=fieldInfo.getField();
            MppMultiId mppMultiId= field.getAnnotation(MppMultiId.class);
            if(mppMultiId!=null){
                String attrName=field.getName();
                String colName=getCol(fieldList, attrName);
                idMap.put(attrName, colName);
            }
        }
        //add 1.7.2
        CheckId.appendIdColum(modelClass,tableInfo,idMap);
        if(idMap.isEmpty()){
            logger.info("entity {} not contain MppMultiId anno", modelClass.getName());
            return null;
        }
        StringBuilder sb=new StringBuilder("");
        idMap.forEach((attrName, colName)->{
            if(sb.length() >0){
                sb.append(" and ");
            }
            sb.append(colName).append("=").append("#{et.").append(attrName).append("}");
        });
        return sb.toString();
    }
    public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
        String cWhere=createWhere(modelClass, tableInfo);
        if(cWhere==null){
            return null;
        }
        String methodName="updateByMultiId";
        boolean logicDelete = tableInfo.isLogicDelete();
        String sqlTemp="";
        String additional = this.optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
        String sql = String.format(sqlTemp, tableInfo.getTableName(), this.sqlSet(logicDelete, false, tableInfo, false, "et", "et."), additional);
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy