支持表级配置

This commit is contained in:
iqudoo
2026-04-17 13:15:49 +08:00
parent ac547d2a57
commit a73e969431
5 changed files with 317 additions and 163 deletions

View File

@@ -1,4 +1,5 @@
# tape-mybatis-generator-plugin
> MyBatis 代码生成插件
## 功能特性
@@ -12,17 +13,22 @@
## 插件说明
### TapeMybatisGeneratorPlugin
扩展 MyBatis Mapper添加以下功能
- 在 Mapper 接口中添加 `selectPrimaryKeyByExample` 方法
- 在 Mapper XML 中生成对应的 SQL 查询语句
### TapeRepositoryGeneratorPlugin
为非视图表生成 Repository 层代码:
- **接口位置**: `{facadeRepositoryPackage}.I{TableName}Repository`
- **实现类位置**: `{domainRepositoryPackage}.{TableName}RepositoryImpl`
- **视图表过滤**: 根据 `viewKeyWords` 配置自动识别并跳过视图表
**生成的方法**
- `trashById(long id)` - 移动到回收站(单个)
- `trashAll({Example} example)` - 移动到回收站(批量)
- `deleteById(long id, boolean release)` - 删除(单个,支持物理删除)
@@ -46,20 +52,25 @@
- `update({Model} record)` - 更新记录(支持乐观锁)
### TapeRepoviewGeneratorPlugin
为视图表生成 RepoView 层代码:
- **接口位置**: `{facadeRepoviewPackage}.I{TableName}Repo`
- **实现类位置**: `{domainRepoviewPackage}.{TableName}RepoImpl`
- **视图表识别**: 仅处理包含 `viewKeyWords` 关键字的表
**生成的方法**
- `findOne({Example} example)` - 查找单条记录
- `getList({Example} example)` - 获取记录列表(支持分页)
- `count({Example} example)` - 统计记录数
### 其他功能
通过 `TapeMybatisGeneratorPlugin` 为所有 Example 类添加支持:
**添加的字段**
- `offset` - 偏移量
- `rows` - 每页数量
- `startPageNum` - 最小页码(默认 1
@@ -68,6 +79,7 @@
- `withBLOBs` - 是否返回BLOBs列的数据
**添加的方法**
- `limit(int rows)` - 设置每页数量
- `limit(int offset, int rows)` - 设置偏移量和每页数量
- `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset
@@ -79,9 +91,11 @@
- `getRows()` - 获取当前分页limit的rows
## 使用方法
### 1. 在 `pom.xml` 中配置插件
```xml
<build>
<finalName>application</finalName>
<plugins>
@@ -162,7 +176,7 @@
### 3. 配置参数说明
| 参数名 | 说明 | 默认值 | 必需 |
|--------------------------------|-------------------------------|---------------------------------------------------------|------|
|--------------------------------|-------------------------------|---------------------------------------------------------|----|
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
@@ -182,9 +196,45 @@
| `startPageNum` | 分页开始页码 | `1` | 否 |
| `maxPageSize` | 最大每页数量 | `100` | 否 |
### 3. TABLE级配置未配置时使用全局配置
| 参数名 |
|------------------------|
| `changeLogEnable` |
| `slowQueryLoggerTime` |
| `slowQueryLoggerLevel` |
| `optimisticLockEnable` |
| `ignorePageSize` |
| `startPageNum` |
| `maxPageSize` |
```xml
<!-- your table -->
<table tableName="your table name"
domainObjectName="YourTableName"
enableInsert="true"
enableDeleteByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
<property name="changeLogEnable" value="false"/>
<property name="slowQueryLoggerTime" value="200"/>
<property name="slowQueryLoggerLevel" value="warn"/>
<property name="optimisticLockEnable" value="false"/>
<property name="ignorePageSize" value="10000"/>
<property name="startPageNum" value="1"/>
<property name="maxPageSize" value="100"/>
</table>
```
## 变更日志监听
ChangeLogContext应该提供以下实现的静态方法供Repository实现中调用
```java
public class ChangeLogContext {
@@ -206,7 +256,8 @@ public class ChangeLogContext {
```mysql
DROP TABLE IF EXISTS `your_table_name`;
CREATE TABLE `your_table_name` (
CREATE TABLE `your_table_name`
(
`guid` bigint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'GUID',
-- ----------------------------
-- add your table other table field
@@ -225,11 +276,13 @@ CREATE TABLE `your_table_name` (
-- 其他参与排序字段,
-- `create_time` desc,
-- `guid`) USING BTREE
KEY `idx_common_query` (`is_hidden`,`is_delete`,`create_time` desc,`guid`) USING BTREE
) ENGINE = InnoDB COMMENT = '你的表格备注' ROW_FORMAT = Dynamic;
KEY `idx_common_query` (`is_hidden`, `is_delete`, `create_time` desc, `guid`) USING BTREE
) ENGINE = InnoDB COMMENT = '你的表格备注'
ROW_FORMAT = Dynamic;
```
**必需字段说明**
- `guid` - 主键,类型为 `bigint UNSIGNED`
- `is_hidden` - 隐藏标志,用于回收站功能
- `is_delete` - 删除标志,用于软删除功能
@@ -258,31 +311,16 @@ CREATE TABLE `your_table_name` (
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<!-- 视图表配置(表名包含 VIEW_ 或 V_ 前缀,大小写不敏感) -->
<table tableName="v_your_view_name"
domainObjectName="ViewYourViewName"
enableInsert="false"
enableDeleteByPrimaryKey="false"
enableUpdateByPrimaryKey="false"
enableCountByExample="true"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
<generatedKey column="guid" sqlStatement="JDBC" identity="false"/>
</table>
```
## 注意事项
## 非视图表注意事项
1. **主键要求**:表必须有一个主键字段,且字段名为 `guid`,类型为 `bigint UNSIGNED`
2. **视图表识别**:视图表通过表名中的关键字识别,默认关键字为 `view_``v_` 不区分大小写
3. **分页功能**:所有 Example 类都自动包含分页功能,可通过 `usePage()` 方法使用
4. **乐观锁**:更新操作使用 `data_version` 字段实现乐观锁,更新时需要传入正确的版本号
5. **软删除**:删除操作默认是软删除(设置 `is_delete` 标志),可通过 `release=true` 参数执行物理删除
6. **回收站**:通过 `is_hidden` 字段实现回收站功能,`trash` 方法将记录移动到回收站,`recover` 方法恢复记录
7. **自动字段**:插入记录时,插件会自动设置 `guid`(使用雪花算法)、`is_delete``is_hidden``delete_token``data_version``create_time``update_time` 等字段
7. **自动字段**:插入记录时,插件会自动设置 `guid`
(使用雪花算法)、`is_delete``is_hidden``delete_token``data_version``create_time``update_time` 等字段
8. **BLOB 字段支持**:如果表包含 BLOB 字段,插件会自动使用 `selectByExampleWithBLOBs``updateByExampleWithBLOBs` 方法
9. **MyBatis Generator 版本**:本插件基于 MyBatis Generator 1.4.1 开发,建议使用 1.4.0 或更高版本

View File

@@ -11,6 +11,7 @@ import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import java.util.List;
@@ -54,6 +55,51 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
}
}
private int getIgnorePageSize(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String getIgnorePageSize = tableConfiguration.getProperty("ignorePageSize");
if (getIgnorePageSize != null && !getIgnorePageSize.isEmpty()) {
int value = Integer.parseInt(getIgnorePageSize);
if (value > 0) {
return value;
}
}
} catch (Throwable ignored) {
}
return ignorePageSize;
}
public int getMaxPageSize(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String getMaxPageSize = tableConfiguration.getProperty("maxPageSize");
if (getMaxPageSize != null && !getMaxPageSize.isEmpty()) {
int value = Integer.parseInt(getMaxPageSize);
if (value > 0) {
return value;
}
}
} catch (Throwable ignored) {
}
return maxPageSize;
}
public int getStartPageNum(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String getStartPageNum = tableConfiguration.getProperty("startPageNum");
if (getStartPageNum != null && !getStartPageNum.isEmpty()) {
int value = Integer.parseInt(getStartPageNum);
if (value > 0) {
return value;
}
}
} catch (Throwable ignored) {
}
return startPageNum;
}
@SuppressWarnings("DuplicatedCode")
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
@@ -65,7 +111,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
"maxPageSize",
JavaVisibility.PROTECTED,
integerType,
this.maxPageSize + ""
getMaxPageSize(introspectedTable) + ""
);
topLevelClass.addField(maxPageSizeField);
@@ -73,7 +119,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
"ignorePageSize",
JavaVisibility.PROTECTED,
integerType,
this.ignorePageSize + ""
getIgnorePageSize(introspectedTable) + ""
);
topLevelClass.addField(ignorePageSizeField);
@@ -81,7 +127,7 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
"startPageNum",
JavaVisibility.PROTECTED,
integerType,
startPageNum + ""
getStartPageNum(introspectedTable) + ""
);
topLevelClass.addField(startPageNumField);

View File

@@ -6,6 +6,7 @@ import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import java.io.File;
@@ -66,10 +67,19 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
modelPackage = stringConfig("modelPackage", modelPackage);
}
private boolean isChangeLogEnable() {
private boolean isChangeLogEnable(IntrospectedTable introspectedTable) {
String tableChangeLogEnable = changeLogEnable;
try {
boolean b = Boolean.parseBoolean(changeLogEnable);
if (b && changeLogContextClassName != null && !changeLogContextClassName.isEmpty()) {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String getChangeLogEnable = tableConfiguration.getProperty("changeLogEnable");
if (getChangeLogEnable != null && !getChangeLogEnable.isEmpty()) {
tableChangeLogEnable = getChangeLogEnable;
}
} catch (Throwable ignored) {
}
try {
boolean b = Boolean.parseBoolean(tableChangeLogEnable);
if (b) {
return true;
}
} catch (Throwable ignored) {
@@ -77,9 +87,18 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
return false;
}
private boolean isOptimisticLockEnable() {
private boolean isOptimisticLockEnable(IntrospectedTable introspectedTable) {
String tableOptimisticLockEnable = optimisticLockEnable;
try {
boolean b = Boolean.parseBoolean(optimisticLockEnable);
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String getOptimisticLockEnable = tableConfiguration.getProperty("optimisticLockEnable");
if (getOptimisticLockEnable != null && !getOptimisticLockEnable.isEmpty()) {
tableOptimisticLockEnable = getOptimisticLockEnable;
}
} catch (Throwable ignored) {
}
try {
boolean b = Boolean.parseBoolean(tableOptimisticLockEnable);
if (b) {
return true;
}
@@ -88,6 +107,30 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
return false;
}
public String getSlowQueryLoggerLevel(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String tableSlowQueryLoggerLevel = tableConfiguration.getProperty("slowQueryLoggerLevel");
if (tableSlowQueryLoggerLevel != null && !tableSlowQueryLoggerLevel.isEmpty()) {
return tableSlowQueryLoggerLevel;
}
} catch (Throwable ignored) {
}
return slowQueryLoggerLevel;
}
public String getSlowQueryLoggerTime(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String tableSlowQueryLoggerTime = tableConfiguration.getProperty("slowQueryLoggerTime");
if (tableSlowQueryLoggerTime != null && !tableSlowQueryLoggerTime.isEmpty()) {
return tableSlowQueryLoggerTime;
}
} catch (Throwable ignored) {
}
return slowQueryLoggerTime;
}
private String stringConfig(String key, String defaultValue) {
String v = properties.getProperty(key);
if (StringUtility.stringHasValue(v)) {
@@ -381,7 +424,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addAnnotation("@SuppressWarnings(\"DuplicatedCode\")");
implClass.addAnnotation("@Repository");
addImportPackages(implClass, modelClassName, exampleClassName, mapperClassName, interfaceName);
addImportPackages(implClass, introspectedTable, modelClassName, exampleClassName, mapperClassName, interfaceName);
FullyQualifiedJavaType superInterface = new FullyQualifiedJavaType(facadeRepositoryPackage + "." + interfaceName);
implClass.addSuperInterface(superInterface);
@@ -404,23 +447,23 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
generateFindAnyByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName, hasBLOBColumns);
generateFindValidByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName, hasBLOBColumns);
generateFindTrashByIdMethod(implClass, modelClassName, mapperFieldName, exampleClassName, hasBLOBColumns);
generateInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable);
generateBatchInsertMethod(implClass, modelClassName, mapperFieldName, introspectedTable);
generateUpdateMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns);
generateUpdateByExampleSelectiveMethod(implClass, modelClassName, exampleClassName, mapperFieldName, introspectedTable, hasBLOBColumns);
generateDeleteByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateDeleteAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateTrashByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateTrashAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateRecoverByIdMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateRecoverAllMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateInsertMethod(implClass, introspectedTable, modelClassName, mapperFieldName);
generateBatchInsertMethod(implClass, introspectedTable, modelClassName, mapperFieldName);
generateUpdateMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateUpdateByExampleSelectiveMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateDeleteByIdMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateDeleteAllMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateTrashByIdMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateTrashAllMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateRecoverByIdMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateRecoverAllMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateFindValidOneMethod(implClass, modelClassName, exampleClassName);
generateFindTrashOneMethod(implClass, modelClassName, exampleClassName);
generateGetValidListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateGetTrashListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateCountByValidMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateGetValidListMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateGetTrashListMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateCountByValidMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateCountByValidWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateCountByTrashMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateCountByTrashMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateCountByTrashWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
return implClass;
@@ -469,9 +512,9 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
}
}
private void addImportPackages(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperClassName, String interfaceName) {
private void addImportPackages(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperClassName, String interfaceName) {
implClass.addImportedType(new FullyQualifiedJavaType(guidGeneratorClass));
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
implClass.addImportedType(new FullyQualifiedJavaType(changeLogContextClassPackage + "." + changeLogContextClassName));
}
implClass.addImportedType(new FullyQualifiedJavaType(mapperPackage + "." + mapperClassName));
@@ -562,7 +605,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateInsertMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName, IntrospectedTable introspectedTable) {
private void generateInsertMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String mapperFieldName) {
Method method = new Method("insert");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -601,8 +644,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int count = " + mapperFieldName + ".insert(aDo);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] insert " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] insert " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
@@ -613,7 +656,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("record.setDataVersion(aDo.getDataVersion());");
method.addBodyLine("record.setCreateTime(aDo.getCreateTime());");
method.addBodyLine("record.setUpdateTime(aDo.getUpdateTime());");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"insert\", aDo.getGuid(), new HashMap<>());");
}
@@ -623,7 +666,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateBatchInsertMethod(TopLevelClass implClass, String modelClassName, String mapperFieldName, IntrospectedTable introspectedTable) {
private void generateBatchInsertMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String mapperFieldName) {
Method method = new Method("batchInsert");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -668,14 +711,14 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int count = " + mapperFieldName + ".batchInsert(batch);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] batch insert " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] batch insert " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
method.addBodyLine("if (count == batch.size()) {");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("for (" + modelClassName + " aDo : batch) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"batchInsert\", aDo.getGuid(), new HashMap<>());");
@@ -688,8 +731,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateUpdateMethod(TopLevelClass implClass, String modelClassName, String exampleClassName,
String mapperFieldName, IntrospectedTable introspectedTable, boolean hasBLOBColumns) {
private void generateUpdateMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName,
String mapperFieldName, boolean hasBLOBColumns) {
Method method = new Method("update");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -702,7 +745,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (aDo == null || aDo.getIsDelete() == 1) {");
method.addBodyLine("throw new Throwable(\"Record not found, " + modelClassName + " GUID:\" + record.getGuid());");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("Map<String, Object[]> changeDiff = new HashMap<>();");
}
for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
@@ -715,7 +758,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
continue;
}
method.addBodyLine("if (record." + getterMethod + "() != null) {");
if (isChangeLogEnable()) {
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("}");
@@ -724,7 +767,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("}");
}
method.addBodyLine(exampleClassName + " updateWhere = new " + exampleClassName + "();");
if (isOptimisticLockEnable()) {
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("Integer lockDataVersion = record.getDataVersion();");
method.addBodyLine("if (lockDataVersion == null) {");
method.addBodyLine("lockDataVersion = aDo.getDataVersion();");
@@ -746,13 +789,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
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 > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] update " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] update " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0 && !changeDiff.isEmpty()) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"update\", aDo.getGuid(), changeDiff);");
@@ -762,8 +805,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateUpdateByExampleSelectiveMethod(TopLevelClass implClass, String modelClassName, String exampleClassName,
String mapperFieldName, IntrospectedTable introspectedTable, boolean hasBLOBColumns) {
private void generateUpdateByExampleSelectiveMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName,
String mapperFieldName, boolean hasBLOBColumns) {
Method method = new Method("updateByExampleSelective");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -784,7 +827,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("}");
method.addBodyLine("example = new " + exampleClassName + "();");
method.addBodyLine("example.createCriteria().andGuidIn(guidList);");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
String selectByExampleMethod = hasBLOBColumns ? "selectByExampleWithBLOBs" : "selectByExample";
method.addBodyLine("List<" + modelClassName + "> recordList");
method.addBodyLine(" = " + mapperFieldName + "." + selectByExampleMethod + "(example);");
@@ -807,7 +850,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("diffGroup.put(aDo.getGuid(), changeDiff);");
method.addBodyLine("}");
}
if (isOptimisticLockEnable()) {
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("// reset data version, with optimistic locking");
method.addBodyLine("record.setDataVersion((int) (new Date().getTime() % 1000));");
}
@@ -821,18 +864,18 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(record, example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] updateByExampleSelective " + modelClassName + " long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] updateByExampleSelective " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine("for (Map.Entry<Long, Map<String, Object[]>> diffEntry : diffGroup.entrySet()) {");
method.addBodyLine("if (diffEntry.getValue() != null && !diffEntry.getValue().isEmpty()) {");
@@ -846,7 +889,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateDeleteByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateDeleteByIdMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("deleteById");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -863,8 +906,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("}");
method.addBodyLine(exampleClassName + " updateWhere");
method.addBodyLine(" = new " + exampleClassName + "();");
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();");
if (isOptimisticLockEnable()) {
method.addBodyLine("updateWhere.createCriteria()");
method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())");
method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);");
@@ -879,13 +922,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] deleteById " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] deleteById " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"deleteById\", aDo.getGuid(), new HashMap<>());");
@@ -895,7 +938,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateTrashByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateTrashByIdMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("trashById");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -910,8 +953,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine(exampleClassName + " updateWhere");
method.addBodyLine(" = new " + exampleClassName + "();");
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();");
if (isOptimisticLockEnable()) {
method.addBodyLine("updateWhere.createCriteria()");
method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())");
method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);");
@@ -927,13 +970,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] trashById " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] trashById " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"trashById\", aDo.getGuid(), new HashMap<>());");
@@ -943,7 +986,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateRecoverByIdMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateRecoverByIdMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("recoverById");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -961,8 +1004,8 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine(exampleClassName + " updateWhere");
method.addBodyLine(" = new " + exampleClassName + "();");
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("Integer lockDataVersion = aDo.getDataVersion();");
if (isOptimisticLockEnable()) {
method.addBodyLine("updateWhere.createCriteria()");
method.addBodyLine(" .andGuidEqualTo(aDo.getGuid())");
method.addBodyLine(" .andDataVersionEqualTo(lockDataVersion);");
@@ -977,13 +1020,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(aDo, updateWhere);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] recoverById " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] recoverById " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
method.addBodyLine(" \"recoverById\", aDo.getGuid(), new HashMap<>());");
@@ -993,7 +1036,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateDeleteAllMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateDeleteAllMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("deleteAll");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -1016,7 +1059,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("example = new " + exampleClassName + "();");
method.addBodyLine("example.createCriteria().andGuidIn(guidList);");
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
if (isOptimisticLockEnable()) {
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("// reset data version, with optimistic locking");
method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));");
}
@@ -1026,13 +1069,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] deleteAll " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] deleteAll " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine("for (Long guid : guidList) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
@@ -1044,7 +1087,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateTrashAllMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateTrashAllMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("trashAll");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -1063,7 +1106,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("example = new " + exampleClassName + "();");
method.addBodyLine("example.createCriteria().andGuidIn(guidList);");
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
if (isOptimisticLockEnable()) {
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("// reset data version, with optimistic locking");
method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));");
}
@@ -1074,13 +1117,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] trashAll " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] trashAll " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine("for (Long guid : guidList) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
@@ -1092,7 +1135,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateRecoverAllMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateRecoverAllMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("recoverAll");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -1111,7 +1154,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("example = new " + exampleClassName + "();");
method.addBodyLine("example.createCriteria().andGuidIn(guidList);");
method.addBodyLine(modelClassName + " " + lowerFirst(modelClassName) + " = new " + modelClassName + "();");
if (isOptimisticLockEnable()) {
if (isOptimisticLockEnable(introspectedTable)) {
method.addBodyLine("// reset data version, with optimistic locking");
method.addBodyLine(lowerFirst(modelClassName) + ".setDataVersion((int) (new Date().getTime() % 1000));");
}
@@ -1122,13 +1165,13 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("int update = " + mapperFieldName + ".updateByExampleSelective(" + lowerFirst(modelClassName) + ", example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] recoverAll " + modelClassName + " long time\" +");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] recoverAll " + modelClassName + " long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
method.addBodyLine(");");
method.addBodyLine("}");
if (isChangeLogEnable()) {
if (isChangeLogEnable(introspectedTable)) {
method.addBodyLine("if (update > 0) {");
method.addBodyLine("for (Long guid : guidList) {");
method.addBodyLine(changeLogContextClassName + ".addLog(\"" + modelClassName + "\",");
@@ -1177,7 +1220,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateGetValidListMethod(TopLevelClass implClass, String modelClassName, String exampleClassName,
private void generateGetValidListMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName,
String mapperFieldName, boolean hasBLOBColumns) {
Method method = new Method("getValidList");
method.addAnnotation("@Override");
@@ -1198,7 +1241,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("return new ArrayList<>();");
method.addBodyLine("}");
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (findPrimaryKeyTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1209,7 +1252,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getLimitString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list primary key long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid list primary key long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + findPrimaryKeyTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -1233,7 +1276,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
}
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1244,7 +1287,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getLimitString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid list long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid list long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -1254,7 +1297,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateGetTrashListMethod(TopLevelClass implClass, String modelClassName, String exampleClassName,
private void generateGetTrashListMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName,
String mapperFieldName, boolean hasBLOBColumns) {
Method method = new Method("getTrashList");
method.addAnnotation("@Override");
@@ -1275,7 +1318,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("return new ArrayList<>();");
method.addBodyLine("}");
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (findPrimaryKeyTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1286,7 +1329,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getLimitString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list primary key long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash list primary key long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + findPrimaryKeyTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -1310,7 +1353,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
}
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1321,7 +1364,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getLimitString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash list long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash list long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -1331,7 +1374,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateCountByValidMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateCountByValidMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("countByValid");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -1345,7 +1388,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1353,7 +1396,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getOrderByClause() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " valid count long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " valid count long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -1381,7 +1424,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
implClass.addMethod(method);
}
private void generateCountByTrashMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateCountByTrashMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("countByTrash");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -1395,7 +1438,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -1403,7 +1446,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getOrderByClause() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " trash count long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " trash count long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");

View File

@@ -9,6 +9,7 @@ import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import java.io.File;
@@ -71,6 +72,30 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
return defaultValue;
}
public String getSlowQueryLoggerLevel(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String tableSlowQueryLoggerLevel = tableConfiguration.getProperty("slowQueryLoggerLevel");
if (tableSlowQueryLoggerLevel != null && !tableSlowQueryLoggerLevel.isEmpty()) {
return tableSlowQueryLoggerLevel;
}
} catch (Throwable ignored) {
}
return slowQueryLoggerLevel;
}
public String getSlowQueryLoggerTime(IntrospectedTable introspectedTable) {
try {
TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
String tableSlowQueryLoggerTime = tableConfiguration.getProperty("slowQueryLoggerTime");
if (tableSlowQueryLoggerTime != null && !tableSlowQueryLoggerTime.isEmpty()) {
return tableSlowQueryLoggerTime;
}
} catch (Throwable ignored) {
}
return slowQueryLoggerTime;
}
/**
* 核心方法:仅为视图表生成 RepoView 代码
*/
@@ -102,6 +127,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
// 3. 生成视图Repo实现类
TopLevelClass repoImpl = generateRepoViewImpl(
introspectedTable,
repoImplName,
repoInterfaceName,
domainObjectName,
@@ -179,6 +205,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
* 生成视图Repo实现类逻辑不变仅适配新接口
*/
private TopLevelClass generateRepoViewImpl(
IntrospectedTable introspectedTable,
String implClassName,
String interfaceName,
String modelClassName,
@@ -215,8 +242,8 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
implClass.addField(mapperField);
generateFindOneMethod(implClass, modelClassName, exampleClassName);
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateCountMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
generateGetListMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
generateCountMethod(implClass, introspectedTable, modelClassName, exampleClassName, mapperFieldName);
generateCountWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
return implClass;
@@ -268,7 +295,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
/**
* 生成getList方法
*/
private void generateGetListMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName, boolean hasBLOBColumns) {
private void generateGetListMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName, boolean hasBLOBColumns) {
Method method = new Method("getList");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -288,7 +315,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
}
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -299,7 +326,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getLimitString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> limit: \" + example.getLimitString();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view list long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " view list long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
@@ -312,7 +339,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
/**
* 生成count方法
*/
private void generateCountMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
private void generateCountMethod(TopLevelClass implClass, IntrospectedTable introspectedTable, String modelClassName, String exampleClassName, String mapperFieldName) {
Method method = new Method("count");
method.addAnnotation("@Override");
method.setVisibility(JavaVisibility.PUBLIC);
@@ -325,7 +352,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
method.addBodyLine("long startTime = new Date().getTime();");
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
method.addBodyLine("if (useTime > " + getSlowQueryLoggerTime(introspectedTable) + ") {");
method.addBodyLine("String exampleString = \"\";");
method.addBodyLine("if (example.getWhereString() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> where: \" + example.getWhereString();");
@@ -333,7 +360,7 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
method.addBodyLine("if (example.getOrderByClause() != null) {");
method.addBodyLine("exampleString += \"\\n\\t|-> order by: \" + example.getOrderByClause();");
method.addBodyLine("}");
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"[SQL] select " + modelClassName + " view count long time\" +");
method.addBodyLine("LOGGER." + getSlowQueryLoggerLevel(introspectedTable) + "(\"[SQL] select " + modelClassName + " view count long time\" +");
method.addBodyLine(" \"\\n\\t|-> use time\" + useTime + \"ms\" +");
method.addBodyLine(" exampleString +");
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");