|
@@ -10,16 +10,14 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
|
|
|
-import com.chelvc.framework.common.util.AssertUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
+import com.chelvc.framework.database.context.DatabaseContextHolder;
|
|
|
import com.chelvc.framework.database.entity.Creatable;
|
|
|
import com.chelvc.framework.database.entity.Deletable;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import com.google.common.collect.Sets;
|
|
|
import lombok.NonNull;
|
|
|
-import org.apache.ibatis.reflection.property.PropertyNamer;
|
|
|
|
|
|
/**
|
|
|
* 数据修改处理工具类
|
|
@@ -242,35 +240,8 @@ public final class Updates {
|
|
|
* @param <T> 数据类型
|
|
|
*/
|
|
|
public static class Update<T> {
|
|
|
- private Map<String, ColumnCache> columns;
|
|
|
private final Map<String, String> expressions = Maps.newHashMapWithExpectedSize(3);
|
|
|
|
|
|
- /**
|
|
|
- * 初始化列缓存
|
|
|
- *
|
|
|
- * @param clazz 对象类型
|
|
|
- */
|
|
|
- private void initializeColumnCache(Class<?> clazz) {
|
|
|
- if (this.columns == null) {
|
|
|
- this.columns = LambdaUtils.getColumnMap(clazz);
|
|
|
- }
|
|
|
- AssertUtils.nonnull(this.columns, () -> "Column cache not found: " + clazz);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将属性Getter方法转换成列名称
|
|
|
- *
|
|
|
- * @param getter 属性Getter方法
|
|
|
- * @return 列名称
|
|
|
- */
|
|
|
- private String getter2column(SFunction<T, ?> getter) {
|
|
|
- SerializedLambda lambda = LambdaUtils.resolve(getter);
|
|
|
- this.initializeColumnCache(lambda.getInstantiatedType());
|
|
|
- String property = PropertyNamer.methodToProperty(lambda.getImplMethodName());
|
|
|
- ColumnCache column = this.columns.get(LambdaUtils.formatKey(property));
|
|
|
- return column.getColumn();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 更新所有字段
|
|
|
*
|
|
@@ -289,13 +260,16 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> not(@NonNull Class<T> clazz, @NonNull String... columns) {
|
|
|
- this.initializeColumnCache(clazz);
|
|
|
+ Map<String, ColumnCache> properties = LambdaUtils.getColumnMap(clazz);
|
|
|
+ if (ObjectUtils.isEmpty(properties)) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
TableInfo table = TableInfoHelper.getTableInfo(clazz);
|
|
|
boolean creatable = Creatable.class.isAssignableFrom(clazz);
|
|
|
boolean deletable = Deletable.class.isAssignableFrom(clazz);
|
|
|
String key = StringUtils.ifEmpty(table.getKeyProperty(), LambdaUtils::formatKey);
|
|
|
Set<String> excludes = ObjectUtils.isEmpty(columns) ? Collections.emptySet() : Sets.newHashSet(columns);
|
|
|
- this.columns.forEach((property, column) -> {
|
|
|
+ properties.forEach((property, column) -> {
|
|
|
if (!Objects.equals(property, key)
|
|
|
&& !(creatable && ("CREATOR".equals(property) || "CREATETIME".equals(property)))
|
|
|
&& !(deletable && "DELETED".equals(property)) && !excludes.contains(column.getColumn())) {
|
|
@@ -313,7 +287,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> not(@NonNull Class<T> clazz, @NonNull SFunction<T, ?> getter) {
|
|
|
- return this.not(clazz, this.getter2column(getter));
|
|
|
+ return this.not(clazz, DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -336,7 +310,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> eq(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.eq(this.getter2column(getter));
|
|
|
+ return this.eq(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -359,7 +333,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> add(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.add(this.getter2column(getter));
|
|
|
+ return this.add(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -382,7 +356,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> clear(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.clear(this.getter2column(getter));
|
|
|
+ return this.clear(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -405,7 +379,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> subtract(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.subtract(this.getter2column(getter));
|
|
|
+ return this.subtract(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -428,7 +402,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> multiply(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.multiply(this.getter2column(getter));
|
|
|
+ return this.multiply(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -451,7 +425,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> divide(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.divide(this.getter2column(getter));
|
|
|
+ return this.divide(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -486,7 +460,7 @@ public final class Updates {
|
|
|
* @return 更新对象实例
|
|
|
*/
|
|
|
public Update<T> exclude(@NonNull SFunction<T, ?> getter) {
|
|
|
- return this.exclude(this.getter2column(getter));
|
|
|
+ return this.exclude(DatabaseContextHolder.getter2column(getter));
|
|
|
}
|
|
|
|
|
|
/**
|