com.baomidou.mybatisplus.extension.injector.LogicAbstractMethod Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2020, hubin ([email protected]).
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* 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.baomidou.mybatisplus.extension.injector;
import java.util.List;
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;
/**
*
* 抽象的注入方法类
*
*
* @author hubin
* @since 2018-04-06
*/
public abstract class LogicAbstractMethod extends AbstractMethod {
public LogicAbstractMethod() {
// TO DO NOTHING
}
/**
*
* SQL 更新 set 语句
*
*
* @param table 表信息
* @return sql and 片段
*/
public String getLogicDeleteSql(boolean startWithAnd, TableInfo table) {
StringBuilder sql = new StringBuilder();
List fieldList = table.getFieldList();
for (TableFieldInfo fieldInfo : fieldList) {
if (fieldInfo.isLogicDelete()) {
if (startWithAnd) {
sql.append(" AND ");
}
sql.append(fieldInfo.getColumn());
if (StringUtils.isCharSequence(fieldInfo.getPropertyType())) {
sql.append("='").append(fieldInfo.getLogicNotDeleteValue()).append("'");
} else {
sql.append("=").append(fieldInfo.getLogicNotDeleteValue());
}
}
}
return sql.toString();
}
/**
*
* SQL 更新 set 语句
*
*
* @param table 表信息
* @return sql set 片段
*/
protected String sqlLogicSet(TableInfo table) {
List fieldList = table.getFieldList();
StringBuilder set = new StringBuilder("SET ");
int i = 0;
for (TableFieldInfo fieldInfo : fieldList) {
if (fieldInfo.isLogicDelete()) {
if (++i > 1) {
set.append(",");
}
set.append(fieldInfo.getColumn()).append("=");
if (StringUtils.isCharSequence(fieldInfo.getPropertyType())) {
set.append("'").append(fieldInfo.getLogicDeleteValue()).append("'");
} else {
set.append(fieldInfo.getLogicDeleteValue());
}
}
}
return set.toString();
}
// ------------ 处理逻辑删除条件过滤 ------------
@Override
protected String sqlWhereEntityWrapper(TableInfo table) {
if (table.isLogicDelete()) {
StringBuilder where = new StringBuilder(128);
where.append("");
where.append("");
where.append("");
if (StringUtils.isNotEmpty(table.getKeyProperty())) {
where.append("");
where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.entity.");
where.append(table.getKeyProperty()).append("}");
where.append(" ");
}
List fieldList = table.getFieldList();
for (TableFieldInfo fieldInfo : fieldList) {
where.append(convertIfTag(fieldInfo, "ew.entity.", false));
where.append(" AND ").append(sqlCondition(fieldInfo.getCondition(),
fieldInfo.getColumn(), "ew.entity." + fieldInfo.getEl()));
where.append(convertIfTag(fieldInfo, true));
}
where.append(" ");
// 删除逻辑
where.append(getLogicDeleteSql(true, table));
// SQL 片段
where.append(" AND ${ew.sqlSegment} ");
where.append(" ");
where.append(" WHERE ");
// 删除逻辑
where.append(getLogicDeleteSql(false, table));
where.append(" ");
return where.toString();
}
// 正常逻辑
return super.sqlWhereEntityWrapper(table);
}
@Override
protected String sqlWhereByMap(TableInfo table) {
if (table.isLogicDelete()) {
return new StringBuilder()
.append("")
.append("")
.append("")
.append(" ${k} IS NULL ")
.append(" ${k}=#{v} ")
.append(" ")
.append(" ")
.append(getLogicDeleteSql(true, table))
.append(" ")
.toString();
}
// 正常逻辑
return super.sqlWhereByMap(table);
}
}