更新数据支持忽略乐观锁,适用于不需要锁的场景
This commit is contained in:
@@ -58,6 +58,7 @@
|
|||||||
| `insert(Model)` → `Model` | 插入并返回带 GUID 的记录 |
|
| `insert(Model)` → `Model` | 插入并返回带 GUID 的记录 |
|
||||||
| `batchInsert(List<Model>)` → `List<Model>` | 批量插入并返回记录列表 |
|
| `batchInsert(List<Model>)` → `List<Model>` | 批量插入并返回记录列表 |
|
||||||
| `update(Model)` → `int` | 按主键更新(支持乐观锁) |
|
| `update(Model)` → `int` | 按主键更新(支持乐观锁) |
|
||||||
|
| `updateIgnoreLock(Model)` → `int` | 按主键更新(忽略乐观锁) |
|
||||||
| `updateByExampleSelective(Model, Example)` → `int` | 按条件选择性更新 |
|
| `updateByExampleSelective(Model, Example)` → `int` | 按条件选择性更新 |
|
||||||
|
|
||||||
### TapeViewRepositoryGeneratorPlugin
|
### TapeViewRepositoryGeneratorPlugin
|
||||||
|
|||||||
Binary file not shown.
@@ -335,7 +335,16 @@ public class TapeTableRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
updateMethod.setAbstract(true);
|
updateMethod.setAbstract(true);
|
||||||
repositoryInterface.addMethod(updateMethod);
|
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");
|
Method updateByExampleSelectiveMethod = new Method("updateByExampleSelective");
|
||||||
updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
|
updateByExampleSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
updateByExampleSelectiveMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||||
@@ -393,6 +402,7 @@ public class TapeTableRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
generateInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName);
|
generateInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName);
|
||||||
generateBatchInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName);
|
generateBatchInsertMethod(implClass, tableName, introspectedTable, modelClassName, mapperFieldName);
|
||||||
generateUpdateMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
generateUpdateMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
|
generateUpdateIgnoreLockMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
generateUpdateByExampleSelectiveMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
generateUpdateByExampleSelectiveMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
generateDeleteByIdMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
|
generateDeleteByIdMethod(implClass, tableName, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
|
||||||
generateDeleteAllMethod(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);
|
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<String, Object[]> 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,
|
private void generateUpdateByExampleSelectiveMethod(TopLevelClass implClass, String tableName, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName,
|
||||||
String mapperFieldName, boolean hasBLOBColumns) {
|
String mapperFieldName, boolean hasBLOBColumns) {
|
||||||
Method method = new Method("updateByExampleSelective");
|
Method method = new Method("updateByExampleSelective");
|
||||||
|
|||||||
Reference in New Issue
Block a user