Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0506b934d | ||
|
|
9868f0ed44 | ||
|
|
40aed67856 | ||
|
|
826ad11e64 | ||
|
|
a82128c10c | ||
|
|
3464bc28aa | ||
|
|
1589709d5c | ||
|
|
b1e23b3b51 | ||
|
|
a4700aa38c | ||
|
|
91322dc642 | ||
|
|
f0867171ff | ||
|
|
e4095dab7b | ||
|
|
c485d7beb1 | ||
|
|
03116e71c0 | ||
|
|
03f79f62a1 | ||
|
|
898568373a | ||
|
|
de89d4e521 | ||
|
|
62bdfb4af9 | ||
|
|
0a4f616253 | ||
|
|
e75e36af58 | ||
|
|
54d68e59b3 | ||
|
|
13e9ad5d55 | ||
|
|
8f8e84cbc0 |
@@ -19,7 +19,7 @@
|
||||
* [自定义注释插件(CommentPlugin)](#13-自定义注释插件)
|
||||
* [增量插件(IncrementsPlugin)](#14-增量插件)
|
||||
* [查询结果选择性返回插件(SelectSelectivePlugin)](#15-查询结果选择性返回插件)
|
||||
* [官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)](#16-官方ConstructorBased配置BUG临时修正插件)
|
||||
* [官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)](#16-官方constructorbased配置bug临时修正插件)
|
||||
|
||||
---------------------------------------
|
||||
Maven引用:
|
||||
@@ -27,7 +27,7 @@ Maven引用:
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.15</version>
|
||||
<version>1.0.19</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
@@ -206,6 +206,13 @@ public class Test {
|
||||
.field3("xx")
|
||||
.field4("xx")
|
||||
.build();
|
||||
// 或者使用builder静态方法创建Builder
|
||||
Tb table = Tb.builder()
|
||||
.field1("xx")
|
||||
.field2("xx")
|
||||
.field3("xx")
|
||||
.field4("xx")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -341,9 +348,11 @@ public class Test {
|
||||
```
|
||||
### 7. 逻辑删除插件
|
||||
因为很多实际项目数据都不允许物理删除,多采用逻辑删除,所以单独为逻辑删除做了一个插件,方便使用。
|
||||
- 增加logicalDeleteByExample和logicalDeleteByPrimaryKey方法;
|
||||
- 查询构造工具中增加逻辑删除条件andDeleted(boolean);
|
||||
- 增加逻辑删除常量DEL_FLAG_OFF(已删除)、DEL_FLAG_ON(未删除);
|
||||
- 增加logicalDeleteByExample和logicalDeleteByPrimaryKey方法;
|
||||
- 增加selectByPrimaryKeyWithLogicalDelete方法([[pull#12]](https://github.com/itfsw/mybatis-generator-plugin/pull/12));
|
||||
- 查询构造工具中增加逻辑删除条件andLogicalDeleted(boolean);
|
||||
- 数据Model增加逻辑删除条件andLogicalDeleted(boolean);
|
||||
- 增加逻辑删除常量IS_DELETED(已删除 默认值)、NOT_DELETED(未删除 默认值)([[issues#11]](https://github.com/itfsw/mybatis-generator-plugin/issues/11));
|
||||
|
||||
插件:
|
||||
```xml
|
||||
@@ -357,6 +366,10 @@ public class Test {
|
||||
<property name="logicalDeleteValue" value="9"/>
|
||||
<!-- 逻辑删除-未删除值 -->
|
||||
<property name="logicalUnDeleteValue" value="0"/>
|
||||
<!-- 逻辑删除常量名称,不配置默认为 IS_DELETED -->
|
||||
<property name="logicalDeleteValue" value="DEL"/>
|
||||
<!-- 逻辑删除常量(未删除)名称,不配置默认为 NOT_DELETED -->
|
||||
<property name="logicalUnDeleteValue" value="UN_DEL"/>
|
||||
</plugin>
|
||||
|
||||
<table tableName="tb">
|
||||
@@ -388,17 +401,22 @@ public class Test {
|
||||
.createCriteria()
|
||||
.andField1EqualTo(1)
|
||||
// 新增了一个andDeleted方法过滤逻辑删除数据
|
||||
.andDeleted(true)
|
||||
.andLogicalDeleted(true)
|
||||
// 当然也可直接使用逻辑删除列的查询方法,我们数据Model中定义了一个逻辑删除常量DEL_FLAG
|
||||
.andDelFlagEqualTo(Tb.DEL_FLAG)
|
||||
.andDelFlagEqualTo(Tb.IS_DELETED)
|
||||
.example()
|
||||
);
|
||||
|
||||
// 4. 逻辑删除和未删除常量
|
||||
Tb tb = new Tb.Builder()
|
||||
.delFlag(Tb.DEL_FLAG_OFF) // 删除
|
||||
.delFlag(Tb.DEL_FLAG_ON) // 未删除
|
||||
.build();
|
||||
.delFlag(Tb.IS_DELETED) // 删除
|
||||
.delFlag(Tb.NOT_DELETED) // 未删除
|
||||
.build()
|
||||
.andLogicalDeleted(true); // 也可以在这里使用true|false设置逻辑删除
|
||||
|
||||
// 5. selectByPrimaryKeyWithLogicalDelete V1.0.18 版本增加
|
||||
// 因为之前觉得既然拿到了主键这种查询没有必要,但是实际使用中可能存在根据主键判断是否逻辑删除的情况,这种场景还是有用的
|
||||
this.tbMapper.selectByPrimaryKeyWithLogicalDelete(1, true);
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -639,7 +657,7 @@ public class Test {
|
||||
```xml
|
||||
<xml>
|
||||
<!-- Table重命名插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.TablePrefixPlugin">
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.TableRenamePlugin">
|
||||
<!-- 可根据具体需求确定是否配置 -->
|
||||
<property name="searchString" value="^T"/>
|
||||
<property name="replaceString" value=""/>
|
||||
@@ -678,7 +696,7 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
|
||||
|
||||
>warning: 请注意拷贝参考模板注释前方空格,idea等工具拷贝进去后自动格式化会造成格式错乱。
|
||||
|
||||
>warning: 模板引擎采用的是freemarker所以一些freemarker指令参数(如:<#if xx></#if>、${.now?string("yyyy-MM-dd HH:mm:ss")})都是可以使用的,请自己尝试。
|
||||
>warning: 模板引擎采用的是freemarker所以一些freemarker指令参数(如:<#if xx></#if>、${.now?string("yyyy-MM-dd HH:mm:ss")})都是可以使用的,请自己尝试。
|
||||
|
||||
| 注释ID | 传入参数 | 备注 |
|
||||
| ----- | ----- | ---- |
|
||||
@@ -1061,4 +1079,4 @@ public class Test {
|
||||
<!-- 官方ConstructorBased配置BUG临时修正插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ConstructorBasedBugFixPlugin" />
|
||||
</xml>
|
||||
```
|
||||
```
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.15</version>
|
||||
<version>1.0.19</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
@@ -212,7 +212,7 @@
|
||||
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
|
||||
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
|
||||
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
|
||||
<tag>V1.0.15</tag>
|
||||
<tag>V1.0.19</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -29,7 +29,7 @@ import java.util.Properties;
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class CommentPlugin extends BasePlugin {
|
||||
public static final String PRE_TEMPLATE = "template"; // 模板 property
|
||||
public static final String PRO_TEMPLATE = "template"; // 模板 property
|
||||
|
||||
/**
|
||||
* 插件具体实现查看BasePlugin
|
||||
|
||||
@@ -35,7 +35,7 @@ import java.util.Properties;
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class ExampleTargetPlugin extends BasePlugin {
|
||||
public static final String TARGET_PACKAGE_KEY = "targetPackage"; // 配置targetPackage名
|
||||
public static final String PRO_TARGET_PACKAGE = "targetPackage"; // 配置targetPackage名
|
||||
private static String targetPackage; // 目标包
|
||||
|
||||
/**
|
||||
@@ -45,7 +45,7 @@ public class ExampleTargetPlugin extends BasePlugin {
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 获取配置的目标package
|
||||
Properties properties = getProperties();
|
||||
this.targetPackage = properties.getProperty(TARGET_PACKAGE_KEY);
|
||||
this.targetPackage = properties.getProperty(PRO_TARGET_PACKAGE);
|
||||
if (this.targetPackage == null){
|
||||
warnings.add("请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!");
|
||||
return false;
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.TopLevelClass;
|
||||
@@ -39,7 +36,7 @@ import java.util.List;
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class IncrementsPlugin extends BasePlugin {
|
||||
public static final String PRE_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
|
||||
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
|
||||
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
|
||||
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
|
||||
private IncrementsPluginTools incTools; // 增量插件工具
|
||||
@@ -184,7 +181,7 @@ public class IncrementsPlugin extends BasePlugin {
|
||||
TextElement textEle = (TextElement) textEles.get(0);
|
||||
String[] strs = textEle.getContent().split("=");
|
||||
String columnName = strs[0].trim();
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
// 查找是否需要进行增量操作
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
xmlElement.getElements().clear();
|
||||
@@ -209,11 +206,11 @@ public class IncrementsPlugin extends BasePlugin {
|
||||
// 找到text节点且格式为 set xx = xx 或者 xx = xx
|
||||
if (ele instanceof TextElement) {
|
||||
String text = ((TextElement) ele).getContent().trim();
|
||||
if (text.matches("(set\\s)?\\S+\\s?=.*")) {
|
||||
if (text.matches("(^set\\s)?\\S+\\s?=.*")) {
|
||||
// 清理 set 操作
|
||||
text = text.replaceFirst("set\\s", "").trim();
|
||||
text = text.replaceFirst("^set\\s", "").trim();
|
||||
String columnName = text.split("=")[0].trim();
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
// 查找判断是否需要进行节点替换
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
newEles.addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix, text.endsWith(",")));
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
@@ -27,10 +28,13 @@ 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.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.internal.util.JavaBeansUtil;
|
||||
|
||||
import java.sql.JDBCType;
|
||||
import java.util.*;
|
||||
|
||||
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 逻辑删除插件
|
||||
@@ -43,23 +47,29 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
public static final String METHOD_LOGICAL_DELETE_BY_EXAMPLE = "logicalDeleteByExample"; // 方法名
|
||||
public static final String METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY = "logicalDeleteByPrimaryKey"; // 方法名
|
||||
|
||||
public static final String LOGICAL_DELETE_COLUMN_KEY = "logicalDeleteColumn"; // 逻辑删除列-Key
|
||||
public static final String LOGICAL_DELETE_VALUE_KEY = "logicalDeleteValue"; // 逻辑删除值-Key
|
||||
public static final String LOGICAL_UN_DELETE_VALUE_KEY = "logicalUnDeleteValue"; // 逻辑删除未删除值-Key
|
||||
public static final String PRO_LOGICAL_DELETE_COLUMN = "logicalDeleteColumn"; // 逻辑删除列-Key
|
||||
public static final String PRO_LOGICAL_DELETE_VALUE = "logicalDeleteValue"; // 逻辑删除值-Key
|
||||
public static final String PRO_LOGICAL_UN_DELETE_VALUE = "logicalUnDeleteValue"; // 逻辑删除未删除值-Key
|
||||
|
||||
public static final String DEL_FLAG_NAME = "DEL_FLAG_OFF"; // 逻辑删除标志位常量名称
|
||||
public static final String UN_DEL_FLAG_NAME = "DEL_FLAG_ON"; // 逻辑删除标志位常量名称(未删除)
|
||||
public static final String PRO_LOGICAL_DELETE_CONST_NAME = "logicalDeleteConstName"; // 逻辑删除常量
|
||||
public static final String PRO_LOGICAL_UN_DELETE_CONST_NAME = "logicalUnDeleteConstName"; // 逻辑删除常量
|
||||
public static final String DEFAULT_LOGICAL_DELETE_CONST_NAME = "IS_DELETED"; // 逻辑删除标志位常量名称
|
||||
public static final String DEFAULT_LOGICAL_UN_DELETE_CONST_NAME = "NOT_DELETED"; // 逻辑删除标志位常量名称(未删除)
|
||||
|
||||
public static final String METHOD_LOGICAL_DELETE = "andDeleted"; // 逻辑删除查询方法
|
||||
public static final String METHOD_LOGICAL_DELETED = "andLogicalDeleted"; // 逻辑删除查询方法
|
||||
public static final String PARAMETER_LOGICAL_DELETED = METHOD_LOGICAL_DELETED; // 增强selectByPrimaryKey是参数名称
|
||||
|
||||
public static final String METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE = "selectByPrimaryKeyWithLogicalDelete"; // selectByPrimaryKey 的逻辑删除增强
|
||||
|
||||
private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列
|
||||
private String logicalDeleteValue; // 逻辑删除值
|
||||
private String logicalUnDeleteValue; // 逻辑删除值(未删除)
|
||||
private String logicalDeleteConstName; // 逻辑删除常量
|
||||
private String logicalUnDeleteConstName; // 逻辑删除常量(未删除)
|
||||
|
||||
/**
|
||||
* 初始化阶段
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@@ -67,24 +77,24 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
// 1. 首先获取全局配置
|
||||
Properties properties = getProperties();
|
||||
String logicalDeleteColumn = properties.getProperty(LOGICAL_DELETE_COLUMN_KEY);
|
||||
this.logicalDeleteValue = properties.getProperty(LOGICAL_DELETE_VALUE_KEY);
|
||||
this.logicalUnDeleteValue = properties.getProperty(LOGICAL_UN_DELETE_VALUE_KEY);
|
||||
String logicalDeleteColumn = properties.getProperty(PRO_LOGICAL_DELETE_COLUMN);
|
||||
this.logicalDeleteValue = properties.getProperty(PRO_LOGICAL_DELETE_VALUE);
|
||||
this.logicalUnDeleteValue = properties.getProperty(PRO_LOGICAL_UN_DELETE_VALUE);
|
||||
// 2. 获取表单独配置,如果有则覆盖全局配置
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){
|
||||
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY);
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) != null) {
|
||||
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN);
|
||||
}
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){
|
||||
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY);
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE) != null) {
|
||||
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE);
|
||||
}
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){
|
||||
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY);
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE) != null) {
|
||||
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE);
|
||||
}
|
||||
// 3. 判断该表是否存在逻辑删除列
|
||||
this.logicalDeleteColumn = null;
|
||||
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
|
||||
for (IntrospectedColumn column : columns) {
|
||||
if (column.getActualColumnName().equalsIgnoreCase(logicalDeleteColumn)){
|
||||
if (column.getActualColumnName().equalsIgnoreCase(logicalDeleteColumn)) {
|
||||
// 判断字段类型
|
||||
JDBCType type = JDBCType.valueOf(column.getJdbcType());
|
||||
if (JDBCType.BIGINT == type
|
||||
@@ -102,28 +112,40 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
|| JDBCType.NVARCHAR == type
|
||||
|| JDBCType.SMALLINT == type
|
||||
|| JDBCType.TINYINT == type
|
||||
|| JDBCType.VARCHAR == type){
|
||||
|| JDBCType.VARCHAR == type) {
|
||||
this.logicalDeleteColumn = column;
|
||||
} else {
|
||||
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")的类型不在支持范围(请使用数字列,字符串列,布尔列)!");
|
||||
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "逻辑删除列(" + introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) + ")的类型不在支持范围(请使用数字列,字符串列,布尔列)!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){
|
||||
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!");
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) != null && this.logicalDeleteColumn == null) {
|
||||
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "没有找到您配置的逻辑删除列(" + introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_COLUMN) + ")!");
|
||||
}
|
||||
|
||||
// 4. 判断逻辑删除值是否配置了
|
||||
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){
|
||||
warnings.add("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
|
||||
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)) {
|
||||
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
|
||||
}
|
||||
|
||||
// 5. 获取逻辑删除常量值
|
||||
this.logicalDeleteConstName = properties.getProperty(PRO_LOGICAL_DELETE_CONST_NAME) != null ? properties.getProperty(PRO_LOGICAL_DELETE_CONST_NAME).toUpperCase() : DEFAULT_LOGICAL_DELETE_CONST_NAME;
|
||||
this.logicalUnDeleteConstName = properties.getProperty(PRO_LOGICAL_UN_DELETE_CONST_NAME) != null ? properties.getProperty(PRO_LOGICAL_UN_DELETE_CONST_NAME).toUpperCase() : DEFAULT_LOGICAL_UN_DELETE_CONST_NAME;
|
||||
|
||||
// 6. 防止增强的selectByPrimaryKey中逻辑删除键冲突
|
||||
if (this.logicalDeleteColumn != null) {
|
||||
Field logicalDeleteField = JavaBeansUtil.getJavaBeansField(this.logicalDeleteColumn, context, introspectedTable);
|
||||
if (logicalDeleteField.getName().equals(PARAMETER_LOGICAL_DELETED)) {
|
||||
this.logicalDeleteColumn = null;
|
||||
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "配置的逻辑删除列和插件保留关键字(" + PARAMETER_LOGICAL_DELETED + ")冲突!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Java Client Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param interfaze
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
@@ -131,7 +153,7 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
if (this.logicalDeleteColumn != null) {
|
||||
// 1. 逻辑删除ByExample
|
||||
Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_LOGICAL_DELETE_BY_EXAMPLE,
|
||||
@@ -144,21 +166,34 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mLogicalDeleteByExample);
|
||||
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByExample。");
|
||||
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。");
|
||||
|
||||
// 2. 判断是否有主键,生成主键删除方法
|
||||
if (introspectedTable.hasPrimaryKeyColumns()){
|
||||
// 1. 逻辑删除ByExample
|
||||
Method mLogicalDeleteByPrimaryKey = new Method(METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY);
|
||||
// 返回值类型
|
||||
mLogicalDeleteByPrimaryKey.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
if (introspectedTable.hasPrimaryKeyColumns()) {
|
||||
// 2.1. 逻辑删除ByExample
|
||||
Method mLogicalDeleteByPrimaryKey = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance()
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
|
||||
|
||||
// 2.2 增强selectByPrimaryKey
|
||||
Method mSelectByPrimaryKey = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE,
|
||||
JavaVisibility.DEFAULT,
|
||||
introspectedTable.getRules().calculateAllFieldsClass()
|
||||
);
|
||||
|
||||
// 添加参数
|
||||
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
|
||||
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
|
||||
importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
|
||||
|
||||
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
|
||||
FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
|
||||
importedTypes.add(type1);
|
||||
mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key")); //$NON-NLS-1$
|
||||
mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key"));
|
||||
mSelectByPrimaryKey.addParameter(new Parameter(type1, "key", "@Param(\"key\")"));
|
||||
} else {
|
||||
// no primary key class - fields are in the base class
|
||||
// if more than one PK field, then we need to annotate the
|
||||
@@ -166,9 +201,6 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
// for MyBatis
|
||||
List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
|
||||
boolean annotate = introspectedColumns.size() > 1;
|
||||
if (annotate) {
|
||||
importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param")); //$NON-NLS-1$
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
|
||||
FullyQualifiedJavaType type1 = introspectedColumn.getFullyQualifiedJavaType();
|
||||
@@ -176,21 +208,38 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
Parameter parameter = new Parameter(type1, introspectedColumn.getJavaProperty());
|
||||
if (annotate) {
|
||||
sb.setLength(0);
|
||||
sb.append("@Param(\""); //$NON-NLS-1$
|
||||
sb.append("@Param(\"");
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append("\")"); //$NON-NLS-1$
|
||||
sb.append("\")");
|
||||
parameter.addAnnotation(sb.toString());
|
||||
}
|
||||
mLogicalDeleteByPrimaryKey.addParameter(parameter);
|
||||
|
||||
|
||||
Parameter parameter1 = new Parameter(type1, introspectedColumn.getJavaProperty());
|
||||
sb.setLength(0);
|
||||
sb.append("@Param(\"");
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append("\")");
|
||||
parameter1.addAnnotation(sb.toString());
|
||||
|
||||
mSelectByPrimaryKey.addParameter(parameter1);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加方法说明
|
||||
commentGenerator.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addImportedTypes(importedTypes);
|
||||
interfaze.addMethod(mLogicalDeleteByPrimaryKey);
|
||||
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByPrimaryKey。");
|
||||
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。");
|
||||
|
||||
// 增强selectByPrimaryKey
|
||||
mSelectByPrimaryKey.addParameter(new Parameter(
|
||||
FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
|
||||
PARAMETER_LOGICAL_DELETED,
|
||||
"@Param(\"" + PARAMETER_LOGICAL_DELETED + "\")"
|
||||
));
|
||||
commentGenerator.addGeneralMethodComment(mSelectByPrimaryKey, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(interfaze, mSelectByPrimaryKey);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -199,22 +248,21 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
if (this.logicalDeleteColumn != null) {
|
||||
// 1. 逻辑删除ByExample
|
||||
XmlElement logicalDeleteByExample = new XmlElement("update"); //$NON-NLS-1$
|
||||
XmlElement logicalDeleteByExample = new XmlElement("update");
|
||||
logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
|
||||
logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-2$
|
||||
commentGenerator.addComment(logicalDeleteByExample);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("update "); //$NON-NLS-1$
|
||||
sb.append("update ");
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
sb.append(" set ");
|
||||
// 更新逻辑删除字段
|
||||
@@ -223,14 +271,14 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
|
||||
// 判断字段类型
|
||||
JDBCType type = JDBCType.valueOf(this.logicalDeleteColumn.getJdbcType());
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)) {
|
||||
sb.append("NULL");
|
||||
} else if (JDBCType.CHAR == type
|
||||
|| JDBCType.LONGNVARCHAR == type
|
||||
|| JDBCType.LONGVARCHAR == type
|
||||
|| JDBCType.NCHAR == type
|
||||
|| JDBCType.NVARCHAR == type
|
||||
|| JDBCType.VARCHAR == type){
|
||||
|| JDBCType.VARCHAR == type) {
|
||||
sb.append("'");
|
||||
sb.append(this.logicalDeleteValue);
|
||||
sb.append("'");
|
||||
@@ -242,11 +290,11 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
|
||||
logicalDeleteByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
document.getRootElement().addElement(logicalDeleteByExample);
|
||||
logger.debug("itfsw(逻辑删除插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"增加方法logicalDeleteByExample的实现。");
|
||||
logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByExample的实现。");
|
||||
|
||||
// 2. 判断是否有主键,生成主键删除方法
|
||||
if (introspectedTable.hasPrimaryKeyColumns()){
|
||||
XmlElement logicalDeleteByPrimaryKey = new XmlElement("update"); //$NON-NLS-1$
|
||||
if (introspectedTable.hasPrimaryKeyColumns()) {
|
||||
XmlElement logicalDeleteByPrimaryKey = new XmlElement("update");
|
||||
logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY));
|
||||
|
||||
String parameterClass;
|
||||
@@ -256,7 +304,7 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
// PK fields are in the base class. If more than on PK
|
||||
// field, then they are coming in a map.
|
||||
if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
|
||||
parameterClass = "map"; //$NON-NLS-1$
|
||||
parameterClass = "map";
|
||||
} else {
|
||||
parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
|
||||
}
|
||||
@@ -265,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
commentGenerator.addComment(logicalDeleteByPrimaryKey);
|
||||
|
||||
StringBuilder sb1 = new StringBuilder();
|
||||
sb1.append("update "); //$NON-NLS-1$
|
||||
sb1.append("update ");
|
||||
sb1.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
sb1.append(" set ");
|
||||
// 更新逻辑删除字段
|
||||
@@ -274,14 +322,14 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
|
||||
// 判断字段类型
|
||||
JDBCType type1 = JDBCType.valueOf(this.logicalDeleteColumn.getJdbcType());
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)) {
|
||||
sb1.append("NULL");
|
||||
} else if (JDBCType.CHAR == type1
|
||||
|| JDBCType.LONGNVARCHAR == type1
|
||||
|| JDBCType.LONGVARCHAR == type1
|
||||
|| JDBCType.NCHAR == type1
|
||||
|| JDBCType.NVARCHAR == type1
|
||||
|| JDBCType.VARCHAR == type1){
|
||||
|| JDBCType.VARCHAR == type1) {
|
||||
sb1.append("'");
|
||||
sb1.append(this.logicalDeleteValue);
|
||||
sb1.append("'");
|
||||
@@ -295,87 +343,212 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
sb.setLength(0);
|
||||
if (and) {
|
||||
sb.append(" and "); //$NON-NLS-1$
|
||||
sb.append(" and ");
|
||||
} else {
|
||||
sb.append("where "); //$NON-NLS-1$
|
||||
sb.append("where ");
|
||||
and = true;
|
||||
}
|
||||
|
||||
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = "); //$NON-NLS-1$
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
logicalDeleteByPrimaryKey.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
document.getRootElement().addElement(logicalDeleteByPrimaryKey);
|
||||
logger.debug("itfsw(逻辑删除插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"增加方法logicalDeleteByPrimaryKey的实现。");
|
||||
logger.debug("itfsw(逻辑删除插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加方法logicalDeleteByPrimaryKey的实现。");
|
||||
|
||||
|
||||
// 3. 增强selectByPrimaryKey
|
||||
XmlElement selectByPrimaryKey = new XmlElement("select");
|
||||
commentGenerator.addComment(selectByPrimaryKey);
|
||||
|
||||
selectByPrimaryKey.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE));
|
||||
if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
|
||||
selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
|
||||
} else {
|
||||
selectByPrimaryKey.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
}
|
||||
|
||||
selectByPrimaryKey.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
context.getCommentGenerator().addComment(selectByPrimaryKey);
|
||||
|
||||
sb = new StringBuilder();
|
||||
sb.append("select ");
|
||||
|
||||
if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
|
||||
sb.append('\'');
|
||||
sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
|
||||
sb.append("' as QUERYID,");
|
||||
}
|
||||
selectByPrimaryKey.addElement(new TextElement(sb.toString()));
|
||||
selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
|
||||
if (introspectedTable.hasBLOBColumns()) {
|
||||
selectByPrimaryKey.addElement(new TextElement(","));
|
||||
selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
|
||||
}
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append("from ");
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
selectByPrimaryKey.addElement(new TextElement(sb.toString()));
|
||||
|
||||
and = false;
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
sb.setLength(0);
|
||||
if (and) {
|
||||
sb.append(" and ");
|
||||
} else {
|
||||
sb.append("where ");
|
||||
and = true;
|
||||
}
|
||||
|
||||
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
selectByPrimaryKey.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
// 逻辑删除的判断
|
||||
sb.setLength(0);
|
||||
sb.append(" and ");
|
||||
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(this.logicalDeleteColumn));
|
||||
sb.append(" = ");
|
||||
selectByPrimaryKey.addElement(new TextElement(sb.toString()));
|
||||
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", PARAMETER_LOGICAL_DELETED));
|
||||
whenEle.addElement(new TextElement("'" + this.logicalDeleteValue + "'"));
|
||||
chooseEle.addElement(whenEle);
|
||||
XmlElement otherwiseEle = new XmlElement("otherwise");
|
||||
otherwiseEle.addElement(new TextElement("'" + this.logicalUnDeleteValue + "'"));
|
||||
chooseEle.addElement(otherwiseEle);
|
||||
selectByPrimaryKey.addElement(chooseEle);
|
||||
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectByPrimaryKey);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
this.generateModelMethodsAndFields(topLevelClass, introspectedTable);
|
||||
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Model 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
this.generateModelMethodsAndFields(topLevelClass, introspectedTable);
|
||||
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
|
||||
|
||||
// 添加删除标志位常量 DEL_FLAG_OFF
|
||||
String delFlagOnValue;
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
|
||||
delFlagOnValue = "null";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
|
||||
delFlagOnValue = "\"" + this.logicalDeleteValue + "\"";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){
|
||||
delFlagOnValue = (this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
|
||||
} else {
|
||||
delFlagOnValue = this.logicalDeleteValue;
|
||||
/**
|
||||
* 生成Model 逻辑删除相关方法和常量
|
||||
* !!! bugfix:逻辑删除列作为复合主键的一部分的情况
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void generateModelMethodsAndFields(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){
|
||||
if (this.logicalDeleteColumn != null) {
|
||||
// 常量和逻辑删除方法跟随 逻辑删除列走
|
||||
boolean flag = false;
|
||||
for (Field field : topLevelClass.getFields()){
|
||||
if (this.logicalDeleteColumn.getJavaProperty().equals(field.getName())){
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue);
|
||||
commentGenerator.addFieldComment(field, introspectedTable);
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field);
|
||||
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_OFF的常量。");
|
||||
|
||||
// 添加删除标志位常量 DEL_FLAG_ON
|
||||
String unDelFlagOnValue;
|
||||
if (this.logicalUnDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalUnDeleteValue)){
|
||||
unDelFlagOnValue = "null";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
|
||||
unDelFlagOnValue = "\"" + this.logicalUnDeleteValue + "\"";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){
|
||||
unDelFlagOnValue = (this.logicalUnDeleteValue.equals("1") || this.logicalUnDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
|
||||
} else {
|
||||
unDelFlagOnValue = this.logicalUnDeleteValue;
|
||||
if (flag){
|
||||
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
|
||||
|
||||
// 添加删除标志位常量
|
||||
String logicalDeleteValue;
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)) {
|
||||
logicalDeleteValue = "null";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")) {
|
||||
logicalDeleteValue = "\"" + this.logicalDeleteValue + "\"";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")) {
|
||||
logicalDeleteValue = (this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
|
||||
} else {
|
||||
logicalDeleteValue = this.logicalDeleteValue;
|
||||
}
|
||||
// TODO 过期
|
||||
Field field = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG_OFF", this.logicalDeleteColumn.getFullyQualifiedJavaType(), logicalDeleteValue);
|
||||
field.addAnnotation("@Deprecated");
|
||||
commentGenerator.addFieldComment(field, introspectedTable);
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field);
|
||||
|
||||
Field logicalDeleteConstField = JavaElementGeneratorTools.generateStaticFinalField(this.logicalDeleteConstName, this.logicalDeleteColumn.getFullyQualifiedJavaType(), logicalDeleteValue);
|
||||
commentGenerator.addFieldComment(logicalDeleteConstField, introspectedTable);
|
||||
fields.add(0, logicalDeleteConstField);
|
||||
|
||||
|
||||
// 添加删除标志位常量 DEL_FLAG_ON
|
||||
String logicalUnDeleteValue;
|
||||
if (this.logicalUnDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalUnDeleteValue)) {
|
||||
logicalUnDeleteValue = "null";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")) {
|
||||
logicalUnDeleteValue = "\"" + this.logicalUnDeleteValue + "\"";
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")) {
|
||||
logicalUnDeleteValue = (this.logicalUnDeleteValue.equals("1") || this.logicalUnDeleteValue.equalsIgnoreCase("true")) ? "true" : "false";
|
||||
} else {
|
||||
logicalUnDeleteValue = this.logicalUnDeleteValue;
|
||||
}
|
||||
|
||||
// TODO 过期
|
||||
Field field1 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG_ON", this.logicalDeleteColumn.getFullyQualifiedJavaType(), logicalUnDeleteValue);
|
||||
field1.addAnnotation("@Deprecated");
|
||||
commentGenerator.addFieldComment(field1, introspectedTable);
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field1);
|
||||
|
||||
Field logicalUnDeleteConstField = JavaElementGeneratorTools.generateStaticFinalField(this.logicalUnDeleteConstName, this.logicalDeleteColumn.getFullyQualifiedJavaType(), logicalUnDeleteValue);
|
||||
commentGenerator.addFieldComment(logicalUnDeleteConstField, introspectedTable);
|
||||
fields.add(0, logicalUnDeleteConstField);
|
||||
|
||||
// ================================================= andLogicalDeleted =============================================
|
||||
Method mAndLogicalDeleted = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_LOGICAL_DELETED,
|
||||
JavaVisibility.PUBLIC,
|
||||
null,
|
||||
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "deleted")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mAndLogicalDeleted, introspectedTable);
|
||||
Method logicalDeleteSetter = JavaBeansUtil.getJavaBeansSetter(this.logicalDeleteColumn, context, introspectedTable);
|
||||
mAndLogicalDeleted.addBodyLine(logicalDeleteSetter.getName() +"(deleted ? " + this.logicalDeleteConstName + " : " + this.logicalUnDeleteConstName + ");");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mAndLogicalDeleted);
|
||||
}
|
||||
Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue);
|
||||
commentGenerator.addFieldComment(field1, introspectedTable);
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field1);
|
||||
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_ON的常量。");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* ModelExample Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
if (this.logicalDeleteColumn != null) {
|
||||
// 引入 Model类
|
||||
FullyQualifiedJavaType model = introspectedTable.getRules().calculateAllFieldsClass();
|
||||
topLevelClass.addImportedType(model);
|
||||
@@ -384,7 +557,7 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
for (InnerClass innerClass : innerClasses) {
|
||||
if ("Criteria".equals(innerClass.getType().getShortName())) {
|
||||
// 增加逻辑删除条件
|
||||
Method method = new Method(METHOD_LOGICAL_DELETE);
|
||||
Method method = new Method(METHOD_LOGICAL_DELETED);
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(innerClass.getType());
|
||||
@@ -399,10 +572,10 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
StringBuilder equalToMethodName = new StringBuilder();
|
||||
equalToMethodName.append(this.logicalDeleteColumn.getJavaProperty());
|
||||
equalToMethodName.setCharAt(0, Character.toUpperCase(equalToMethodName.charAt(0)));
|
||||
equalToMethodName.insert(0, "and"); //$NON-NLS-1$
|
||||
equalToMethodName.insert(0, "and");
|
||||
equalToMethodName.append("EqualTo");
|
||||
sb.append(equalToMethodName);
|
||||
sb.append("("+modelName + "." + DEL_FLAG_NAME + ")");
|
||||
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
|
||||
|
||||
sb.append(" : ");
|
||||
|
||||
@@ -410,17 +583,23 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
StringBuilder notEqualToMethodName = new StringBuilder();
|
||||
notEqualToMethodName.append(this.logicalDeleteColumn.getJavaProperty());
|
||||
notEqualToMethodName.setCharAt(0, Character.toUpperCase(notEqualToMethodName.charAt(0)));
|
||||
notEqualToMethodName.insert(0, "and"); //$NON-NLS-1$
|
||||
notEqualToMethodName.insert(0, "and");
|
||||
notEqualToMethodName.append("NotEqualTo");
|
||||
sb.append(notEqualToMethodName);
|
||||
sb.append("("+modelName + "." + DEL_FLAG_NAME + ")");
|
||||
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
|
||||
|
||||
sb.append(";");
|
||||
|
||||
method.addBodyLine(sb.toString());
|
||||
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加andDeleted方法。");
|
||||
|
||||
// TODO 过期方法
|
||||
Method mAndDeleted = JavaElementGeneratorTools.cloneMethod(method);
|
||||
mAndDeleted.setName("andDeleted");
|
||||
mAndDeleted.addAnnotation("@Deprecated");
|
||||
innerClass.addMethod(mAndDeleted);
|
||||
logger.debug("itfsw(逻辑删除插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加andDeleted方法。");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,15 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.internal.util.JavaBeansUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -37,6 +40,7 @@ import java.util.List;
|
||||
*/
|
||||
public class ModelBuilderPlugin extends BasePlugin {
|
||||
public static final String BUILDER_CLASS_NAME = "Builder"; // Builder 类名
|
||||
private Map<IntrospectedTable, InnerTypeFullyQualifiedJavaType> innerClasses = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Model Methods 生成
|
||||
@@ -49,7 +53,7 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// 判断是否有生成Model的WithBLOBs类
|
||||
List<IntrospectedColumn> columns = introspectedTable.getRules().generateRecordWithBLOBsClass() ? introspectedTable.getNonBLOBColumns() : introspectedTable.getAllColumns();
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, columns, true);
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, columns);
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
@@ -63,7 +67,7 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getAllColumns(), false);
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getAllColumns());
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
@@ -76,7 +80,7 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getPrimaryKeyColumns(), false);
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getPrimaryKeyColumns());
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
@@ -86,14 +90,34 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @param columns
|
||||
* @param modelBaseRecord
|
||||
* @return
|
||||
*/
|
||||
private InnerClass generateModelBuilder(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns, boolean modelBaseRecord) {
|
||||
private InnerClass generateModelBuilder(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns) {
|
||||
// 生成内部Builder类
|
||||
InnerClass innerClass = new InnerClass(BUILDER_CLASS_NAME);
|
||||
innerClass.setVisibility(JavaVisibility.PUBLIC);
|
||||
innerClass.setStatic(true);
|
||||
|
||||
// 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
// 顺序为 key base withBLOBs
|
||||
InnerTypeFullyQualifiedJavaType builderType = new InnerTypeFullyQualifiedJavaType(topLevelClass.getType().getFullyQualifiedName() + "." + BUILDER_CLASS_NAME);
|
||||
if (innerClasses.get(introspectedTable) != null) {
|
||||
innerClass.setSuperClass(innerClasses.get(introspectedTable));
|
||||
innerClasses.remove(introspectedTable);
|
||||
}
|
||||
innerClasses.put(introspectedTable, builderType);
|
||||
|
||||
// 增加静态builder方法实现和lombok一样
|
||||
Method builder = JavaElementGeneratorTools.generateMethod(
|
||||
"builder",
|
||||
JavaVisibility.PUBLIC,
|
||||
builderType
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(builder, introspectedTable);
|
||||
builder.setStatic(true);
|
||||
builder.addBodyLine("return new " + builderType.getShortName() + "();");
|
||||
topLevelClass.addMethod(builder);
|
||||
|
||||
commentGenerator.addClassComment(innerClass, introspectedTable);
|
||||
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
|
||||
|
||||
@@ -146,62 +170,60 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
// ========================================== IncrementsPlugin =======================================
|
||||
IncrementsPluginTools incTools = IncrementsPluginTools.getTools(context, introspectedTable, warnings);
|
||||
if (incTools.support()) {
|
||||
if (modelBaseRecord) {
|
||||
// 增加枚举
|
||||
InnerEnum eIncrements = new InnerEnum(new FullyQualifiedJavaType("Inc"));
|
||||
eIncrements.setVisibility(JavaVisibility.PUBLIC);
|
||||
eIncrements.setStatic(true);
|
||||
eIncrements.addEnumConstant("INC(\"+\")");
|
||||
eIncrements.addEnumConstant("DEC(\"-\")");
|
||||
commentGenerator.addEnumComment(eIncrements, introspectedTable);
|
||||
// 生成属性和构造函数
|
||||
Field fValue = new Field("value", FullyQualifiedJavaType.getStringInstance());
|
||||
fValue.setVisibility(JavaVisibility.PRIVATE);
|
||||
fValue.setFinal(true);
|
||||
commentGenerator.addFieldComment(fValue, introspectedTable);
|
||||
eIncrements.addField(fValue);
|
||||
// 增加枚举
|
||||
InnerEnum eIncrements = new InnerEnum(new FullyQualifiedJavaType("Inc"));
|
||||
eIncrements.setVisibility(JavaVisibility.PUBLIC);
|
||||
eIncrements.setStatic(true);
|
||||
eIncrements.addEnumConstant("INC(\"+\")");
|
||||
eIncrements.addEnumConstant("DEC(\"-\")");
|
||||
commentGenerator.addEnumComment(eIncrements, introspectedTable);
|
||||
// 生成属性和构造函数
|
||||
Field fValue = new Field("value", FullyQualifiedJavaType.getStringInstance());
|
||||
fValue.setVisibility(JavaVisibility.PRIVATE);
|
||||
fValue.setFinal(true);
|
||||
commentGenerator.addFieldComment(fValue, introspectedTable);
|
||||
eIncrements.addField(fValue);
|
||||
|
||||
Method mInc = new Method("Inc");
|
||||
mInc.setConstructor(true);
|
||||
mInc.addBodyLine("this.value = value;");
|
||||
mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value"));
|
||||
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
|
||||
eIncrements.addMethod(mInc);
|
||||
Method mInc = new Method("Inc");
|
||||
mInc.setConstructor(true);
|
||||
mInc.addBodyLine("this.value = value;");
|
||||
mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value"));
|
||||
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
|
||||
eIncrements.addMethod(mInc);
|
||||
|
||||
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
|
||||
eIncrements.addMethod(mValue);
|
||||
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
|
||||
eIncrements.addMethod(mValue);
|
||||
|
||||
innerClass.addInnerEnum(eIncrements);
|
||||
// 增加field
|
||||
Field fIncrements = JavaElementGeneratorTools.generateField(
|
||||
IncrementsPlugin.FIELD_INC_MAP,
|
||||
JavaVisibility.PROTECTED,
|
||||
new FullyQualifiedJavaType("Map<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">"),
|
||||
"new HashMap<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">()"
|
||||
);
|
||||
commentGenerator.addFieldComment(fIncrements, introspectedTable);
|
||||
topLevelClass.addField(fIncrements);
|
||||
topLevelClass.addImportedType("java.util.Map");
|
||||
topLevelClass.addImportedType("java.util.HashMap");
|
||||
// getter&setter
|
||||
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
|
||||
commentGenerator.addGetterComment(mGetter, introspectedTable, null);
|
||||
topLevelClass.addMethod(mGetter);
|
||||
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
|
||||
commentGenerator.addSetterComment(mSetter, introspectedTable, null);
|
||||
topLevelClass.addMethod(mSetter);
|
||||
// 增加判断方法
|
||||
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
|
||||
IncrementsPlugin.METHOD_INC_CHECK,
|
||||
JavaVisibility.PUBLIC,
|
||||
FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
|
||||
new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mHasIncsForColumn, introspectedTable);
|
||||
mHasIncsForColumn.addBodyLine("return " + IncrementsPlugin.FIELD_INC_MAP + ".get(column) != null;");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mHasIncsForColumn);
|
||||
}
|
||||
innerClass.addInnerEnum(eIncrements);
|
||||
// 增加field
|
||||
Field fIncrements = JavaElementGeneratorTools.generateField(
|
||||
IncrementsPlugin.FIELD_INC_MAP,
|
||||
JavaVisibility.PROTECTED,
|
||||
new FullyQualifiedJavaType("Map<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">"),
|
||||
"new HashMap<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">()"
|
||||
);
|
||||
commentGenerator.addFieldComment(fIncrements, introspectedTable);
|
||||
topLevelClass.addField(fIncrements);
|
||||
topLevelClass.addImportedType("java.util.Map");
|
||||
topLevelClass.addImportedType("java.util.HashMap");
|
||||
// getter&setter
|
||||
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
|
||||
commentGenerator.addGetterComment(mGetter, introspectedTable, null);
|
||||
topLevelClass.addMethod(mGetter);
|
||||
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
|
||||
commentGenerator.addSetterComment(mSetter, introspectedTable, null);
|
||||
topLevelClass.addMethod(mSetter);
|
||||
// 增加判断方法
|
||||
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
|
||||
IncrementsPlugin.METHOD_INC_CHECK,
|
||||
JavaVisibility.PUBLIC,
|
||||
FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
|
||||
new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mHasIncsForColumn, introspectedTable);
|
||||
mHasIncsForColumn.addBodyLine("return " + IncrementsPlugin.FIELD_INC_MAP + ".get(column) != null;");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mHasIncsForColumn);
|
||||
|
||||
// Builder 中 添加字段支持
|
||||
for (IntrospectedColumn column : columns) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
@@ -33,11 +34,13 @@ import java.util.regex.Pattern;
|
||||
* ---------------------------------------------------------------------------
|
||||
* Selective 增强插件
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* @author: hewei
|
||||
* @time:2017/4/20 15:39
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
public static final String METHOD_HAS_SELECTIVE = "hasSelective"; // 方法名
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
@@ -60,6 +63,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
/**
|
||||
* Model Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
@@ -77,22 +81,22 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
selectiveColumnsField.setInitializationString("new HashMap<String, Boolean>()");
|
||||
topLevelClass.addField(selectiveColumnsField);
|
||||
|
||||
// Method isSelective
|
||||
Method mIsSelective = new Method("isSelective");
|
||||
commentGenerator.addGeneralMethodComment(mIsSelective, introspectedTable);
|
||||
mIsSelective.setVisibility(JavaVisibility.PUBLIC);
|
||||
mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
|
||||
mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
|
||||
topLevelClass.addMethod(mIsSelective);
|
||||
// Method hasSelective
|
||||
Method mHasSelective = new Method(METHOD_HAS_SELECTIVE);
|
||||
commentGenerator.addGeneralMethodComment(mHasSelective, introspectedTable);
|
||||
mHasSelective.setVisibility(JavaVisibility.PUBLIC);
|
||||
mHasSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
|
||||
mHasSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
|
||||
topLevelClass.addMethod(mHasSelective);
|
||||
|
||||
// Method isSelective
|
||||
Method mIsSelective1 = new Method("isSelective");
|
||||
commentGenerator.addGeneralMethodComment(mIsSelective1, introspectedTable);
|
||||
mIsSelective1.setVisibility(JavaVisibility.PUBLIC);
|
||||
mIsSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
|
||||
mIsSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
|
||||
mIsSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
|
||||
topLevelClass.addMethod(mIsSelective1);
|
||||
// Method hasSelective
|
||||
Method mHasSelective1 = new Method(METHOD_HAS_SELECTIVE);
|
||||
commentGenerator.addGeneralMethodComment(mHasSelective1, introspectedTable);
|
||||
mHasSelective1.setVisibility(JavaVisibility.PUBLIC);
|
||||
mHasSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
|
||||
mHasSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
|
||||
mHasSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
|
||||
topLevelClass.addMethod(mHasSelective1);
|
||||
|
||||
// Method selective
|
||||
Method mSelective = new Method("selective");
|
||||
@@ -115,6 +119,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
@@ -181,6 +186,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
|
||||
/**
|
||||
* 替换节点if信息
|
||||
*
|
||||
* @param element
|
||||
* @param prefix
|
||||
* @param introspectedTable
|
||||
@@ -190,10 +196,10 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
// when
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
|
||||
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
|
||||
for (Element ele : element.getElements()) {
|
||||
// 对于字符串主键,是没有if判断节点的
|
||||
if (ele instanceof XmlElement){
|
||||
if (ele instanceof XmlElement) {
|
||||
// if的text节点
|
||||
XmlElement xmlElement = (XmlElement) ele;
|
||||
|
||||
@@ -204,7 +210,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
if (text.matches("#\\{.*\\},?")) {
|
||||
Pattern pattern = Pattern.compile("#\\{(.*?),.*\\},?");
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
if (matcher.find()){
|
||||
if (matcher.find()) {
|
||||
String field = matcher.group(1);
|
||||
// 查找对应column
|
||||
for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
|
||||
@@ -214,19 +220,21 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (text.matches(".*=.*")){
|
||||
if (text.matches(".*=.*")) {
|
||||
columnName = text.split("=")[0];
|
||||
} else {
|
||||
columnName = text.replaceAll(",", "");
|
||||
}
|
||||
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
|
||||
columnName = columnName.trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
// columnName = columnName.trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
}
|
||||
|
||||
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
|
||||
XmlElement ifEle = new XmlElement("if");
|
||||
|
||||
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedTable.getColumn(columnName)) + "\')"));
|
||||
for (Element ifChild : xmlElement.getElements()){
|
||||
ifEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + column.getActualColumnName() + "\')"));
|
||||
for (Element ifChild : xmlElement.getElements()) {
|
||||
ifEle.addElement(ifChild);
|
||||
}
|
||||
whenEle.addElement(ifEle);
|
||||
@@ -251,6 +259,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
|
||||
/**
|
||||
* 替换节点upsertByExampleSelective if信息
|
||||
*
|
||||
* @param element
|
||||
* @param prefix
|
||||
* @param introspectedTable
|
||||
@@ -261,10 +270,10 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
// when
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
|
||||
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
|
||||
for (IntrospectedColumn introspectedColumn : (allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns())) {
|
||||
XmlElement eleIf = new XmlElement("if");
|
||||
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + introspectedColumn.getActualColumnName() + "\')"));
|
||||
eleIf.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + introspectedColumn.getActualColumnName() + "\')"));
|
||||
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
|
||||
whenEle.addElement(eleIf);
|
||||
|
||||
@@ -33,7 +33,7 @@ import java.util.List;
|
||||
*/
|
||||
public class TablePrefixPlugin extends BasePlugin {
|
||||
|
||||
public static final String PRE_PREFIX = "prefix"; // 前缀 property
|
||||
public static final String PRO_PREFIX = "prefix"; // 前缀 property
|
||||
private String prefix; // 前缀
|
||||
|
||||
/**
|
||||
@@ -62,14 +62,14 @@ public class TablePrefixPlugin extends BasePlugin {
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
// 1. 首先获取全局配置
|
||||
this.prefix = getProperties().getProperty(PRE_PREFIX);
|
||||
this.prefix = getProperties().getProperty(PRO_PREFIX);
|
||||
// 2. 获取每个table 具体的
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null) {
|
||||
this.prefix = introspectedTable.getTableConfigurationProperty(PRE_PREFIX);
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_PREFIX) != null) {
|
||||
this.prefix = introspectedTable.getTableConfigurationProperty(PRO_PREFIX);
|
||||
}
|
||||
// 3. 判断是否配置了前缀
|
||||
// !!! TableRenamePlugin 插件的 tableOverride 优先级最高
|
||||
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRE_TABLE_OVERRIDE) == null) {
|
||||
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRO_TABLE_OVERRIDE) == null) {
|
||||
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
|
||||
domainObjectName = prefix + domainObjectName;
|
||||
try {
|
||||
|
||||
@@ -34,9 +34,9 @@ import java.util.regex.Pattern;
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class TableRenamePlugin extends BasePlugin {
|
||||
public static final String PRE_SEARCH_STRING = "searchString"; // 查找 property
|
||||
public static final String PRE_REPLACE_STRING = "replaceString"; // 替换 property
|
||||
public static final String PRE_TABLE_OVERRIDE = "tableOverride"; // table 重命名 property
|
||||
public static final String PRO_SEARCH_STRING = "searchString"; // 查找 property
|
||||
public static final String PRO_REPLACE_STRING = "replaceString"; // 替换 property
|
||||
public static final String PRO_TABLE_OVERRIDE = "tableOverride"; // table 重命名 property
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
@@ -45,8 +45,8 @@ public class TableRenamePlugin extends BasePlugin {
|
||||
public boolean validate(List<String> warnings) {
|
||||
|
||||
// 如果配置了searchString 或者 replaceString,二者不允许单独存在
|
||||
if ((getProperties().getProperty(PRE_SEARCH_STRING) == null && getProperties().getProperty(PRE_REPLACE_STRING) != null)
|
||||
|| (getProperties().getProperty(PRE_SEARCH_STRING) != null && getProperties().getProperty(PRE_REPLACE_STRING) == null)) {
|
||||
if ((getProperties().getProperty(PRO_SEARCH_STRING) == null && getProperties().getProperty(PRO_REPLACE_STRING) != null)
|
||||
|| (getProperties().getProperty(PRO_SEARCH_STRING) != null && getProperties().getProperty(PRO_REPLACE_STRING) == null)) {
|
||||
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件的searchString、replaceString属性需配合使用,不能单独存在!");
|
||||
return false;
|
||||
}
|
||||
@@ -71,16 +71,16 @@ public class TableRenamePlugin extends BasePlugin {
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
// 1. 获取表单独配置
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE) != null) {
|
||||
String override = introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE);
|
||||
if (introspectedTable.getTableConfigurationProperty(PRO_TABLE_OVERRIDE) != null) {
|
||||
String override = introspectedTable.getTableConfigurationProperty(PRO_TABLE_OVERRIDE);
|
||||
try {
|
||||
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), override);
|
||||
} catch (Exception e) {
|
||||
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用tableOverride替换时异常!", e);
|
||||
}
|
||||
} else if (getProperties().getProperty(PRE_SEARCH_STRING) != null) {
|
||||
String searchString = getProperties().getProperty(PRE_SEARCH_STRING);
|
||||
String replaceString = getProperties().getProperty(PRE_REPLACE_STRING);
|
||||
} else if (getProperties().getProperty(PRO_SEARCH_STRING) != null) {
|
||||
String searchString = getProperties().getProperty(PRO_SEARCH_STRING);
|
||||
String replaceString = getProperties().getProperty(PRO_REPLACE_STRING);
|
||||
|
||||
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
|
||||
Pattern pattern = Pattern.compile(searchString);
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -49,7 +46,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
|
||||
|
||||
|
||||
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
|
||||
public static final String PRO_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
|
||||
private boolean allowMultiQueries = false; // 是否允许多sql提交
|
||||
|
||||
/**
|
||||
@@ -66,7 +63,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
|
||||
// 插件是否开启了多sql提交
|
||||
Properties properties = this.getProperties();
|
||||
String allowMultiQueries = properties.getProperty(PRE_ALLOW_MULTI_QUERIES);
|
||||
String allowMultiQueries = properties.getProperty(PRO_ALLOW_MULTI_QUERIES);
|
||||
this.allowMultiQueries = allowMultiQueries == null ? false : StringUtility.isTrue(allowMultiQueries);
|
||||
if (this.allowMultiQueries) {
|
||||
// 提示用户注意信息
|
||||
@@ -438,8 +435,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
// 获取if节点
|
||||
for (Element element : trimXmlElement.getElements()) {
|
||||
String text = ((TextElement) (((XmlElement) element).getElements().get(0))).getContent();
|
||||
String columnName = text.split("=")[0].trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
String columnName = text.split("=")[0];
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
// if 节点数据替换
|
||||
((XmlElement) element).getElements().clear();
|
||||
@@ -465,8 +462,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
if (incTools.support()) {
|
||||
// 获取column
|
||||
String text = element.getContent().trim();
|
||||
String columnName = text.split("=")[0].trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
String columnName = text.split("=")[0];
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
xmlElement.getElements().addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix, text.endsWith(",")));
|
||||
continue;
|
||||
|
||||
@@ -56,7 +56,7 @@ public class BasePlugin extends PluginAdapter {
|
||||
// 配置插件使用的模板引擎
|
||||
PluginConfiguration cfg = PluginTools.getPluginConfiguration(context, CommentPlugin.class);
|
||||
|
||||
if (cfg == null || cfg.getProperty(CommentPlugin.PRE_TEMPLATE) == null){
|
||||
if (cfg == null || cfg.getProperty(CommentPlugin.PRO_TEMPLATE) == null){
|
||||
if (context.getCommentGenerator() instanceof DefaultCommentGenerator){
|
||||
// 使用默认模板引擎
|
||||
commentGenerator = new TemplateCommentGenerator("default-comment.ftl", true);
|
||||
@@ -65,7 +65,7 @@ public class BasePlugin extends PluginAdapter {
|
||||
commentGenerator = context.getCommentGenerator();
|
||||
}
|
||||
} else {
|
||||
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE), false);
|
||||
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRO_TEMPLATE), false);
|
||||
|
||||
// ITFSW 插件使用的注释生成器
|
||||
commentGenerator = templateCommentGenerator;
|
||||
|
||||
@@ -69,12 +69,12 @@ public class IncrementsPluginTools {
|
||||
IncrementsPluginTools tools = new IncrementsPluginTools(context, introspectedTable);
|
||||
// 判断是否启用了插件
|
||||
if (PluginTools.getPluginConfiguration(context, IncrementsPlugin.class) != null) {
|
||||
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRE_INCREMENTS_COLUMNS);
|
||||
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRO_INCREMENTS_COLUMNS);
|
||||
if (StringUtility.stringHasValue(incrementsColumns)) {
|
||||
// 切分
|
||||
String[] incrementsColumnsStrs = incrementsColumns.split(",");
|
||||
for (String incrementsColumnsStr : incrementsColumnsStrs) {
|
||||
IntrospectedColumn column = introspectedTable.getColumn(incrementsColumnsStr.trim());
|
||||
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, incrementsColumnsStr);
|
||||
if (column == null) {
|
||||
warnings.add("itfsw:插件" + IncrementsPlugin.class.getTypeName() + "插件没有找到column为" + incrementsColumnsStr.trim() + "的字段!");
|
||||
} else {
|
||||
@@ -146,12 +146,12 @@ public class IncrementsPluginTools {
|
||||
when.addAttribute(new Attribute(
|
||||
"test",
|
||||
(hasPrefix ? "record." : "_parameter.") + IncrementsPlugin.METHOD_INC_CHECK
|
||||
+ "('" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + "')"
|
||||
+ "('" + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + "')"
|
||||
));
|
||||
TextElement spec = new TextElement(
|
||||
MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
|
||||
+ " ${" + (hasPrefix ? "record." : "")
|
||||
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ".value} "
|
||||
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
|
||||
+ MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, hasPrefix ? "record." : null));
|
||||
when.addElement(spec);
|
||||
choose.addElement(when);
|
||||
|
||||
+28
-2
@@ -18,10 +18,12 @@ package com.itfsw.mybatis.generator.plugins.utils;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin;
|
||||
import org.mybatis.generator.api.FullyQualifiedTable;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -31,6 +33,7 @@ import java.lang.reflect.Method;
|
||||
* ---------------------------------------------------------------------------
|
||||
* IntrospectedTable 的一些拓展增强
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* @author: hewei
|
||||
* @time:2017/6/13 13:48
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -68,14 +71,37 @@ public class IntrospectedTableTools {
|
||||
|
||||
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
|
||||
PluginConfiguration configuration = PluginTools.getPluginConfiguration(context, ExampleTargetPlugin.class);
|
||||
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null){
|
||||
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.PRO_TARGET_PACKAGE) != null) {
|
||||
String exampleType = introspectedTable.getExampleType();
|
||||
// 修改包名
|
||||
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
|
||||
String targetPackage = javaModelGeneratorConfiguration.getTargetPackage();
|
||||
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY));
|
||||
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.PRO_TARGET_PACKAGE));
|
||||
|
||||
introspectedTable.setExampleType(newExampleType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全获取column 通过正则获取的name可能包含beginningDelimiter&&endingDelimiter
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @param columnName
|
||||
* @return
|
||||
*/
|
||||
public static IntrospectedColumn safeGetColumn(IntrospectedTable introspectedTable, String columnName) {
|
||||
// columnName
|
||||
columnName = columnName.trim();
|
||||
// 过滤
|
||||
String beginningDelimiter = introspectedTable.getContext().getBeginningDelimiter();
|
||||
if (StringUtility.stringHasValue(beginningDelimiter)) {
|
||||
columnName = columnName.replaceFirst("^" + beginningDelimiter, "");
|
||||
}
|
||||
String endingDelimiter = introspectedTable.getContext().getEndingDelimiter();
|
||||
if (StringUtility.stringHasValue(endingDelimiter)) {
|
||||
columnName = columnName.replaceFirst(endingDelimiter + "$", "");
|
||||
}
|
||||
|
||||
return introspectedTable.getColumn(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
+45
-29
@@ -33,18 +33,17 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成静态常量
|
||||
*
|
||||
* @param fieldName 常量名称
|
||||
* @param javaType 类型
|
||||
* @param fieldName 常量名称
|
||||
* @param javaType 类型
|
||||
* @param initString 初始化字段
|
||||
* @return
|
||||
*/
|
||||
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString){
|
||||
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString) {
|
||||
Field field = new Field(fieldName, javaType);
|
||||
field.setVisibility(JavaVisibility.PUBLIC);
|
||||
field.setStatic(true);
|
||||
field.setFinal(true);
|
||||
if (initString != null){
|
||||
if (initString != null) {
|
||||
field.setInitializationString(initString);
|
||||
}
|
||||
return field;
|
||||
@@ -52,17 +51,16 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成属性
|
||||
*
|
||||
* @param fieldName 常量名称
|
||||
* @param visibility 可见性
|
||||
* @param javaType 类型
|
||||
* @param fieldName 常量名称
|
||||
* @param visibility 可见性
|
||||
* @param javaType 类型
|
||||
* @param initString 初始化字段
|
||||
* @return
|
||||
*/
|
||||
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString){
|
||||
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString) {
|
||||
Field field = new Field(fieldName, javaType);
|
||||
field.setVisibility(visibility);
|
||||
if (initString != null){
|
||||
if (initString != null) {
|
||||
field.setInitializationString(initString);
|
||||
}
|
||||
return field;
|
||||
@@ -70,19 +68,18 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成方法
|
||||
*
|
||||
* @param methodName 方法名
|
||||
* @param visibility 可见性
|
||||
* @param visibility 可见性
|
||||
* @param returnType 返回值类型
|
||||
* @param parameters 参数列表
|
||||
* @return
|
||||
*/
|
||||
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter ... parameters){
|
||||
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter... parameters) {
|
||||
Method method = new Method(methodName);
|
||||
method.setVisibility(visibility);
|
||||
method.setReturnType(returnType);
|
||||
if (parameters != null){
|
||||
for (Parameter parameter: parameters) {
|
||||
if (parameters != null) {
|
||||
for (Parameter parameter : parameters) {
|
||||
method.addParameter(parameter);
|
||||
}
|
||||
}
|
||||
@@ -92,14 +89,13 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成方法实现体
|
||||
*
|
||||
* @param method 方法
|
||||
* @param method 方法
|
||||
* @param bodyLines 方法实现行
|
||||
* @return
|
||||
*/
|
||||
public static Method generateMethodBody(Method method, String ... bodyLines){
|
||||
if (bodyLines != null){
|
||||
for (String bodyLine: bodyLines){
|
||||
public static Method generateMethodBody(Method method, String... bodyLines) {
|
||||
if (bodyLines != null) {
|
||||
for (String bodyLine : bodyLines) {
|
||||
method.addBodyLine(bodyLine);
|
||||
}
|
||||
}
|
||||
@@ -108,11 +104,10 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成Filed的Set方法
|
||||
*
|
||||
* @param field field
|
||||
* @return
|
||||
*/
|
||||
public static Method generateSetterMethod(Field field){
|
||||
public static Method generateSetterMethod(Field field) {
|
||||
Method method = generateMethod(
|
||||
"set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
|
||||
JavaVisibility.PUBLIC,
|
||||
@@ -124,11 +119,10 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 生成Filed的Get方法
|
||||
*
|
||||
* @param field field
|
||||
* @return
|
||||
*/
|
||||
public static Method generateGetterMethod(Field field){
|
||||
public static Method generateGetterMethod(Field field) {
|
||||
Method method = generateMethod(
|
||||
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
|
||||
JavaVisibility.PUBLIC,
|
||||
@@ -139,11 +133,10 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 获取Model没有BLOBs类时的类型
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static FullyQualifiedJavaType getModelTypeWithoutBLOBs(IntrospectedTable introspectedTable){
|
||||
public static FullyQualifiedJavaType getModelTypeWithoutBLOBs(IntrospectedTable introspectedTable) {
|
||||
FullyQualifiedJavaType type;
|
||||
if (introspectedTable.getRules().generateBaseRecordClass()) {
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
|
||||
@@ -157,11 +150,10 @@ public class JavaElementGeneratorTools {
|
||||
|
||||
/**
|
||||
* 获取Model有BLOBs类时的类型
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static FullyQualifiedJavaType getModelTypeWithBLOBs(IntrospectedTable introspectedTable){
|
||||
public static FullyQualifiedJavaType getModelTypeWithBLOBs(IntrospectedTable introspectedTable) {
|
||||
FullyQualifiedJavaType type;
|
||||
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
|
||||
@@ -171,4 +163,28 @@ public class JavaElementGeneratorTools {
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆方法
|
||||
* @param method
|
||||
*/
|
||||
public static Method cloneMethod(Method method) {
|
||||
Method result = new Method();
|
||||
result.setConstructor(method.isConstructor());
|
||||
result.setFinal(method.isFinal());
|
||||
result.setName(method.getName());
|
||||
result.setNative(method.isNative());
|
||||
result.setReturnType(method.getReturnType());
|
||||
result.setSynchronized(method.isSynchronized());
|
||||
result.setStatic(method.isStatic());
|
||||
result.setVisibility(method.getVisibility());
|
||||
for (Parameter parameter : method.getParameters()) {
|
||||
result.addParameter(parameter);
|
||||
}
|
||||
for (String docLine : method.getJavaDocLines()){
|
||||
result.addJavaDocLine(docLine);
|
||||
}
|
||||
result.addBodyLines(method.getBodyLines());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ public class XmlElementGeneratorTools {
|
||||
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix) {
|
||||
GeneratedKey gk = introspectedTable.getGeneratedKey();
|
||||
if (gk != null) {
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, gk.getColumn());
|
||||
// if the column is null, then it's a configuration error. The
|
||||
// warning has already been reported
|
||||
if (introspectedColumn != null) {
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2017.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins.utils.enhanced;
|
||||
|
||||
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/8/2 16:55
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class InnerTypeFullyQualifiedJavaType extends FullyQualifiedJavaType {
|
||||
private final static Logger logger = LoggerFactory.getLogger(InnerTypeFullyQualifiedJavaType.class);
|
||||
private String outerType; // 内部类
|
||||
/**
|
||||
* Use this constructor to construct a generic type with the specified type parameters.
|
||||
* @param fullTypeSpecification the full type specification
|
||||
*/
|
||||
public InnerTypeFullyQualifiedJavaType(String fullTypeSpecification){
|
||||
super(fullTypeSpecification);
|
||||
|
||||
try{
|
||||
// 修正package
|
||||
java.lang.reflect.Field packageName = this.getClass().getSuperclass().getDeclaredField("packageName");
|
||||
packageName.setAccessible(true);
|
||||
String oldPackageName = getPackageName();
|
||||
packageName.set(this, oldPackageName.substring(0, oldPackageName.lastIndexOf(".")));
|
||||
|
||||
outerType = oldPackageName.substring(oldPackageName.lastIndexOf(".") + 1);
|
||||
} catch (Exception e){
|
||||
logger.error("InnerTypeFullyQualifiedJavaType 赋值失败!", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the fully qualified name - including any generic type parameters.
|
||||
*
|
||||
* @return Returns the fullyQualifiedName.
|
||||
*/
|
||||
@Override
|
||||
public String getFullyQualifiedName() {
|
||||
String fullyQualifiedName = super.getFullyQualifiedName();
|
||||
String before = fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("."));
|
||||
String end = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf("."));
|
||||
return before + "." + outerType + end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the short name.
|
||||
*
|
||||
* @return Returns the shortName - including any type arguments.
|
||||
*/
|
||||
@Override
|
||||
public String getShortName() {
|
||||
return outerType + "." + super.getShortName();
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,14 @@ import org.apache.ibatis.session.SqlSession;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -66,7 +71,7 @@ public class IncrementsPluginTest {
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试生成的方法
|
||||
ObjectUtil tbBuilder = new ObjectUtil(loader, packagz + ".Tb$Builder");
|
||||
List<Method> methods = tbBuilder.getMethods("incF1");
|
||||
List<Method> methods = getDeclaredMethods(tbBuilder.getCls(), "incF1");
|
||||
Assert.assertEquals(methods.size(), 2);
|
||||
// 自增方法
|
||||
Method incMethod = methods.get(0).getParameterTypes().length == 1 ? methods.get(1) : methods.get(0);
|
||||
@@ -74,19 +79,36 @@ public class IncrementsPluginTest {
|
||||
|
||||
// 2. 测试有空格
|
||||
ObjectUtil tbKeysBuilder = new ObjectUtil(loader, packagz + ".TbKeys$Builder");
|
||||
Assert.assertEquals(tbKeysBuilder.getMethods("incF1").size(), 2);
|
||||
Assert.assertEquals(tbKeysBuilder.getMethods("incF2").size(), 2);
|
||||
Assert.assertEquals(tbKeysBuilder.getMethods("incF3").size(), 2);
|
||||
Assert.assertEquals(getDeclaredMethods(tbKeysBuilder.getCls(), "incF1").size(), 2);
|
||||
Assert.assertEquals(getDeclaredMethods(tbKeysBuilder.getCls(), "incF2").size(), 2);
|
||||
Assert.assertEquals(getDeclaredMethods(tbKeysBuilder.getCls(), "incF3").size(), 2);
|
||||
|
||||
// 3. 测试在WithBlobs正确生成
|
||||
ObjectUtil tbBlobsWithBLOBs = new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs$Builder");
|
||||
Assert.assertEquals(tbBlobsWithBLOBs.getMethods("incF1").size(), 2);
|
||||
Assert.assertEquals(tbBlobsWithBLOBs.getMethods("incF2").size(), 1);
|
||||
Assert.assertEquals(tbBlobsWithBLOBs.getMethods("incF3").size(), 2);
|
||||
Assert.assertEquals(getDeclaredMethods(tbBlobsWithBLOBs.getCls(), "incF1").size(), 2);
|
||||
Assert.assertEquals(getDeclaredMethods(tbBlobsWithBLOBs.getCls(), "incF2").size(), 1);
|
||||
Assert.assertEquals(getDeclaredMethods(tbBlobsWithBLOBs.getCls(), "incF3").size(), 2);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类方法
|
||||
* @param cls
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
private List<Method> getDeclaredMethods(Class cls, String name) {
|
||||
List<Method> list = new ArrayList<>();
|
||||
Method[] methods = cls.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(name) && cls.equals(method.getReturnType())) {
|
||||
list.add(method);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试生成的sql和具体执行
|
||||
*/
|
||||
@@ -188,4 +210,36 @@ public class IncrementsPluginTest {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 autoDelimitKeywords
|
||||
*/
|
||||
@Test
|
||||
public void testWithAutoDelimitKeywords() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExample、updateByExampleSelective
|
||||
ObjectUtil TbKeyWord = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeyWordMapper")));
|
||||
|
||||
ObjectUtil TbKeyWordExample = new ObjectUtil(loader, packagz + ".TbKeyWordExample");
|
||||
ObjectUtil criteria = new ObjectUtil(TbKeyWordExample.invoke("createCriteria"));
|
||||
criteria.invoke("andIdEqualTo", 1l);
|
||||
|
||||
ObjectUtil TbKeyWordBuilder = new ObjectUtil(loader, packagz + ".TbKeyWord$Builder");
|
||||
ObjectUtil TbKeyWordBuilderInc = new ObjectUtil(loader, packagz + ".TbKeyWord$Builder$Inc#INC");
|
||||
TbKeyWordBuilder.invoke("update", 100l, TbKeyWordBuilderInc.getObject());
|
||||
|
||||
// 执行
|
||||
// inc_f1 增加100
|
||||
Object result = TbKeyWord.invoke("updateByExampleSelective", TbKeyWordBuilder.invoke("build"), TbKeyWordExample.getObject());
|
||||
Assert.assertEquals(result, 1);
|
||||
// 验证更新
|
||||
ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select * from tb_key_word where id = 1");
|
||||
rs.first();
|
||||
Assert.assertEquals(rs.getLong("update"), 101);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,12 @@ public class LogicalDeletePluginTest {
|
||||
tool.generate();
|
||||
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw(逻辑删除插件):tb没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
|
||||
|
||||
// 4. 保留关键词冲突
|
||||
tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-keywords.xml");
|
||||
tool.generate();
|
||||
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw(逻辑删除插件):tb配置的逻辑删除列和插件保留关键字(andLogicalDeleted)冲突!");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,11 +147,83 @@ public class LogicalDeletePluginTest {
|
||||
|
||||
// 验证sql
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
|
||||
Assert.assertEquals(sql, "select id, del_flag, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '1' and id = '3' )");
|
||||
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '1' and id = '3' )");
|
||||
// 验证执行
|
||||
Object result = tbMapper.invoke("selectByExample", tbExample.getObject());
|
||||
Assert.assertEquals(((List)result).size(), 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试自定义常量
|
||||
*/
|
||||
@Test
|
||||
public void testCustomConst() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
ObjectUtil Tb = new ObjectUtil(loader, packagz + ".Tb");
|
||||
|
||||
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
|
||||
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
|
||||
criteria.invoke("andDelFlagEqualTo", Tb.get("UN_DEL"));
|
||||
criteria.invoke("andIdEqualTo", 3l);
|
||||
|
||||
// 验证sql
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
|
||||
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '0' and id = '3' )");
|
||||
// 验证执行
|
||||
Object result = tbMapper.invoke("selectByExample", tbExample.getObject());
|
||||
Assert.assertEquals(((List)result).size(), 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试Model andLogicalDeleted 方法
|
||||
*/
|
||||
@Test
|
||||
public void testModelAndLogicalDeletedMethod() throws InterruptedException, SQLException, InvalidConfigurationException, IOException, XMLParserException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
|
||||
ObjectUtil Tb = new ObjectUtil(loader, packagz + ".Tb");
|
||||
|
||||
Tb.invoke("andLogicalDeleted", true);
|
||||
Assert.assertEquals(Tb.get("delFlag"), (short)1);
|
||||
|
||||
Tb.invoke("andLogicalDeleted", false);
|
||||
Assert.assertEquals(Tb.get("delFlag"), (short)0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 selectByPrimaryKeyWithLogicalDelete
|
||||
*/
|
||||
@Test
|
||||
public void testSelectByPrimaryKeyWithLogicalDelete() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
|
||||
// 验证sql
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByPrimaryKeyWithLogicalDelete", 5l, true);
|
||||
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 , ts_2 from tb where id = 5 and del_flag = '1'");
|
||||
// 验证执行
|
||||
Object result = tbMapper.invoke("selectByPrimaryKeyWithLogicalDelete", 5l, true);
|
||||
Assert.assertNull(result);
|
||||
|
||||
// 验证执行
|
||||
result = tbMapper.invoke("selectByPrimaryKeyWithLogicalDelete", 5l, false);
|
||||
Assert.assertNotNull(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,91 @@ public class ModelBuilderPluginTest {
|
||||
ObjectUtil tbKeys = new ObjectUtil(tbKeysBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbKeys.invoke("getKey1"), 50l);
|
||||
Assert.assertEquals(tbKeys.invoke("getField1"), "ts2");
|
||||
|
||||
// 4. key and blobs
|
||||
ObjectUtil TbKeysBlobsKeyBuilder = new ObjectUtil(loader, packagz + ".TbKeysBlobsKey$Builder");
|
||||
TbKeysBlobsKeyBuilder.invoke("key1", 60l);
|
||||
ObjectUtil TbKeysBlobsKey = new ObjectUtil(TbKeysBlobsKeyBuilder.invoke("build"));
|
||||
Assert.assertEquals(TbKeysBlobsKey.invoke("getKey1"), 60l);
|
||||
|
||||
ObjectUtil TbKeysBlobsWithBLOBsBuilder = new ObjectUtil(loader, packagz + ".TbKeysBlobsWithBLOBs$Builder");
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("key1", 90l);
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("incF2", 70l);
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("field2", "ts3");
|
||||
|
||||
ObjectUtil TbKeysBlobsWithBLOBs = new ObjectUtil(TbKeysBlobsWithBLOBsBuilder.invoke("build"));
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getKey1"), 90l);
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getIncF2"), 70l);
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getField2"), "ts3");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试静态builder方法
|
||||
*/
|
||||
@Test
|
||||
public void testBuilderMethod() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelBuilderPlugin/mybatis-generator.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 普通model
|
||||
ObjectUtil Tb = new ObjectUtil(loader, packagz + ".Tb");
|
||||
ObjectUtil tbBuilder = new ObjectUtil(Tb.invoke("builder"));
|
||||
tbBuilder.invoke("field1", "ts1");
|
||||
tbBuilder.invoke("incF1", 100l);
|
||||
ObjectUtil tb = new ObjectUtil(tbBuilder.invoke("build"));
|
||||
Assert.assertEquals(tb.invoke("getField1"), "ts1");
|
||||
Assert.assertEquals(tb.invoke("getIncF1"), 100l);
|
||||
|
||||
// 2. withBlobs
|
||||
ObjectUtil TbBlobs = new ObjectUtil(loader, packagz + ".TbBlobs");
|
||||
ObjectUtil tbBlobsBuilder = new ObjectUtil(TbBlobs.invoke("builder"));
|
||||
tbBlobsBuilder.invoke("field1", "ts1");
|
||||
ObjectUtil tbBlobs = new ObjectUtil(tbBlobsBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbBlobs.invoke("getField1"), "ts1");
|
||||
|
||||
ObjectUtil TbBlobsWithBLOBs= new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs");
|
||||
ObjectUtil tbBlobsWithBLOBsBuilder = new ObjectUtil(TbBlobsWithBLOBs.invoke("builder"));
|
||||
tbBlobsWithBLOBsBuilder.invoke("field1", "ts1");
|
||||
tbBlobsWithBLOBsBuilder.invoke("field2", "ts2");
|
||||
ObjectUtil tbBlobsWithBLOBs = new ObjectUtil(tbBlobsWithBLOBsBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbBlobsWithBLOBs.invoke("getField1"), "ts1");
|
||||
Assert.assertEquals(tbBlobsWithBLOBs.invoke("getField2"), "ts2");
|
||||
|
||||
// 3. key
|
||||
ObjectUtil TbKeysKey = new ObjectUtil(loader, packagz + ".TbKeysKey");
|
||||
ObjectUtil tbKeysKeyBuilder = new ObjectUtil(TbKeysKey.invoke("builder"));
|
||||
tbKeysKeyBuilder.invoke("key1", 60l);
|
||||
ObjectUtil tbKeysKey = new ObjectUtil(tbKeysKeyBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbKeysKey.invoke("getKey1"), 60l);
|
||||
|
||||
ObjectUtil TbKeys = new ObjectUtil(loader, packagz + ".TbKeys");
|
||||
ObjectUtil tbKeysBuilder = new ObjectUtil(TbKeys.invoke("builder"));
|
||||
tbKeysBuilder.invoke("key1", 50l);
|
||||
tbKeysBuilder.invoke("field1", "ts2");
|
||||
ObjectUtil tbKeys = new ObjectUtil(tbKeysBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbKeys.invoke("getKey1"), 50l);
|
||||
Assert.assertEquals(tbKeys.invoke("getField1"), "ts2");
|
||||
|
||||
// 4. key and blobs
|
||||
ObjectUtil TbKeysBlobsKey = new ObjectUtil(loader, packagz + ".TbKeysBlobsKey");
|
||||
ObjectUtil TbKeysBlobsKeyBuilder = new ObjectUtil(TbKeysBlobsKey.invoke("builder"));
|
||||
TbKeysBlobsKeyBuilder.invoke("key1", 60l);
|
||||
ObjectUtil tbKeysBlobsKey = new ObjectUtil(TbKeysBlobsKeyBuilder.invoke("build"));
|
||||
Assert.assertEquals(tbKeysBlobsKey.invoke("getKey1"), 60l);
|
||||
|
||||
ObjectUtil TbKeysBlobsWithBLOBs = new ObjectUtil(loader, packagz + ".TbKeysBlobsWithBLOBs");
|
||||
ObjectUtil TbKeysBlobsWithBLOBsBuilder = new ObjectUtil(TbKeysBlobsWithBLOBs.invoke("builder"));
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("key1", 90l);
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("incF2", 70l);
|
||||
TbKeysBlobsWithBLOBsBuilder.invoke("field2", "ts3");
|
||||
|
||||
TbKeysBlobsWithBLOBs = new ObjectUtil(TbKeysBlobsWithBLOBsBuilder.invoke("build"));
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getKey1"), 90l);
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getIncF2"), 70l);
|
||||
Assert.assertEquals(TbKeysBlobsWithBLOBs.invoke("getField2"), "ts3");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -79,10 +79,10 @@ public class SelectiveEnhancedPluginTest {
|
||||
Array.set(columns, 1, TbColumnTsIncF2.getObject());
|
||||
|
||||
tb.invoke("selective", columns);
|
||||
Assert.assertTrue((Boolean) tb.invoke("isSelective"));
|
||||
Assert.assertTrue((Boolean) tb.invoke("isSelective", "field_1"));
|
||||
Assert.assertFalse((Boolean) tb.invoke("isSelective", "inc_f1"));
|
||||
Assert.assertTrue((Boolean) tb.invoke("isSelective", "inc_f2"));
|
||||
Assert.assertTrue((Boolean) tb.invoke("hasSelective"));
|
||||
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "field_1"));
|
||||
Assert.assertFalse((Boolean) tb.invoke("hasSelective", "inc_f1"));
|
||||
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "inc_f2"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -137,6 +137,7 @@ public class MyBatisGeneratorTool {
|
||||
public SqlSession getSqlSession() throws IOException, ClassNotFoundException {
|
||||
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
|
||||
config.setCallSettersOnNulls(true); // 设计null调用setter方法
|
||||
config.setMapUnderscoreToCamelCase(true); // 驼峰命名支持
|
||||
|
||||
// 设置mapper
|
||||
config.addMappers(targetPackage);
|
||||
|
||||
@@ -101,3 +101,20 @@ CREATE TABLE `tb_single_blob` (
|
||||
INSERT INTO `tb_single_blob` VALUES ('1', 'fd1', '0', '1', '2', '3');
|
||||
INSERT INTO `tb_single_blob` VALUES ('2', null, null, '3', '2', '1');
|
||||
INSERT INTO `tb_single_blob` VALUES ('3', null, null, '1', '1', '1');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_key_word
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_key_word`;
|
||||
CREATE TABLE `tb_key_word` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`field2` int(11) DEFAULT NULL,
|
||||
`inc_f1` bigint(20) NOT NULL DEFAULT '0',
|
||||
`update` bigint(20) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb_key_word
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb_key_word` VALUES ('1', '0', '0', '1');
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2017.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
<properties resource="db.properties"/>
|
||||
|
||||
<!--导入属性配置 -->
|
||||
<context id="default" targetRuntime="MyBatis3">
|
||||
<property name="autoDelimitKeywords" value="true"/>
|
||||
<property name="beginningDelimiter" value="`"/>
|
||||
<property name="endingDelimiter" value="`"/>
|
||||
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.IncrementsPlugin" />
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin" />
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject="">
|
||||
<!-- 是否对model添加 构造函数 -->
|
||||
<property name="constructorBased" value="true"/>
|
||||
<!-- 给Model添加一个父类 -->
|
||||
<!--<property name="rootClass" value="com.itfsw.base"/>-->
|
||||
</javaModelGenerator>
|
||||
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
|
||||
<sqlMapGenerator targetPackage="" targetProject="" />
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
|
||||
|
||||
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<table tableName="tb_key_word">
|
||||
<property name="incrementsColumns" value="update"/>
|
||||
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
@@ -22,6 +22,7 @@ DROP TABLE IF EXISTS `tb`;
|
||||
CREATE TABLE `tb` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`del_flag` smallint(1) NOT NULL DEFAULT '0',
|
||||
`and_logical_deleted` smallint(1) NOT NULL DEFAULT '0',
|
||||
`ts_1` tinyint(1) DEFAULT NULL,
|
||||
`ts_2` binary(255) DEFAULT NULL,
|
||||
`ts_3` float DEFAULT NULL,
|
||||
@@ -32,6 +33,7 @@ CREATE TABLE `tb` (
|
||||
-- ----------------------------
|
||||
-- Records of tb
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb` VALUES ('1', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('2', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('3', '1', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('1', '0', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('2', '0', '1', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('3', '1', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
INSERT INTO `tb` VALUES ('5', '0', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2017.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
<properties resource="db.properties"/>
|
||||
<!--导入属性配置 -->
|
||||
<context id="default" targetRuntime="MyBatis3">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
|
||||
<property name="logicalDeleteColumn" value="del_flag"/>
|
||||
<property name="logicalDeleteValue" value="1"/>
|
||||
<property name="logicalUnDeleteValue" value="0"/>
|
||||
<property name="logicalDeleteConstName" value="DEL"/>
|
||||
<property name="logicalUnDeleteConstName" value="UN_DEL"/>
|
||||
</plugin>
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject=""/>
|
||||
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
|
||||
<sqlMapGenerator targetPackage="" targetProject="" />
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
|
||||
|
||||
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<table tableName="tb">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2017.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
<properties resource="db.properties"/>
|
||||
<!--导入属性配置 -->
|
||||
<context id="default" targetRuntime="MyBatis3">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
|
||||
<property name="logicalDeleteColumn" value="and_logical_deleted"/>
|
||||
<property name="logicalDeleteValue" value="1"/>
|
||||
<property name="logicalUnDeleteValue" value="0"/>
|
||||
</plugin>
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject=""/>
|
||||
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
|
||||
<sqlMapGenerator targetPackage="" targetProject="" />
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
|
||||
|
||||
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<table tableName="tb">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
@@ -70,3 +70,22 @@ CREATE TABLE `tb_keys` (
|
||||
-- Records of tb_keys
|
||||
-- ----------------------------
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_keys_blobs
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_keys_blobs`;
|
||||
CREATE TABLE `tb_keys_blobs` (
|
||||
`key1` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`key2` varchar(255) NOT NULL,
|
||||
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
`field2` longtext COMMENT '注释2',
|
||||
`field3` longtext,
|
||||
`inc_f2` bigint(20) NOT NULL DEFAULT '0',
|
||||
`inc_f3` bigint(20) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`key1`,`key2`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb_keys
|
||||
-- ----------------------------
|
||||
|
||||
|
||||
@@ -49,5 +49,6 @@
|
||||
<table tableName="tb_blobs">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
<table tableName="tb_keys_blobs"/>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user