|
|
@@ -0,0 +1,105 @@
|
|
|
+package com.ydtech.config.mybatisPlusNull;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
|
|
+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.StringPool;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.ibatis.mapping.MappedStatement;
|
|
|
+import org.apache.ibatis.mapping.SqlSource;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static java.util.stream.Collectors.joining;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @version
|
|
|
+ * @author: hxl
|
|
|
+ * @Date: 2024/6/12 9:32
|
|
|
+ * @Description: mybatis-plus 更新空值的公共类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class UpdateWithNullMethod extends AbstractMethod {
|
|
|
+ protected UpdateWithNullMethod() {
|
|
|
+ super("updateWithNull");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
|
|
+ String sql = "<script>\nupdate %s %s where %s=#{%s}\n</script>";
|
|
|
+
|
|
|
+ final List<TableFieldInfo> fieldList = tableInfo.getFieldList();
|
|
|
+ for (TableFieldInfo tableFieldInfo : fieldList) {
|
|
|
+ final Class<? extends TableFieldInfo> aClass = tableFieldInfo.getClass();
|
|
|
+ try {
|
|
|
+ final Field fieldFill = aClass.getDeclaredField("fieldFill");
|
|
|
+ fieldFill.setAccessible(true);
|
|
|
+ fieldFill.set(tableFieldInfo, FieldFill.UPDATE);
|
|
|
+ } catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
+ log.error("获取fieldFill失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String setSql = this.sqlSet(tableInfo);
|
|
|
+ String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
|
|
|
+ log.debug("sqlResult----->{}", sqlResult);
|
|
|
+ SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
|
|
|
+ // 第三个参数必须和rootMapper的自定义方法名一致
|
|
|
+ return this.addUpdateMappedStatement(mapperClass, modelClass, "updateWithNull", sqlSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected String sqlSet(TableInfo table) {
|
|
|
+ String sqlScript = table.getFieldList()
|
|
|
+ .stream().map(i -> this.getSqlSet(i, StringPool.EMPTY)).collect(joining(StringPool.NEWLINE));
|
|
|
+ sqlScript = SqlScriptUtils.convertTrim(sqlScript, "SET", null, null, ",");
|
|
|
+ return sqlScript;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getSqlSet(TableFieldInfo i, String prefix) {
|
|
|
+ String newPrefix = prefix == null ? StringPool.EMPTY : prefix;
|
|
|
+ String column = i.getColumn();
|
|
|
+ String update = i.getUpdate();
|
|
|
+ FieldFill fieldFill = i.getFieldFill();
|
|
|
+ String el = i.getEl();
|
|
|
+
|
|
|
+ // 默认:column=
|
|
|
+ String sqlSet = column + StringPool.EQUALS;
|
|
|
+ if (StringUtils.isNotEmpty(update)) {
|
|
|
+ sqlSet += String.format(update, column);
|
|
|
+ } else {
|
|
|
+ sqlSet += SqlScriptUtils.safeParam(newPrefix +el);
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlSet += StringPool.COMMA;
|
|
|
+ if (fieldFill == FieldFill.UPDATE || fieldFill == FieldFill.INSERT_UPDATE) {
|
|
|
+ // 不进行if包裹
|
|
|
+ return sqlSet;
|
|
|
+ }
|
|
|
+ return convertIf(sqlSet, convertIfProperty(newPrefix, column), i.getUpdateStrategy(),
|
|
|
+ i.getPropertyType().isPrimitive(), StringUtils.isCharSequence(i.getPropertyType()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private String convertIfProperty(String prefix, String property) {
|
|
|
+ return StringUtils.isNotBlank(prefix)
|
|
|
+ ? prefix.substring(0, prefix.length() - 1) + "['" + property + "']" : property;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String convertIf(final String sqlScript, final String property, final FieldStrategy fieldStrategy,
|
|
|
+ boolean isPrimitive, boolean isCharSequence) {
|
|
|
+ if (fieldStrategy == FieldStrategy.NEVER) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (isPrimitive || fieldStrategy == FieldStrategy.IGNORED) {
|
|
|
+ return sqlScript;
|
|
|
+ }
|
|
|
+ if (fieldStrategy == FieldStrategy.NOT_EMPTY && isCharSequence) {
|
|
|
+ return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s !=''", property, property), false);
|
|
|
+ }
|
|
|
+ return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false);
|
|
|
+ }
|
|
|
+}
|