woody před 8 měsíci
rodič
revize
4cdbe7261b

+ 1 - 1
framework-common/src/main/java/com/chelvc/framework/common/util/StringUtils.java

@@ -1255,7 +1255,7 @@ public final class StringUtils {
             buffer.append(Character.toUpperCase(original.charAt(begin = delimiter + 1)));
             begin++;
         }
-        if (begin < end - 1) {
+        if (begin < end) {
             buffer.append(original, begin, end);
         }
         return buffer.toString();

+ 9 - 13
framework-database/src/main/java/com/chelvc/framework/database/config/TypeHandlerConfigurer.java

@@ -223,12 +223,9 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
                     }
 
                     // 是否是JSON字段
-                    Class<?> handler = ObjectUtils.ifNull(annotation, TableField::typeHandler);
-                    if (handler == null || handler == UnknownTypeHandler.class || handler == JacksonTypeHandler.class) {
-                        Type type = TypeParameterResolver.resolveFieldType(field, clazz);
-                        if (!this.isTypeHandlerRegistered(ObjectUtils.type2class(type))) {
-                            return true;
-                        }
+                    Type type = TypeParameterResolver.resolveFieldType(field, clazz);
+                    if (!this.isTypeHandlerRegistered(ObjectUtils.type2class(type))) {
+                        return true;
                     }
                 }
             }
@@ -243,7 +240,6 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
      * @param field 表字段信息
      * @return true/false
      */
-    @SuppressWarnings("unchecked")
     private boolean bindTypeHandler(Class<?> clazz, TableFieldInfo field) {
         // 查找字段类型处理器
         Class<? extends TypeHandler<?>> handler =
@@ -254,13 +250,13 @@ public class TypeHandlerConfigurer extends MybatisConfigurer {
 
         // 绑定并注册字段类型处理器
         ObjectUtils.setValue(field, "typeHandler", handler);
-        String el = ObjectUtils.getValue(field, "el") + ",typeHandler=" + handler.getName();
-        ObjectUtils.setValue(field, "el", el);
-        if (!this.isTypeHandlerRegistered(field.getField().getType())) {
-            this.configuration.getTypeHandlerRegistry().register(
-                    (Class<Object>) field.getField().getType(), DatabaseContextHolder.initializeTypeHandler(handler)
-            );
+        String el = (String) ObjectUtils.getValue(field, "el");
+        int delimiter = el.indexOf(',');
+        if (delimiter > 0) {
+            el = el.substring(0, delimiter);
         }
+        el = el + ",typeHandler=" + handler.getName();
+        ObjectUtils.setValue(field, "el", el);
         return true;
     }
 

+ 232 - 7
framework-database/src/main/java/com/chelvc/framework/database/support/Updates.java

@@ -28,6 +28,31 @@ public final class Updates {
     private Updates() {
     }
 
+    /**
+     * 数据更新条件枚举
+     */
+    public enum Condition {
+        /**
+         * 大于
+         */
+        GT,
+
+        /**
+         * 小于
+         */
+        LT,
+
+        /**
+         * 不等于
+         */
+        NE,
+
+        /**
+         * 非空
+         */
+        NONNULL;
+    }
+
     /**
      * 获取字段更新对象
      *
@@ -46,7 +71,19 @@ public final class Updates {
      * @return 字段更新对象实例
      */
     public static <T> Update<T> all(@NonNull Class<T> clazz) {
-        return not(clazz, StringUtils.EMPTY_ARRAY);
+        return all(clazz, null);
+    }
+
+    /**
+     * 更新所有字段
+     *
+     * @param clazz     对象类型
+     * @param condition 更新条件
+     * @param <T>       数据类型
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> all(@NonNull Class<T> clazz, Condition condition) {
+        return not(clazz, condition, StringUtils.EMPTY_ARRAY);
     }
 
     /**
@@ -58,6 +95,19 @@ public final class Updates {
      * @return 字段更新对象实例
      */
     public static <T> Update<T> not(@NonNull Class<T> clazz, @NonNull String... columns) {
+        return not(clazz, null, columns);
+    }
+
+    /**
+     * 更新除指定字段外的所有字段
+     *
+     * @param clazz     对象类型
+     * @param condition 更新条件
+     * @param columns   排除字段数组
+     * @param <T>       数据类型
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> not(@NonNull Class<T> clazz, Condition condition, @NonNull String... columns) {
         Update<T> update = update();
         Map<String, ColumnCache> properties = LambdaUtils.getColumnMap(clazz);
         if (ObjectUtils.isEmpty(properties)) {
@@ -68,7 +118,7 @@ public final class Updates {
         Set<String> excludes = ObjectUtils.isEmpty(columns) ? Collections.emptySet() : Sets.newHashSet(columns);
         properties.forEach((property, column) -> {
             if (!Objects.equals(property, key) && !excludes.contains(column.getColumn())) {
-                update.use(column.getColumn());
+                update.use(condition, column.getColumn());
             }
         });
         return update;
@@ -84,6 +134,20 @@ public final class Updates {
      */
     @SafeVarargs
     public static <T> Update<T> not(@NonNull Class<T> clazz, @NonNull SFunction<T, ?>... getters) {
+        return not(clazz, null, getters);
+    }
+
+    /**
+     * 更新除指定字段外的所有字段
+     *
+     * @param clazz     对象类型
+     * @param condition 更新条件
+     * @param getters   排除Getter方法数组
+     * @param <T>       数据类型
+     * @return 字段更新对象实例
+     */
+    @SafeVarargs
+    public static <T> Update<T> not(@NonNull Class<T> clazz, Condition condition, @NonNull SFunction<T, ?>... getters) {
         if (ObjectUtils.isEmpty(getters)) {
             return not(clazz, StringUtils.EMPTY_ARRAY);
         }
@@ -91,7 +155,7 @@ public final class Updates {
         for (int i = 0; i < getters.length; i++) {
             columns[i] = DatabaseContextHolder.getter2column(getters[i]);
         }
-        return not(clazz, columns);
+        return not(clazz, condition, columns);
     }
 
     /**
@@ -102,8 +166,20 @@ public final class Updates {
      * @return 字段更新对象实例
      */
     public static <T> Update<T> use(@NonNull String... columns) {
+        return use(null, columns);
+    }
+
+    /**
+     * 使用
+     *
+     * @param condition 使用条件
+     * @param columns   列名数组
+     * @param <T>       数据类型
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> use(Condition condition, @NonNull String... columns) {
         Update<T> update = update();
-        return update.use(columns);
+        return update.use(condition, columns);
     }
 
     /**
@@ -115,8 +191,21 @@ public final class Updates {
      */
     @SafeVarargs
     public static <T> Update<T> use(@NonNull SFunction<T, ?>... getters) {
+        return use(null, getters);
+    }
+
+    /**
+     * 使用
+     *
+     * @param condition 使用条件
+     * @param getters   Getter方法数组
+     * @param <T>       数据类型
+     * @return 字段更新对象实例
+     */
+    @SafeVarargs
+    public static <T> Update<T> use(Condition condition, @NonNull SFunction<T, ?>... getters) {
         Update<T> update = update();
-        return update.use(getters);
+        return update.use(condition, getters);
     }
 
     /**
@@ -244,6 +333,57 @@ public final class Updates {
         return update.divide(getters);
     }
 
+    /**
+     * JSON合并
+     *
+     * @param columns 列名数组
+     * @param <T>     数据类型
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> jsonMerge(@NonNull String... columns) {
+        Update<T> update = update();
+        return update.jsonMerge(columns);
+    }
+
+    /**
+     * JSON合并
+     *
+     * @param getters Getter方法数组
+     * @param <T>     数据类型
+     * @return 字段更新对象实例
+     */
+    @SafeVarargs
+    public static <T> Update<T> jsonMerge(@NonNull SFunction<T, ?>... getters) {
+        Update<T> update = update();
+        return update.jsonMerge(getters);
+    }
+
+    /**
+     * JSON长度计算
+     *
+     * @param column 列名
+     * @param <T>    数据类型
+     * @param target 计算长度目标列名
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> jsonLength(@NonNull String column, @NonNull String target) {
+        Update<T> update = update();
+        return update.jsonLength(column, target);
+    }
+
+    /**
+     * JSON长度计算
+     *
+     * @param getter Getter方法
+     * @param target 计算长度目标Getter方法
+     * @param <T>    数据类型
+     * @return 字段更新对象实例
+     */
+    public static <T> Update<T> jsonLength(@NonNull SFunction<T, ?> getter, @NonNull SFunction<T, ?> target) {
+        Update<T> update = update();
+        return update.jsonLength(getter, target);
+    }
+
     /**
      * 设置更新表达式
      *
@@ -272,8 +412,30 @@ public final class Updates {
          * @return 字段更新对象实例
          */
         public final Update<T> use(@NonNull String... columns) {
+            return use(null, columns);
+        }
+
+        /**
+         * 使用
+         *
+         * @param condition 使用条件
+         * @param columns   列名数组
+         * @return 字段更新对象实例
+         */
+        public final Update<T> use(Condition condition, @NonNull String... columns) {
             for (String column : columns) {
-                this.set(column, "VALUES(" + column + ")");
+                String value = "VALUES(" + column + ")";
+                if (condition == Condition.GT) {
+                    this.set(column, "IF(" + value + " > " + column + ", " + value + ", " + column + ")");
+                } else if (condition == Condition.LT) {
+                    this.set(column, "IF(" + value + " < " + column + ", " + value + ", " + column + ")");
+                } else if (condition == Condition.NE) {
+                    this.set(column, "IF(" + value + " != " + column + ", " + value + ", " + column + ")");
+                } else if (condition == Condition.NONNULL) {
+                    this.set(column, "IFNULL(" + value + ", " + column + ")");
+                } else {
+                    this.set(column, value);
+                }
             }
             return this;
         }
@@ -286,8 +448,20 @@ public final class Updates {
          */
         @SafeVarargs
         public final Update<T> use(@NonNull SFunction<T, ?>... getters) {
+            return use(null, getters);
+        }
+
+        /**
+         * 使用
+         *
+         * @param condition 使用条件
+         * @param getters   Getter方法数组
+         * @return 字段更新对象实例
+         */
+        @SafeVarargs
+        public final Update<T> use(Condition condition, @NonNull SFunction<T, ?>... getters) {
             for (SFunction<T, ?> getter : getters) {
-                this.use(DatabaseContextHolder.getter2column(getter));
+                this.use(condition, DatabaseContextHolder.getter2column(getter));
             }
             return this;
         }
@@ -427,6 +601,57 @@ public final class Updates {
             return this;
         }
 
+        /**
+         * JSON合并
+         *
+         * @param columns 列名数组
+         * @return 字段更新对象实例
+         */
+        public final Update<T> jsonMerge(@NonNull String... columns) {
+            for (String column : columns) {
+                this.set(column, "JSON_MERGE_PATCH(VALUES(" + column + "), " + column + ")");
+            }
+            return this;
+        }
+
+        /**
+         * JSON合并
+         *
+         * @param getters Getter方法数组
+         * @return 字段更新对象实例
+         */
+        @SafeVarargs
+        public final Update<T> jsonMerge(@NonNull SFunction<T, ?>... getters) {
+            for (SFunction<T, ?> getter : getters) {
+                this.jsonMerge(DatabaseContextHolder.getter2column(getter));
+            }
+            return this;
+        }
+
+        /**
+         * JSON长度计算
+         *
+         * @param column 列名
+         * @param target 计算长度目标列名
+         * @return 字段更新对象实例
+         */
+        public final Update<T> jsonLength(@NonNull String column, @NonNull String target) {
+            return this.set(column, "JSON_LENGTH(" + target + ")");
+        }
+
+        /**
+         * JSON长度计算
+         *
+         * @param getter Getter方法
+         * @param target 计算长度目标Getter方法
+         * @return 字段更新对象实例
+         */
+        public final Update<T> jsonLength(@NonNull SFunction<T, ?> getter, @NonNull SFunction<T, ?> target) {
+            return this.jsonLength(
+                    DatabaseContextHolder.getter2column(getter), DatabaseContextHolder.getter2column(target)
+            );
+        }
+
         /**
          * 设置更新表达式
          *