diff --git a/README.md b/README.md index a417e32..75fcd63 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ | `insert(Model)` → `Model` | 插入并返回带 GUID 的记录 | | `batchInsert(List)` → `List` | 批量插入并返回记录列表 | | `update(Model)` → `int` | 按主键更新(支持乐观锁) | +| `updateIgnoreLock(Model)` → `int` | 按主键更新(忽略乐观锁) | | `updateByExampleSelective(Model, Example)` → `int` | 按条件选择性更新 | ### TapeViewRepositoryGeneratorPlugin diff --git a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar index d120025..28589ed 100644 Binary files a/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar and b/releases/tape-mybatis-generator-plugin-1.0-SNAPSHOT.jar differ diff --git a/src/main/java/com/iqudoo/framework/mybatis/TapeTableRepositoryGeneratorPlugin.java b/src/main/java/com/iqudoo/framework/mybatis/TapeTableRepositoryGeneratorPlugin.java index f118f1e..840efcd 100644 --- a/src/main/java/com/iqudoo/framework/mybatis/TapeTableRepositoryGeneratorPlugin.java +++ b/src/main/java/com/iqudoo/framework/mybatis/TapeTableRepositoryGeneratorPlugin.java @@ -335,7 +335,16 @@ public class TapeTableRepositoryGeneratorPlugin extends PluginAdapter { updateMethod.setAbstract(true); repositoryInterface.addMethod(updateMethod); - // 15. updateByExampleSelective + // 15. updateIgnoreLock + Method updateIgnoreLockMethod = new Method("updateIgnoreLock"); + updateIgnoreLockMethod.setVisibility(JavaVisibility.PUBLIC); + updateIgnoreLockMethod.setReturnType(new FullyQualifiedJavaType("int")); + updateIgnoreLockMethod.addParameter(new Parameter(new FullyQualifiedJavaType(modelClassName), "record")); + updateIgnoreLockMethod.addException(new FullyQualifiedJavaType("Throwable")); + updateIgnoreLockMethod.setAbstract(true); + repositoryInterface.addMethod(updateIgnoreLockMethod); + + // 16. updateByExampleSelective Method updateByExampleSelectiveMethod = new Method("updateByExampleSelective"); updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC); updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int")); @@ -393,6 +402,7 @@ public class TapeTableRepositoryGeneratorPlugin extends PluginAdapter { generateInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName); generateBatchInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName); generateUpdateMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); + generateUpdateIgnoreLockMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); generateUpdateByExampleSelectiveMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns); generateDeleteByIdMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); generateDeleteAllMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName); @@ -742,6 +752,69 @@ public class TapeTableRepositoryGeneratorPlugin extends PluginAdapter { implClass.addMethod(method); } + private void generateUpdateIgnoreLockMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, + String mapperFieldName, boolean hasBLOBColumns) { + Method method = new Method("updateIgnoreLock"); + method.addAnnotation("@Override"); + method.setVisibility(JavaVisibility.PUBLIC); + method.setReturnType(new FullyQualifiedJavaType("int")); + method.addParameter(new Parameter(new FullyQualifiedJavaType(modelClassName), "record")); + method.addException(new FullyQualifiedJavaType("Throwable")); + + method.addBodyLine(modelClassName + " aDo"); + method.addBodyLine(" = " + mapperFieldName + ".selectByPrimaryKey(record.getGuid());"); + method.addBodyLine("if (aDo == null || aDo.getIsDelete() == 1) {"); + method.addBodyLine("throw new Throwable(\"Record not found, " + modelClassName + " GUID:\" + record.getGuid());"); + method.addBodyLine("}"); + if (isChangeLogEnable(introspectedTable)) { + method.addBodyLine("Map changeDiff = new HashMap<>();"); + } + for (IntrospectedColumn column : introspectedTable.getAllColumns()) { + String fieldName = column.getJavaProperty(); + String setterMethod = "set" + upperFirst(fieldName); + String getterMethod = "get" + upperFirst(fieldName); + if ("guid".equals(fieldName) || "isDelete".equals(fieldName) || "isHidden".equals(fieldName) + || "deleteToken".equals(fieldName) || "dataVersion".equals(fieldName) + || "createTime".equals(fieldName) || "updateTime".equals(fieldName)) { + continue; + } + method.addBodyLine("if (record." + getterMethod + "() != null) {"); + if (isChangeLogEnable(introspectedTable)) { + method.addBodyLine("if (!Objects.equals(record." + getterMethod + "(), aDo." + getterMethod + "())) {"); + method.addBodyLine("changeDiff.put(\"" + fieldName + "\", new Object[]{aDo." + getterMethod + "(), record." + getterMethod + "()});"); + method.addBodyLine("}"); + } + method.addBodyLine("aDo." + setterMethod + "(record." + getterMethod + "());"); + method.addBodyLine("}"); + } + method.addBodyLine(exampleClassName + " updateWhere = new " + exampleClassName + "();"); + method.addBodyLine("updateWhere.createCriteria()"); + method.addBodyLine(" .andGuidEqualTo(aDo.getGuid());"); + method.addBodyLine("aDo.setUpdateTime(new Date());"); + method.addBodyLine("// update data version"); + method.addBodyLine("record.setUpdateTime(aDo.getUpdateTime());"); + + method.addBodyLine("long startTime = new Date().getTime();"); + String updateMethod = hasBLOBColumns ? "updateByExampleWithBLOBs" : "updateByExample"; + method.addBodyLine("int update = " + mapperFieldName + "." + updateMethod + "(aDo, updateWhere);"); + method.addBodyLine("long useTime = new Date().getTime() - startTime;"); + method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {"); + method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] update " + modelClassName + " long time\" +"); + method.addBodyLine(" \"\\n\\t|-> table name: " + tableName + "\" +"); + method.addBodyLine(" \"\\n\\t|-> use time: \" + useTime + \"ms\" +"); + method.addBodyLine(" \"\\n\\t|-----------------------------------\""); + method.addBodyLine(");"); + method.addBodyLine("}"); + if (isChangeLogEnable(introspectedTable)) { + method.addBodyLine("if (update > 0 && !changeDiff.isEmpty()) {"); + method.addBodyLine(changeLogContextClassName + ".addLog(\"" + tableName + "\","); + method.addBodyLine(" \"update\", aDo.getGuid(), changeDiff);"); + method.addBodyLine("}"); + } + method.addBodyLine("return update;"); + implClass.addMethod(method); + } + private void generateUpdateByExampleSelectiveMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName, boolean hasBLOBColumns) { Method method = new Method("updateByExampleSelective");