插件更新
This commit is contained in:
61
README.md
61
README.md
@@ -39,6 +39,8 @@
|
|||||||
- `getTrashList({Example} example)` - 获取回收站记录列表(支持分页)
|
- `getTrashList({Example} example)` - 获取回收站记录列表(支持分页)
|
||||||
- `countByValid({Example} example)` - 统计有效记录数
|
- `countByValid({Example} example)` - 统计有效记录数
|
||||||
- `countByTrash({Example} example)` - 统计回收站记录数
|
- `countByTrash({Example} example)` - 统计回收站记录数
|
||||||
|
- `countByValidWithPage({Example} example)` - 统计有效记录数
|
||||||
|
- `countByTrashWithPage({Example} example)` - 统计回收站记录数
|
||||||
- `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值)
|
- `insert({Model} record)` - 插入记录(自动生成 GUID、设置默认值)
|
||||||
- `updateByExampleSelective({Model} record, {Example} example)` - 按条件更新记录
|
- `updateByExampleSelective({Model} record, {Example} example)` - 按条件更新记录
|
||||||
- `update({Model} record)` - 更新记录(支持乐观锁)
|
- `update({Model} record)` - 更新记录(支持乐观锁)
|
||||||
@@ -60,9 +62,8 @@
|
|||||||
**添加的字段**:
|
**添加的字段**:
|
||||||
- `offset` - 偏移量
|
- `offset` - 偏移量
|
||||||
- `rows` - 每页数量
|
- `rows` - 每页数量
|
||||||
- `minPageNum` - 最小页码(默认 1)
|
- `startPageNum` - 最小页码(默认 1)
|
||||||
- `ignorePageSize` - 忽略分页数量(默认 10000)每页数量大于10000时,忽略分页
|
- `ignorePageSize` - 忽略分页数量(默认 10000)每页数量大于10000时,忽略分页
|
||||||
- `defaultPageSize` - 默认每页数量(默认 20)
|
|
||||||
- `maxPageSize` - 最大每页数量(默认 100)
|
- `maxPageSize` - 最大每页数量(默认 100)
|
||||||
- `withBLOBs` - 是否返回BLOBs列的数据
|
- `withBLOBs` - 是否返回BLOBs列的数据
|
||||||
|
|
||||||
@@ -70,10 +71,12 @@
|
|||||||
- `limit(int rows)` - 设置每页数量
|
- `limit(int rows)` - 设置每页数量
|
||||||
- `limit(int offset, int rows)` - 设置偏移量和每页数量
|
- `limit(int offset, int rows)` - 设置偏移量和每页数量
|
||||||
- `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset)
|
- `usePage(int pageNum, int pageSize)` - 使用页码和每页数量(自动计算 offset)
|
||||||
- `getPageNum()` - 获取当前页码
|
|
||||||
- `getPageSize()` - 获取当前每页数量
|
|
||||||
- `setWithBLOBs(boolean withBLOBs)` - 设置是否返回BLOBs列的数据
|
- `setWithBLOBs(boolean withBLOBs)` - 设置是否返回BLOBs列的数据
|
||||||
- `isWithBLOBs()` - 是否返回BLOBs列的数据
|
- `isWithBLOBs()` - 是否返回BLOBs列的数据
|
||||||
|
- `getPageNum()` - 获取当前页码
|
||||||
|
- `getPageSize()` - 获取当前每页数量
|
||||||
|
- `getOffset()` - 获取当前分页limit的offset
|
||||||
|
- `getRows()` - 获取当前分页limit的rows
|
||||||
|
|
||||||
## 使用方法
|
## 使用方法
|
||||||
### 1. 在 `pom.xml` 中配置插件
|
### 1. 在 `pom.xml` 中配置插件
|
||||||
@@ -138,9 +141,11 @@
|
|||||||
<property name="snowflakeUtilClass" value="com.iqudoo.framework.tape.modules.utils.SnowflakeUtil"/>
|
<property name="snowflakeUtilClass" value="com.iqudoo.framework.tape.modules.utils.SnowflakeUtil"/>
|
||||||
<property name="snowflakeUtilGenId" value="SnowflakeUtil.nextId()"/>
|
<property name="snowflakeUtilGenId" value="SnowflakeUtil.nextId()"/>
|
||||||
<property name="slowQueryLoggerTime" value="300"/>
|
<property name="slowQueryLoggerTime" value="300"/>
|
||||||
|
<property name="slowQueryLoggerLevel" value="error"/>
|
||||||
|
<property name="priorityPrimaryKeyOffset" value="100"/>
|
||||||
<property name="ignorePageSize" value="10000"/>
|
<property name="ignorePageSize" value="10000"/>
|
||||||
<property name="defaultPageSize" value="20"/>
|
<property name="startPageNum" value="1"/>
|
||||||
<property name="startPage" value="1"/>
|
<property name="maxPageSize" value="100"/>
|
||||||
|
|
||||||
<!-- 添加插件 -->
|
<!-- 添加插件 -->
|
||||||
<plugin type="com.iqudoo.framework.mybatis.TapeMybatisGeneratorPlugin"/>
|
<plugin type="com.iqudoo.framework.mybatis.TapeMybatisGeneratorPlugin"/>
|
||||||
@@ -153,21 +158,23 @@
|
|||||||
|
|
||||||
### 3. 配置参数说明
|
### 3. 配置参数说明
|
||||||
|
|
||||||
| 参数名 | 说明 | 默认值 | 必需 |
|
| 参数名 | 说明 | 默认值 | 必需 |
|
||||||
|--------|---------------------|---------------------------------------------------------|------|
|
|----------------------------|-------------------------------|-------------------------------------------------------|------|
|
||||||
| `viewKeyWords` | 视图表关键字(逗号分隔,不区分大小写) | `VIEW_,V_` | 否 |
|
| `viewKeyWords` | 视图表关键字(逗号分隔,不区分大小写) | `VIEW_,V_` | 否 |
|
||||||
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
|
| `targetProject` | 生成代码的目标项目路径 | `src/main/java` | 否 |
|
||||||
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
|
| `modelPackage` | Model 类的包路径 | `com.iqudoo.platform.application.database.model` | 是 |
|
||||||
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
|
| `mapperPackage` | Mapper 接口的包路径 | `com.iqudoo.platform.application.database.mapper` | 是 |
|
||||||
| `facadeRepositoryPackage` | Repository 接口的包路径 | `com.iqudoo.platform.application.facade.repository` | 否 |
|
| `facadeRepositoryPackage` | Repository 接口的包路径 | `com.iqudoo.platform.application.facade.repository` | 否 |
|
||||||
| `domainRepositoryPackage` | Repository 实现类的包路径 | `com.iqudoo.platform.application.domain.repository` | 否 |
|
| `domainRepositoryPackage` | Repository 实现类的包路径 | `com.iqudoo.platform.application.domain.repository` | 否 |
|
||||||
| `facadeRepoviewPackage` | RepoView 接口的包路径 | `com.iqudoo.platform.application.facade.repoview` | 否 |
|
| `facadeRepoviewPackage` | RepoView 接口的包路径 | `com.iqudoo.platform.application.facade.repoview` | 否 |
|
||||||
| `snowflakeUtilClass` | 雪花算法ID生成工具类 | `com.iqudoo.framework.tape.modules.utils.SnowflakeUtil` | 否 |
|
| `snowflakeUtilClass` | 雪花算法ID生成工具类 | `com.iqudoo.framework.tape.modules.utils.SnowflakeUtil` | 否 |
|
||||||
| `snowflakeUtilGenId` | 雪花算法ID生成方法 | `SnowflakeUtil.nextId()` | 否 |
|
| `snowflakeUtilGenId` | 雪花算法ID生成方法 | `SnowflakeUtil.nextId()` | 否 |
|
||||||
| `slowQueryLoggerTime` | 慢查询日志时间阈值 | `200` | 否 |
|
| `slowQueryLoggerTime` | 慢查询日志时间阈值 | `300` | 否 |
|
||||||
| `ignorePageSize` | 忽略分页阈值 | `10000` | 否 |
|
| `slowQueryLoggerLevel` | 慢查询日志类型:error,warn,debug,info | `error` | 否 |
|
||||||
| `defaultPageSize` | 默认分页数量 | `20` | 否 |
|
| `priorityPrimaryKeyOffset` | 优先查询主键偏移阈值 | `0` | 否 |
|
||||||
| `startPage` | 开始页码 | `1` | 否 |
|
| `ignorePageSize` | 忽略分页阈值 | `10000` | 否 |
|
||||||
|
| `startPageNum` | 分页开始页码 | `1` | 否 |
|
||||||
|
| `maxPageSize` | 最大每页数量 | `100` | 否 |
|
||||||
|
|
||||||
**视图表识别规则**:
|
**视图表识别规则**:
|
||||||
- 表名包含 `viewKeyWords` 中任一关键字的表将被识别为视图表,大小写不敏感
|
- 表名包含 `viewKeyWords` 中任一关键字的表将被识别为视图表,大小写不敏感
|
||||||
@@ -180,7 +187,7 @@
|
|||||||
|
|
||||||
为了使用完整的 Repository 功能(软删除、回收站等),表结构需要包含以下标准字段:
|
为了使用完整的 Repository 功能(软删除、回收站等),表结构需要包含以下标准字段:
|
||||||
|
|
||||||
```sql
|
```mysql
|
||||||
DROP TABLE IF EXISTS `your_table_name`;
|
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',
|
`guid` bigint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'GUID',
|
||||||
@@ -193,7 +200,15 @@ CREATE TABLE `your_table_name` (
|
|||||||
`data_version` int(0) NOT NULL DEFAULT 0 COMMENT '数据版本',
|
`data_version` int(0) NOT NULL DEFAULT 0 COMMENT '数据版本',
|
||||||
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
|
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
|
||||||
`update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
|
`update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
|
||||||
PRIMARY KEY (`guid`) USING BTREE
|
PRIMARY KEY (`guid`) USING BTREE,
|
||||||
|
-- UNIQUE KEY `idx_unique_key` (`you unique key`,`delete_token`) USING BTREE,
|
||||||
|
-- KEY `idx_common_query` (
|
||||||
|
-- 等值查询字段,
|
||||||
|
-- `is_hidden`,`is_delete`,
|
||||||
|
-- 其他参与排序字段,
|
||||||
|
-- `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;
|
) ENGINE = InnoDB COMMENT = '你的表格备注' ROW_FORMAT = Dynamic;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -9,61 +9,60 @@ import org.mybatis.generator.api.dom.xml.Attribute;
|
|||||||
import org.mybatis.generator.api.dom.xml.Document;
|
import org.mybatis.generator.api.dom.xml.Document;
|
||||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||||
|
import org.mybatis.generator.config.Context;
|
||||||
import org.mybatis.generator.internal.util.StringUtility;
|
import org.mybatis.generator.internal.util.StringUtility;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
||||||
|
|
||||||
private final static int DEFAULT_START_PAGE = 1;
|
private int startPageNum = 1;
|
||||||
private final static int DEFAULT_PAGE_SIZE = 20;
|
private int maxPageSize = 100;
|
||||||
private final static int DEFAULT_IGNORE_PAGE_SIZE = 10000;
|
private int ignorePageSize = 10000;
|
||||||
private int startPage = DEFAULT_START_PAGE;
|
|
||||||
private int defaultPageSize = DEFAULT_PAGE_SIZE;
|
|
||||||
private int ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setProperties(Properties properties) {
|
|
||||||
super.setProperties(properties);
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("startPage"))) {
|
|
||||||
try {
|
|
||||||
startPage = Integer.parseInt(properties.getProperty("startPage"));
|
|
||||||
} catch (Throwable ignored) {
|
|
||||||
startPage = DEFAULT_START_PAGE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("defaultPageSize"))) {
|
|
||||||
try {
|
|
||||||
defaultPageSize = Integer.parseInt(properties.getProperty("defaultPageSize"));
|
|
||||||
} catch (Throwable ignored) {
|
|
||||||
defaultPageSize = DEFAULT_PAGE_SIZE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("ignorePageSize"))) {
|
|
||||||
try {
|
|
||||||
ignorePageSize = Integer.parseInt(properties.getProperty("ignorePageSize"));
|
|
||||||
} catch (Throwable ignored) {
|
|
||||||
ignorePageSize = DEFAULT_IGNORE_PAGE_SIZE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(List<String> list) {
|
public boolean validate(List<String> list) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContext(Context context) {
|
||||||
|
super.setContext(context);
|
||||||
|
resolveConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resolveConfiguration() {
|
||||||
|
startPageNum = intConfig("startPageNum", startPageNum);
|
||||||
|
ignorePageSize = intConfig("ignorePageSize", ignorePageSize);
|
||||||
|
maxPageSize = intConfig("maxPageSize", maxPageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int intConfig(String key, int defaultValue) {
|
||||||
|
String v = properties.getProperty(key);
|
||||||
|
if (!StringUtility.stringHasValue(v) && context != null) {
|
||||||
|
v = context.getProperty(key);
|
||||||
|
}
|
||||||
|
if (!StringUtility.stringHasValue(v)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(v.trim());
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
@Override
|
@Override
|
||||||
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||||
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
|
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
|
||||||
// 添加 minPageNum、defaultPageSize、maxPageSize、ignorePageSize 字段
|
// 添加 startPageNum、maxPageSize、ignorePageSize 字段
|
||||||
Field maxPageSizeField = ElementTools.generateField(
|
Field maxPageSizeField = ElementTools.generateField(
|
||||||
"maxPageSize",
|
"maxPageSize",
|
||||||
JavaVisibility.PROTECTED,
|
JavaVisibility.PROTECTED,
|
||||||
integerWrapper,
|
integerWrapper,
|
||||||
"100"
|
this.maxPageSize + ""
|
||||||
);
|
);
|
||||||
topLevelClass.addField(maxPageSizeField);
|
topLevelClass.addField(maxPageSizeField);
|
||||||
|
|
||||||
@@ -75,21 +74,13 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
);
|
);
|
||||||
topLevelClass.addField(ignorePageSizeField);
|
topLevelClass.addField(ignorePageSizeField);
|
||||||
|
|
||||||
Field defaultPageSizeField = ElementTools.generateField(
|
Field startPageNumField = ElementTools.generateField(
|
||||||
"defaultPageSize",
|
"startPageNum",
|
||||||
JavaVisibility.PROTECTED,
|
JavaVisibility.PROTECTED,
|
||||||
integerWrapper,
|
integerWrapper,
|
||||||
defaultPageSize + ""
|
startPageNum + ""
|
||||||
);
|
);
|
||||||
topLevelClass.addField(defaultPageSizeField);
|
topLevelClass.addField(startPageNumField);
|
||||||
|
|
||||||
Field minPageNumField = ElementTools.generateField(
|
|
||||||
"minPageNum",
|
|
||||||
JavaVisibility.PROTECTED,
|
|
||||||
integerWrapper,
|
|
||||||
startPage + ""
|
|
||||||
);
|
|
||||||
topLevelClass.addField(minPageNumField);
|
|
||||||
|
|
||||||
// 添加offset和rows字段
|
// 添加offset和rows字段
|
||||||
Field offsetField = ElementTools.generateField(
|
Field offsetField = ElementTools.generateField(
|
||||||
@@ -127,7 +118,6 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
FormatTools.addMethodWithBestPosition(topLevelClass, isWithBLOBsMethod);
|
FormatTools.addMethodWithBestPosition(topLevelClass, isWithBLOBsMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 增加getter && setter 方法
|
// 增加getter && setter 方法
|
||||||
Method mSetMaxPageSize = ElementTools.generateSetterMethod(maxPageSizeField);
|
Method mSetMaxPageSize = ElementTools.generateSetterMethod(maxPageSizeField);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMaxPageSize);
|
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMaxPageSize);
|
||||||
@@ -141,59 +131,41 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
Method mGetIgnorePageSize = ElementTools.generateGetterMethod(ignorePageSizeField);
|
Method mGetIgnorePageSize = ElementTools.generateGetterMethod(ignorePageSizeField);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetIgnorePageSize);
|
FormatTools.addMethodWithBestPosition(topLevelClass, mGetIgnorePageSize);
|
||||||
|
|
||||||
Method mSetDefaultPageSize = ElementTools.generateSetterMethod(defaultPageSizeField);
|
Method mSetStartPageNum = ElementTools.generateSetterMethod(startPageNumField);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetDefaultPageSize);
|
FormatTools.addMethodWithBestPosition(topLevelClass, mSetStartPageNum);
|
||||||
|
|
||||||
Method mGetDefaultPageSize = ElementTools.generateGetterMethod(defaultPageSizeField);
|
Method mGetStartPageNum = ElementTools.generateGetterMethod(startPageNumField);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetDefaultPageSize);
|
FormatTools.addMethodWithBestPosition(topLevelClass, mGetStartPageNum);
|
||||||
|
|
||||||
Method mSetMinPageNum = ElementTools.generateSetterMethod(minPageNumField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetMinPageNum);
|
|
||||||
|
|
||||||
Method mGetMinPageNum = ElementTools.generateGetterMethod(minPageNumField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetMinPageNum);
|
|
||||||
|
|
||||||
Method mSetOffset = ElementTools.generateSetterMethod(offsetField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetOffset);
|
|
||||||
|
|
||||||
Method mGetOffset = ElementTools.generateGetterMethod(offsetField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetOffset);
|
|
||||||
|
|
||||||
Method mSetRows = ElementTools.generateSetterMethod(rowsField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetRows);
|
|
||||||
|
|
||||||
Method mGetRows = ElementTools.generateGetterMethod(rowsField);
|
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetRows);
|
|
||||||
|
|
||||||
// 提供几个快捷方法
|
// 提供几个快捷方法
|
||||||
Method setLimit = ElementTools.generateMethod(
|
Method setLimitByRows = ElementTools.generateMethod(
|
||||||
"limit",
|
"limit",
|
||||||
JavaVisibility.PUBLIC,
|
JavaVisibility.PUBLIC,
|
||||||
topLevelClass.getType(),
|
topLevelClass.getType(),
|
||||||
new Parameter(integerWrapper, "rows")
|
new Parameter(integerWrapper, "rows")
|
||||||
);
|
);
|
||||||
setLimit = ElementTools.generateMethodBody(
|
setLimitByRows = ElementTools.generateMethodBody(
|
||||||
setLimit,
|
setLimitByRows,
|
||||||
"this.offset = null;",
|
"this.offset = null;",
|
||||||
"this.rows = rows;",
|
"this.rows = rows;",
|
||||||
"return this;"
|
"return this;"
|
||||||
);
|
);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit);
|
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByRows);
|
||||||
|
|
||||||
Method setLimit2 = ElementTools.generateMethod(
|
Method setLimitByOffsetRows = ElementTools.generateMethod(
|
||||||
"limit",
|
"limit",
|
||||||
JavaVisibility.PUBLIC,
|
JavaVisibility.PUBLIC,
|
||||||
topLevelClass.getType(),
|
topLevelClass.getType(),
|
||||||
new Parameter(integerWrapper, "offset"),
|
new Parameter(integerWrapper, "offset"),
|
||||||
new Parameter(integerWrapper, "rows")
|
new Parameter(integerWrapper, "rows")
|
||||||
);
|
);
|
||||||
setLimit2 = ElementTools.generateMethodBody(
|
setLimitByOffsetRows = ElementTools.generateMethodBody(
|
||||||
setLimit2,
|
setLimitByOffsetRows,
|
||||||
"this.offset = offset;",
|
"this.offset = offset;",
|
||||||
"this.rows = rows;",
|
"this.rows = rows;",
|
||||||
"return this;"
|
"return this;"
|
||||||
);
|
);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, setLimit2);
|
FormatTools.addMethodWithBestPosition(topLevelClass, setLimitByOffsetRows);
|
||||||
|
|
||||||
Method usePage = ElementTools.generateMethod(
|
Method usePage = ElementTools.generateMethod(
|
||||||
"usePage",
|
"usePage",
|
||||||
@@ -204,15 +176,15 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
);
|
);
|
||||||
usePage = ElementTools.generateMethodBody(
|
usePage = ElementTools.generateMethodBody(
|
||||||
usePage,
|
usePage,
|
||||||
"pageSize = pageSize == null || pageSize <= 0 ? this.defaultPageSize : pageSize;",
|
"pageSize = pageSize == null || pageSize <= 0 ? 1 : pageSize;",
|
||||||
"pageNum = pageNum == null || pageNum < this.minPageNum ? this.minPageNum : pageNum;",
|
"pageNum = pageNum == null || pageNum < this.startPageNum ? this.startPageNum : pageNum;",
|
||||||
"if (pageSize >= this.ignorePageSize) {",
|
"if (pageSize >= this.ignorePageSize) {",
|
||||||
"this.rows = null;",
|
"this.rows = null;",
|
||||||
"this.offset = null;",
|
"this.offset = null;",
|
||||||
"return this;",
|
"return this;",
|
||||||
"}",
|
"}",
|
||||||
"int cPageSize = pageSize > this.maxPageSize ? this.maxPageSize: pageSize;",
|
"int cPageSize = pageSize > this.maxPageSize ? this.maxPageSize: pageSize;",
|
||||||
"this.offset = (pageNum - this.minPageNum) * cPageSize;",
|
"this.offset = (pageNum - this.startPageNum) * cPageSize;",
|
||||||
"this.rows = cPageSize;",
|
"this.rows = cPageSize;",
|
||||||
"return this;"
|
"return this;"
|
||||||
);
|
);
|
||||||
@@ -227,9 +199,9 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
getPageNum = ElementTools.generateMethodBody(
|
getPageNum = ElementTools.generateMethodBody(
|
||||||
getPageNum,
|
getPageNum,
|
||||||
"if (this.rows == null || this.offset == null || this.rows == 0) {",
|
"if (this.rows == null || this.offset == null || this.rows == 0) {",
|
||||||
"return this.minPageNum;",
|
"return this.startPageNum;",
|
||||||
"}",
|
"}",
|
||||||
"return this.offset / this.rows + this.minPageNum;"
|
"return this.offset / this.rows + this.startPageNum;"
|
||||||
);
|
);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum);
|
FormatTools.addMethodWithBestPosition(topLevelClass, getPageNum);
|
||||||
// 计算获取当前每页数量
|
// 计算获取当前每页数量
|
||||||
@@ -247,6 +219,28 @@ public class TapeMybatisGeneratorPlugin extends PluginAdapter {
|
|||||||
);
|
);
|
||||||
FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize);
|
FormatTools.addMethodWithBestPosition(topLevelClass, getPageSize);
|
||||||
|
|
||||||
|
Method getRows = ElementTools.generateMethod(
|
||||||
|
"getRows",
|
||||||
|
JavaVisibility.PUBLIC,
|
||||||
|
integerWrapper
|
||||||
|
);
|
||||||
|
getRows = ElementTools.generateMethodBody(
|
||||||
|
getRows,
|
||||||
|
"return this.rows;"
|
||||||
|
);
|
||||||
|
FormatTools.addMethodWithBestPosition(topLevelClass, getRows);
|
||||||
|
|
||||||
|
Method getOffset = ElementTools.generateMethod(
|
||||||
|
"getOffset",
|
||||||
|
JavaVisibility.PUBLIC,
|
||||||
|
integerWrapper
|
||||||
|
);
|
||||||
|
getOffset = ElementTools.generateMethodBody(
|
||||||
|
getOffset,
|
||||||
|
"return this.offset;"
|
||||||
|
);
|
||||||
|
FormatTools.addMethodWithBestPosition(topLevelClass, getOffset);
|
||||||
|
|
||||||
// !!! clear 方法增加 offset 和 rows的清理
|
// !!! clear 方法增加 offset 和 rows的清理
|
||||||
List<Method> methodList = topLevelClass.getMethods();
|
List<Method> methodList = topLevelClass.getMethods();
|
||||||
for (Method method : methodList) {
|
for (Method method : methodList) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.iqudoo.framework.mybatis;
|
package com.iqudoo.framework.mybatis;
|
||||||
|
|
||||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||||
|
import com.iqudoo.framework.mybatis.utils.UtilTools;
|
||||||
import org.mybatis.generator.api.*;
|
import org.mybatis.generator.api.*;
|
||||||
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
|
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
|
||||||
import org.mybatis.generator.api.dom.java.*;
|
import org.mybatis.generator.api.dom.java.*;
|
||||||
@@ -11,13 +12,14 @@ import java.io.File;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@SuppressWarnings({"DuplicatedCode", "unused", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
@SuppressWarnings({"DuplicatedCode", "unused", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||||
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
||||||
|
|
||||||
// 固定配置项
|
// 固定配置项
|
||||||
private String slowQueryLoggerTime = "300";
|
private String slowQueryLoggerTime = "300";
|
||||||
|
private String priorityPrimaryKeyOffset = "0";
|
||||||
|
private String slowQueryLoggerLevel = "error";
|
||||||
private String snowflakeUtilClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil";
|
private String snowflakeUtilClass = "com.iqudoo.framework.tape.modules.utils.SnowflakeUtil";
|
||||||
private String snowflakeUtilGenId = "SnowflakeUtil.nextId()";
|
private String snowflakeUtilGenId = "SnowflakeUtil.nextId()";
|
||||||
private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository";
|
private String facadeRepositoryPackage = "com.iqudoo.platform.application.facade.repository";
|
||||||
@@ -30,49 +32,51 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
// 1.4.1版本专用:Java格式化器
|
// 1.4.1版本专用:Java格式化器
|
||||||
private JavaFormatter javaFormatter;
|
private JavaFormatter javaFormatter;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setContext(Context context) {
|
|
||||||
super.setContext(context);
|
|
||||||
this.javaFormatter = new DefaultJavaFormatter();
|
|
||||||
this.javaFormatter.setContext(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(List<String> warnings) {
|
public boolean validate(List<String> warnings) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setProperties(Properties properties) {
|
public void setContext(Context context) {
|
||||||
super.setProperties(properties);
|
super.setContext(context);
|
||||||
// 读取自定义配置
|
this.javaFormatter = new DefaultJavaFormatter();
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
this.javaFormatter.setContext(context);
|
||||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
resolveConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resolveConfiguration() {
|
||||||
|
slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime);
|
||||||
|
slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel);
|
||||||
|
priorityPrimaryKeyOffset = stringConfig("priorityPrimaryKeyOffset", priorityPrimaryKeyOffset);
|
||||||
|
if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) {
|
||||||
|
slowQueryLoggerLevel = "error";
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilClass"))) {
|
snowflakeUtilClass = stringConfig("snowflakeUtilClass", snowflakeUtilClass);
|
||||||
snowflakeUtilClass = properties.getProperty("snowflakeUtilClass");
|
snowflakeUtilGenId = stringConfig("snowflakeUtilGenId", snowflakeUtilGenId);
|
||||||
|
facadeRepositoryPackage = stringConfig("facadeRepositoryPackage", facadeRepositoryPackage);
|
||||||
|
domainRepositoryPackage = stringConfig("domainRepositoryPackage", domainRepositoryPackage);
|
||||||
|
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||||
|
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||||
|
targetProject = stringConfig("targetProject", targetProject);
|
||||||
|
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||||
|
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||||
|
viewKeyWords = viewKeyWords.toUpperCase();
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("snowflakeUtilGenId"))) {
|
}
|
||||||
snowflakeUtilGenId = properties.getProperty("snowflakeUtilGenId");
|
|
||||||
|
private String stringConfig(String key, String defaultValue) {
|
||||||
|
String v = properties.getProperty(key);
|
||||||
|
if (StringUtility.stringHasValue(v)) {
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepositoryPackage"))) {
|
if (context != null) {
|
||||||
facadeRepositoryPackage = properties.getProperty("facadeRepositoryPackage");
|
v = context.getProperty(key);
|
||||||
}
|
if (StringUtility.stringHasValue(v)) {
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("domainRepositoryPackage"))) {
|
return v;
|
||||||
domainRepositoryPackage = properties.getProperty("domainRepositoryPackage");
|
}
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("modelPackage"))) {
|
|
||||||
modelPackage = properties.getProperty("modelPackage");
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("mapperPackage"))) {
|
|
||||||
mapperPackage = properties.getProperty("mapperPackage");
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("targetProject"))) {
|
|
||||||
targetProject = properties.getProperty("targetProject");
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("viewKeyWords"))) {
|
|
||||||
viewKeyWords = properties.getProperty("viewKeyWords").toUpperCase();
|
|
||||||
}
|
}
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -286,7 +290,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
countByValidMethod.setAbstract(true);
|
countByValidMethod.setAbstract(true);
|
||||||
repositoryInterface.addMethod(countByValidMethod);
|
repositoryInterface.addMethod(countByValidMethod);
|
||||||
|
|
||||||
// 15. countByTrash
|
// 15. countByValidWithPage
|
||||||
|
Method countByValidWithPageMethod = new Method("countByValidWithPage");
|
||||||
|
countByValidWithPageMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
countByValidWithPageMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
countByValidWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
countByValidWithPageMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
countByValidWithPageMethod.setAbstract(true);
|
||||||
|
repositoryInterface.addMethod(countByValidWithPageMethod);
|
||||||
|
|
||||||
|
// 16. countByTrash
|
||||||
Method countByTrashMethod = new Method("countByTrash");
|
Method countByTrashMethod = new Method("countByTrash");
|
||||||
countByTrashMethod.setVisibility(JavaVisibility.PUBLIC);
|
countByTrashMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
countByTrashMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
countByTrashMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
@@ -295,7 +308,16 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
countByTrashMethod.setAbstract(true);
|
countByTrashMethod.setAbstract(true);
|
||||||
repositoryInterface.addMethod(countByTrashMethod);
|
repositoryInterface.addMethod(countByTrashMethod);
|
||||||
|
|
||||||
// 16. insert
|
// 17. countByTrashWithPage
|
||||||
|
Method countByTrashWithPageMethod = new Method("countByTrashWithPage");
|
||||||
|
countByTrashWithPageMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
countByTrashWithPageMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
countByTrashWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
countByTrashWithPageMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
countByTrashWithPageMethod.setAbstract(true);
|
||||||
|
repositoryInterface.addMethod(countByTrashWithPageMethod);
|
||||||
|
|
||||||
|
// 18. insert
|
||||||
Method insertMethod = new Method("insert");
|
Method insertMethod = new Method("insert");
|
||||||
insertMethod.setVisibility(JavaVisibility.PUBLIC);
|
insertMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
insertMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
insertMethod.setReturnType(new FullyQualifiedJavaType(modelClassName));
|
||||||
@@ -304,7 +326,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
insertMethod.setAbstract(true);
|
insertMethod.setAbstract(true);
|
||||||
repositoryInterface.addMethod(insertMethod);
|
repositoryInterface.addMethod(insertMethod);
|
||||||
|
|
||||||
// 17. update
|
// 19. update
|
||||||
Method updateMethod = new Method("update");
|
Method updateMethod = new Method("update");
|
||||||
updateMethod.setVisibility(JavaVisibility.PUBLIC);
|
updateMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
updateMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
updateMethod.setReturnType(new FullyQualifiedJavaType("int"));
|
||||||
@@ -313,7 +335,7 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
updateMethod.setAbstract(true);
|
updateMethod.setAbstract(true);
|
||||||
repositoryInterface.addMethod(updateMethod);
|
repositoryInterface.addMethod(updateMethod);
|
||||||
|
|
||||||
// 17. updateByExampleSelective
|
// 20. 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"));
|
||||||
@@ -379,8 +401,10 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
generateFindTrashOneMethod(implClass, modelClassName, exampleClassName);
|
generateFindTrashOneMethod(implClass, modelClassName, exampleClassName);
|
||||||
generateGetValidListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
generateGetValidListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
generateGetTrashListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
generateGetTrashListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
generateCountByValidMethod(implClass, exampleClassName, mapperFieldName);
|
generateCountByValidMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
generateCountByTrashMethod(implClass, exampleClassName, mapperFieldName);
|
generateCountByValidWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
|
generateCountByTrashMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
|
generateCountByTrashWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
|
|
||||||
return implClass;
|
return implClass;
|
||||||
}
|
}
|
||||||
@@ -798,43 +822,48 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("List<" + modelClassName + "> result = null;");
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
|
||||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
|
||||||
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
|
||||||
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
|
||||||
method.addBodyLine("return new ArrayList<>();");
|
|
||||||
method.addBodyLine("}");
|
|
||||||
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
|
||||||
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
|
||||||
method.addBodyLine("LOGGER.error(\"find valid list primary key use long time: \" + findPrimaryKeyTime + \"ms\");");
|
|
||||||
method.addBodyLine("}");
|
|
||||||
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
|
||||||
if (hasBLOBColumns) {
|
if (hasBLOBColumns) {
|
||||||
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {");
|
||||||
}
|
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||||
method.addBodyLine("example = new " + exampleClassName + "();");
|
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
||||||
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
method.addBodyLine("return new ArrayList<>();");
|
||||||
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
method.addBodyLine("}");
|
||||||
if (hasBLOBColumns) {
|
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
||||||
}
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid list primary key use long time: \" + findPrimaryKeyTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
if (hasBLOBColumns) {
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("// reset start time");
|
||||||
|
method.addBodyLine("startTime = new Date().getTime();");
|
||||||
|
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
||||||
|
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
||||||
|
method.addBodyLine("example = new " + exampleClassName + "();");
|
||||||
|
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
||||||
|
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
||||||
|
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
||||||
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||||
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||||
|
method.addBodyLine("} else {");
|
||||||
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
|
||||||
} else {
|
} else {
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
}
|
}
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"get valid list use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid list use long time: \" + useTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return result;");
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -850,47 +879,52 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
method.addBodyLine("for (" + exampleClassName + ".Criteria criteria : example.getOredCriteria()) {");
|
||||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("List<" + modelClassName + "> result = null;");
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
|
||||||
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
|
||||||
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
|
||||||
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
|
||||||
method.addBodyLine("return new ArrayList<>();");
|
|
||||||
method.addBodyLine("}");
|
|
||||||
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
|
||||||
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
|
||||||
method.addBodyLine("LOGGER.error(\"find trash list primary key use long time: \" + findPrimaryKeyTime + \"ms\");");
|
|
||||||
method.addBodyLine("}");
|
|
||||||
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
|
||||||
if (hasBLOBColumns) {
|
if (hasBLOBColumns) {
|
||||||
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null && example.getOffset() > " + priorityPrimaryKeyOffset + ") {");
|
||||||
}
|
method.addBodyLine("List<Long> primaryKeyList = " + mapperFieldName + ".selectPrimaryKeyByExample(example);");
|
||||||
method.addBodyLine("example = new " + exampleClassName + "();");
|
method.addBodyLine("if (primaryKeyList == null || primaryKeyList.isEmpty()) {");
|
||||||
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
method.addBodyLine("return new ArrayList<>();");
|
||||||
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
method.addBodyLine("}");
|
||||||
if (hasBLOBColumns) {
|
method.addBodyLine("long findPrimaryKeyTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
method.addBodyLine("if (findPrimaryKeyTime > " + slowQueryLoggerTime + ") {");
|
||||||
}
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash list primary key use long time: \" + findPrimaryKeyTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
if (hasBLOBColumns) {
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("// reset start time");
|
||||||
|
method.addBodyLine("startTime = new Date().getTime();");
|
||||||
|
method.addBodyLine("String oldOrderByClause = example.getOrderByClause();");
|
||||||
|
method.addBodyLine("Boolean withBLOBsFlag = example.isWithBLOBs();");
|
||||||
|
method.addBodyLine("example = new " + exampleClassName + "();");
|
||||||
|
method.addBodyLine("example.createCriteria().andGuidIn(primaryKeyList);");
|
||||||
|
method.addBodyLine("example.setOrderByClause(oldOrderByClause);");
|
||||||
|
method.addBodyLine("example.setWithBLOBs(withBLOBsFlag);");
|
||||||
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||||
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||||
|
method.addBodyLine("} else {");
|
||||||
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
|
||||||
} else {
|
} else {
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
}
|
}
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"get trash list use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash list use long time: \" + useTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return result;");
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateCountByValidMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
private void generateCountByValidMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
Method method = new Method("countByValid");
|
Method method = new Method("countByValid");
|
||||||
method.addAnnotation("@Override");
|
method.addAnnotation("@Override");
|
||||||
method.setVisibility(JavaVisibility.PUBLIC);
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
@@ -902,19 +936,38 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(0);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"count by valid use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " valid count use long time: \" + useTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return count;");
|
||||||
|
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateCountByTrashMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
private void generateCountByValidWithPageMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
|
Method method = new Method("countByValidWithPage");
|
||||||
|
method.addAnnotation("@Override");
|
||||||
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
method.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
|
||||||
|
method.addBodyLine("// When not paginated, the count query returns 0 to avoid unnecessary queries");
|
||||||
|
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||||
|
method.addBodyLine("return countByValid(example);");
|
||||||
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return 0L;");
|
||||||
|
|
||||||
|
implClass.addMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateCountByTrashMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
Method method = new Method("countByTrash");
|
Method method = new Method("countByTrash");
|
||||||
method.addAnnotation("@Override");
|
method.addAnnotation("@Override");
|
||||||
method.setVisibility(JavaVisibility.PUBLIC);
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
@@ -926,14 +979,33 @@ public class TapeRepositoryGeneratorPlugin extends PluginAdapter {
|
|||||||
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
method.addBodyLine("criteria.andIsDeleteEqualTo(0).andIsHiddenEqualTo(1);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"count by trash use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " trash count use long time: \" + useTime + \"ms\" +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return count;");
|
||||||
|
|
||||||
|
implClass.addMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateCountByTrashWithPageMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
|
Method method = new Method("countByTrashWithPage");
|
||||||
|
method.addAnnotation("@Override");
|
||||||
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
method.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
|
||||||
|
method.addBodyLine("// When not paginated, the count query returns 0 to avoid unnecessary queries");
|
||||||
|
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||||
|
method.addBodyLine("return countByTrash(example);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return 0L;");
|
||||||
|
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.iqudoo.framework.mybatis;
|
package com.iqudoo.framework.mybatis;
|
||||||
|
|
||||||
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
import com.iqudoo.framework.mybatis.utils.ElementTools;
|
||||||
|
import com.iqudoo.framework.mybatis.utils.UtilTools;
|
||||||
import org.mybatis.generator.api.GeneratedJavaFile;
|
import org.mybatis.generator.api.GeneratedJavaFile;
|
||||||
import org.mybatis.generator.api.IntrospectedTable;
|
import org.mybatis.generator.api.IntrospectedTable;
|
||||||
import org.mybatis.generator.api.JavaFormatter;
|
import org.mybatis.generator.api.JavaFormatter;
|
||||||
@@ -14,62 +15,65 @@ import java.io.File;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@SuppressWarnings({"DuplicatedCode", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
@SuppressWarnings({"DuplicatedCode", "SpellCheckingInspection", "ExtractMethodRecommender"})
|
||||||
public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
||||||
|
|
||||||
// 视图Repo包配置(可通过配置文件自定义)
|
// 视图Repo包配置(可通过配置文件自定义)
|
||||||
private String slowQueryLoggerTime = "300";
|
private String slowQueryLoggerTime = "300";
|
||||||
|
private String slowQueryLoggerLevel = "error";
|
||||||
private String facadeRepoviewPackage = "com.iqudoo.platform.application.facade.repoview";
|
private String facadeRepoviewPackage = "com.iqudoo.platform.application.facade.repoview";
|
||||||
private String domainRepoviewPackage = "com.iqudoo.platform.application.domain.repoview";
|
private String domainRepoviewPackage = "com.iqudoo.platform.application.domain.repoview";
|
||||||
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
private String modelPackage = "com.iqudoo.platform.application.database.model";
|
||||||
private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
|
private String mapperPackage = "com.iqudoo.platform.application.database.mapper";
|
||||||
private String targetProject = "src/main/java";
|
private String targetProject = "src/main/java";
|
||||||
|
|
||||||
// 视图名称关键字(可配置)
|
|
||||||
private String viewKeyWords = "VIEW_,V_";
|
private String viewKeyWords = "VIEW_,V_";
|
||||||
|
|
||||||
// 1.4.1版本专用格式化器
|
// 1.4.1版本专用格式化器
|
||||||
private JavaFormatter javaFormatter;
|
private JavaFormatter javaFormatter;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setContext(Context context) {
|
|
||||||
super.setContext(context);
|
|
||||||
this.javaFormatter = new DefaultJavaFormatter();
|
|
||||||
this.javaFormatter.setContext(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(List<String> warnings) {
|
public boolean validate(List<String> warnings) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setProperties(Properties properties) {
|
public void setContext(Context context) {
|
||||||
super.setProperties(properties);
|
super.setContext(context);
|
||||||
// 读取自定义配置
|
this.javaFormatter = new DefaultJavaFormatter();
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("slowQueryLoggerTime"))) {
|
this.javaFormatter.setContext(context);
|
||||||
slowQueryLoggerTime = properties.getProperty("slowQueryLoggerTime");
|
resolveConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resolveConfiguration() {
|
||||||
|
slowQueryLoggerTime = stringConfig("slowQueryLoggerTime", slowQueryLoggerTime);
|
||||||
|
slowQueryLoggerLevel = stringConfig("slowQueryLoggerLevel", slowQueryLoggerLevel);
|
||||||
|
if (!UtilTools.inArray(new String[]{"error", "warn", "debug", "info"}, slowQueryLoggerLevel)) {
|
||||||
|
slowQueryLoggerLevel = "error";
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("facadeRepoviewPackage"))) {
|
facadeRepoviewPackage = stringConfig("facadeRepoviewPackage", facadeRepoviewPackage);
|
||||||
facadeRepoviewPackage = properties.getProperty("facadeRepoviewPackage");
|
domainRepoviewPackage = stringConfig("domainRepoviewPackage", domainRepoviewPackage);
|
||||||
|
modelPackage = stringConfig("modelPackage", modelPackage);
|
||||||
|
mapperPackage = stringConfig("mapperPackage", mapperPackage);
|
||||||
|
targetProject = stringConfig("targetProject", targetProject);
|
||||||
|
viewKeyWords = stringConfig("viewKeyWords", viewKeyWords);
|
||||||
|
if (StringUtility.stringHasValue(viewKeyWords)) {
|
||||||
|
viewKeyWords = viewKeyWords.toUpperCase();
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("domainRepoviewPackage"))) {
|
}
|
||||||
domainRepoviewPackage = properties.getProperty("domainRepoviewPackage");
|
|
||||||
|
private String stringConfig(String key, String defaultValue) {
|
||||||
|
String v = properties.getProperty(key);
|
||||||
|
if (StringUtility.stringHasValue(v)) {
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("modelPackage"))) {
|
if (context != null) {
|
||||||
modelPackage = properties.getProperty("modelPackage");
|
v = context.getProperty(key);
|
||||||
}
|
if (StringUtility.stringHasValue(v)) {
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("mapperPackage"))) {
|
return v;
|
||||||
mapperPackage = properties.getProperty("mapperPackage");
|
}
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("targetProject"))) {
|
|
||||||
targetProject = properties.getProperty("targetProject");
|
|
||||||
}
|
|
||||||
if (StringUtility.stringHasValue(properties.getProperty("viewKeyWords"))) {
|
|
||||||
viewKeyWords = properties.getProperty("viewKeyWords").toUpperCase();
|
|
||||||
}
|
}
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -183,6 +187,15 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
|||||||
countMethod.setAbstract(true);
|
countMethod.setAbstract(true);
|
||||||
repoInterface.addMethod(countMethod);
|
repoInterface.addMethod(countMethod);
|
||||||
|
|
||||||
|
// 4. 添加countWithPage方法
|
||||||
|
Method countWithPageMethod = new Method("countWithPage");
|
||||||
|
countWithPageMethod.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
countWithPageMethod.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
countWithPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
countWithPageMethod.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
countWithPageMethod.setAbstract(true);
|
||||||
|
repoInterface.addMethod(countWithPageMethod);
|
||||||
|
|
||||||
return repoInterface;
|
return repoInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,12 +238,10 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
|||||||
mapperField.addAnnotation("@Resource");
|
mapperField.addAnnotation("@Resource");
|
||||||
implClass.addField(mapperField);
|
implClass.addField(mapperField);
|
||||||
|
|
||||||
// 生成findOne方法
|
generateFindOneMethod(implClass, modelClassName, exampleClassName);
|
||||||
generateFindOneMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
|
||||||
// 生成getList方法
|
|
||||||
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
generateGetListMethod(implClass, modelClassName, exampleClassName, mapperFieldName, hasBLOBColumns);
|
||||||
// 生成count方法
|
generateCountMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
generateCountMethod(implClass, exampleClassName, mapperFieldName);
|
generateCountWithPageMethod(implClass, modelClassName, exampleClassName, mapperFieldName);
|
||||||
|
|
||||||
return implClass;
|
return implClass;
|
||||||
}
|
}
|
||||||
@@ -251,14 +262,14 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
|||||||
implClass.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
|
implClass.addImportedType(new FullyQualifiedJavaType("javax.annotation.Resource"));
|
||||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.Logger"));
|
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.Logger"));
|
||||||
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.LoggerFactory"));
|
implClass.addImportedType(new FullyQualifiedJavaType("org.slf4j.LoggerFactory"));
|
||||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.ArrayList"));
|
implClass.addImportedType(new FullyQualifiedJavaType("java.util.Date"));
|
||||||
implClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
implClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成findOne方法
|
* 生成findOne方法
|
||||||
*/
|
*/
|
||||||
private void generateFindOneMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
private void generateFindOneMethod(TopLevelClass implClass, String modelClassName, String exampleClassName) {
|
||||||
Method method = new Method("findOne");
|
Method method = new Method("findOne");
|
||||||
method.addAnnotation("@Override");
|
method.addAnnotation("@Override");
|
||||||
method.setVisibility(JavaVisibility.PUBLIC);
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
@@ -289,29 +300,33 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
|||||||
// 参数名匹配示例(首字母小写)
|
// 参数名匹配示例(首字母小写)
|
||||||
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
method.addException(new FullyQualifiedJavaType("Throwable"));
|
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
method.addBodyLine("List<" + modelClassName + "> result = null;");
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
|
||||||
if (hasBLOBColumns) {
|
if (hasBLOBColumns) {
|
||||||
method.addBodyLine("if (example.isWithBLOBs()) {");
|
method.addBodyLine("if (example.isWithBLOBs()) {");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExampleWithBLOBs(example);");
|
||||||
|
method.addBodyLine("} else {");
|
||||||
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
|
||||||
} else {
|
} else {
|
||||||
method.addBodyLine("return " + mapperFieldName + ".selectByExample(example);");
|
method.addBodyLine("result = " + mapperFieldName + ".selectByExample(example);");
|
||||||
}
|
}
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"get view list use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " view list use long time: \" + useTime + \"ms\" +");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return result;");
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成count方法
|
* 生成count方法
|
||||||
*/
|
*/
|
||||||
private void generateCountMethod(TopLevelClass implClass, String exampleClassName, String mapperFieldName) {
|
private void generateCountMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
Method method = new Method("count");
|
Method method = new Method("count");
|
||||||
method.addAnnotation("@Override");
|
method.addAnnotation("@Override");
|
||||||
method.setVisibility(JavaVisibility.PUBLIC);
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
@@ -322,14 +337,38 @@ public class TapeRepoviewGeneratorPlugin extends PluginAdapter {
|
|||||||
|
|
||||||
// 方法体
|
// 方法体
|
||||||
method.addBodyLine("long startTime = new Date().getTime();");
|
method.addBodyLine("long startTime = new Date().getTime();");
|
||||||
method.addBodyLine("try {");
|
method.addBodyLine("long count = " + mapperFieldName + ".countByExample(example);");
|
||||||
method.addBodyLine("return " + mapperFieldName + ".countByExample(example);");
|
|
||||||
method.addBodyLine("} finally {");
|
|
||||||
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
method.addBodyLine("long useTime = new Date().getTime() - startTime;");
|
||||||
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
method.addBodyLine("if (useTime > " + slowQueryLoggerTime + ") {");
|
||||||
method.addBodyLine("LOGGER.error(\"count view use long time: \" + useTime + \"ms\");");
|
method.addBodyLine("LOGGER." + slowQueryLoggerLevel + "(\"Select " + modelClassName + " view count use long time: \" + useTime + \"ms\" +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> criteria: \" + example.getOredCriteria() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-> order by: \" + example.getOrderByClause() +");
|
||||||
|
method.addBodyLine(" \"\\n\\t|-----------------------------------\"");
|
||||||
|
method.addBodyLine(");");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return count;");
|
||||||
|
|
||||||
|
implClass.addMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成count方法
|
||||||
|
*/
|
||||||
|
private void generateCountWithPageMethod(TopLevelClass implClass, String modelClassName, String exampleClassName, String mapperFieldName) {
|
||||||
|
Method method = new Method("countWithPage");
|
||||||
|
method.addAnnotation("@Override");
|
||||||
|
method.setVisibility(JavaVisibility.PUBLIC);
|
||||||
|
method.setReturnType(new FullyQualifiedJavaType("long"));
|
||||||
|
// 参数名匹配示例(首字母小写)
|
||||||
|
method.addParameter(new Parameter(new FullyQualifiedJavaType(exampleClassName), "example"));
|
||||||
|
method.addException(new FullyQualifiedJavaType("Throwable"));
|
||||||
|
|
||||||
|
// 方法体
|
||||||
|
method.addBodyLine("// When not paginated, the count query returns 0 to avoid unnecessary queries");
|
||||||
|
method.addBodyLine("if (example.getRows() != null && example.getOffset() != null) {");
|
||||||
|
method.addBodyLine("return count(example);");
|
||||||
method.addBodyLine("}");
|
method.addBodyLine("}");
|
||||||
|
method.addBodyLine("return 0L;");
|
||||||
|
|
||||||
implClass.addMethod(method);
|
implClass.addMethod(method);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.iqudoo.framework.mybatis.utils;
|
||||||
|
|
||||||
|
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||||
|
public class UtilTools {
|
||||||
|
|
||||||
|
public static <T> boolean inArray(T[] array, T target) {
|
||||||
|
for (T val : array) {
|
||||||
|
if (val != null && val.equals(target)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user