Skip to content

Commit

Permalink
feat: 支持全局逻辑删除字段
Browse files Browse the repository at this point in the history
  • Loading branch information
huayanYu committed Sep 20, 2019
1 parent ff0418d commit 66d8536
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public static class DbConfig {
* 表主键生成器
*/
private IKeyGenerator keyGenerator;
/**
* 逻辑删除全局字段 (默认无 设置会自动扫描实体字段)
*/
private String logicDeleteField;
/**
* 逻辑删除全局值(默认 1、表示已删除)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,30 @@
*/
package com.baomidou.mybatisplus.core.metadata;

import com.baomidou.mybatisplus.annotation.*;
import java.lang.reflect.Field;

import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.type.UnknownTypeHandler;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.type.UnknownTypeHandler;

import java.lang.reflect.Field;

/**
* 数据库表字段反射信息
Expand Down Expand Up @@ -274,6 +281,13 @@ private boolean initLogicDelete(GlobalConfig.DbConfig dbConfig, Field field) {
this.logicDeleteValue = dbConfig.getLogicDeleteValue();
}
return true;
} else {
String globalLogicDeleteField = dbConfig.getLogicDeleteField();
if (StringUtils.isNotEmpty(globalLogicDeleteField) && globalLogicDeleteField.equalsIgnoreCase(field.getName())) {
this.logicNotDeleteValue = dbConfig.getLogicNotDeleteValue();
this.logicDeleteValue = dbConfig.getLogicDeleteValue();
return true;
}
}
return false;
}
Expand Down Expand Up @@ -421,7 +435,7 @@ public String getSqlWhere(final String prefix) {
*/
ResultMapping getResultMapping(final MybatisConfiguration configuration) {
ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property,
StringUtils.getTargetColumn(column), propertyType);
StringUtils.getTargetColumn(column), propertyType);
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
if (jdbcType != null && jdbcType != JdbcType.UNDEFINED) {
builder.jdbcType(jdbcType);
Expand Down Expand Up @@ -454,7 +468,7 @@ private String convertIf(final String sqlScript, final String property, final Fi
}
if (fieldStrategy == FieldStrategy.NOT_EMPTY && isCharSequence) {
return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s != ''", property, property),
false);
false);
}
return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false);
}
Expand Down

0 comments on commit 66d8536

Please sign in to comment.