Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c062929a60 | ||
|
|
a43ee15469 | ||
|
|
9b83bc1520 | ||
|
|
91fcd50591 | ||
|
|
b479fd34d5 | ||
|
|
ba044ccdfc | ||
|
|
07c78ddd29 | ||
|
|
bfbae5f30d | ||
|
|
1b151673ac | ||
|
|
fa2867dde1 | ||
|
|
6ac9cd0c11 | ||
|
|
12bd2b18ac | ||
|
|
e359e3a677 |
@@ -13,6 +13,7 @@
|
||||
* 逻辑删除插件(LogicalDeletePlugin)
|
||||
* 数据Model属性对应Column获取插件(ModelColumnPlugin)
|
||||
* 存在即更新插件(UpsertPlugin)
|
||||
* Selective选择插入更新增强插件(SelectiveEnhancedPlugin(1.0.7-SNAPSHOT))
|
||||
|
||||
---------------------------------------
|
||||
Maven引用:
|
||||
@@ -20,7 +21,10 @@ Maven引用:
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<!-- 稳定版本 -->
|
||||
<version>1.0.6</version>
|
||||
<!-- 快照版本(最新提交到中央库,增加了部分功能,欢迎测试反馈) -->
|
||||
<version>1.0.7-SNAPSHOT</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
@@ -293,6 +297,10 @@ public class Test {
|
||||
```
|
||||
### 7. 逻辑删除插件
|
||||
因为很多实际项目数据都不允许物理删除,多采用逻辑删除,所以单独为逻辑删除做了一个插件,方便使用。
|
||||
- 增加logicalDeleteByExample和logicalDeleteByPrimaryKey方法;
|
||||
- 查询构造工具中增加逻辑删除条件andDeleted(boolean);
|
||||
- 增加逻辑删除常量DEL_FLAG_OFF(已删除)、DEL_FLAG_ON(未删除)(1.0.7-SNAPSHOT);
|
||||
|
||||
插件:
|
||||
```xml
|
||||
<xml>
|
||||
@@ -301,14 +309,17 @@ public class Test {
|
||||
<!-- 这里配置的是全局逻辑删除列和逻辑删除值,当然在table中配置的值会覆盖该全局配置 -->
|
||||
<!-- 逻辑删除列类型只能为数字、字符串或者布尔类型 -->
|
||||
<property name="logicalDeleteColumn" value="del_flag"/>
|
||||
<!-- 未设置该属性或者该属性的值为null或者NULL,逻辑删除时会把该字段置为NULL。 -->
|
||||
<!-- 逻辑删除-已删除值 -->
|
||||
<property name="logicalDeleteValue" value="9"/>
|
||||
<!-- 逻辑删除-未删除值 -->
|
||||
<property name="logicalUnDeleteValue" value="0"/>
|
||||
</plugin>
|
||||
|
||||
<table tableName="tb">
|
||||
<!-- 这里可以单独表配置逻辑删除列和删除值,覆盖全局配置 -->
|
||||
<property name="logicalDeleteColumn" value="del_flag"/>
|
||||
<property name="logicalDeleteValue" value="1"/>
|
||||
<property name="logicalUnDeleteValue" value="-1"/>
|
||||
</table>
|
||||
</xml>
|
||||
```
|
||||
@@ -338,6 +349,12 @@ public class Test {
|
||||
.andDelFlagEqualTo(Tb.DEL_FLAG)
|
||||
.example()
|
||||
);
|
||||
|
||||
// 4. 逻辑删除和未删除常量
|
||||
Tb tb = new Tb.Builder()
|
||||
.delFlag(Tb.DEL_FLAG_OFF) // 删除
|
||||
.delFlag(Tb.DEL_FLAG_ON) // 未删除
|
||||
.build();
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -403,12 +420,20 @@ public class Test {
|
||||
}
|
||||
```
|
||||
### 9. 存在即更新插件
|
||||
使用MySQL的[“insert ... on duplicate key update”](https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html)实现存在即更新操作,简化数据入库操作([[issues#2]](https://github.com/itfsw/mybatis-generator-plugin/issues/2))。
|
||||
1. 使用MySQL的[“insert ... on duplicate key update”](https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html)实现存在即更新操作,简化数据入库操作([[issues#2]](https://github.com/itfsw/mybatis-generator-plugin/issues/2))。
|
||||
2. 在开启allowMultiQueries=true(默认不会开启)情况下支持upsertByExample,upsertByExampleSelective操作,但强力建议不要使用(需保证团队没有使用statement提交sql,否则会存在sql注入风险)(1.0.7-SNAPSHOT)([[issues#2]](https://github.com/itfsw/mybatis-generator-plugin/issues/2))。
|
||||
|
||||
插件:
|
||||
```xml
|
||||
<!-- 存在即更新插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.UpsertPlugin"/>
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.UpsertPlugin">
|
||||
<!--
|
||||
支持upsertByExample,upsertByExampleSelective操作
|
||||
!需开启allowMultiQueries=true多条sql提交操作,所以不建议使用!
|
||||
! 目前在1.0.7-SNAPSHOT支持,有需要的可以下载试用
|
||||
-->
|
||||
<property name="allowMultiQueries" value="true"/>
|
||||
</plugin>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
@@ -418,7 +443,7 @@ public class Test {
|
||||
Tb tb = new Tb.Builder()
|
||||
.field1(1)
|
||||
.field2("xx0")
|
||||
.delFlag((short)0)
|
||||
.delFlag(Tb.DEL_FLAG_ON)
|
||||
.build();
|
||||
int k0 = this.tbMapper.upsert(tb);
|
||||
// 2. 已入库数据再次入库,执行update(!!需要注意如触发update其返回的受影响行数为2)
|
||||
@@ -433,6 +458,80 @@ public class Test {
|
||||
int k2 = this.tbMapper.upsertSelective(tb1);
|
||||
tb1.setField2("xx1");
|
||||
int k3 = this.tbMapper.upsertSelective(tb1);
|
||||
|
||||
// 4. 开启allowMultiQueries后增加upsertByExample,upsertByExampleSelective但强力建议不要使用(需保证团队没有使用statement提交sql,否则会存在sql注入风险)
|
||||
Tb tb2 = new Tb.Builder()
|
||||
.field1(1)
|
||||
.field2("xx0")
|
||||
.field3(1003)
|
||||
.delFlag(Tb.DEL_FLAG_ON)
|
||||
.build();
|
||||
int k4 = this.tbMapper.upsertByExample(tb2,
|
||||
new TbExample()
|
||||
.createCriteria()
|
||||
.andField3EqualTo(1003)
|
||||
.example()
|
||||
);
|
||||
tb2.setField2("xx1");
|
||||
// !!! upsertByExample,upsertByExampleSelective触发更新时,更新条数返回是有问题的,这里只会返回0
|
||||
// 这是mybatis自身问题,也是不怎么建议开启allowMultiQueries功能原因之一
|
||||
int k5 = this.tbMapper.upsertByExample(tb2,
|
||||
new TbExample()
|
||||
.createCriteria()
|
||||
.andField3EqualTo(1003)
|
||||
.example()
|
||||
);
|
||||
// upsertByExampleSelective 用法类似
|
||||
}
|
||||
}
|
||||
```
|
||||
### 10. Selective选择插入更新增强插件(1.0.7-SNAPSHOT)
|
||||
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
|
||||
WAREN:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
|
||||
|
||||
插件:
|
||||
```xml
|
||||
<!-- Selective选择插入更新增强插件!请配在所有插件末尾以便最后执行 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
// 1. 指定插入或更新字段
|
||||
Tb tb = new Tb.Builder()
|
||||
.field1(1)
|
||||
.field2("xx2")
|
||||
.field3(1)
|
||||
.field4(new Date())
|
||||
.createTime(new Date())
|
||||
.delFlag(Tb.DEL_FLAG_ON)
|
||||
.build();
|
||||
// 只插入或者更新field1,field2字段
|
||||
this.tbMapper.insertSelective(tb.selective(Tb.Column.field1, Tb.Column.field2));
|
||||
this.tbMapper.updateByExampleSelective(tb.selective(Tb.Column.field1, Tb.Column.field2),
|
||||
new TbExample()
|
||||
.createCriteria()
|
||||
.andIdEqualTo(1l)
|
||||
.example()
|
||||
);
|
||||
this.tbMapper.updateByPrimaryKeySelective(tb.selective(Tb.Column.field1, Tb.Column.field2));
|
||||
this.tbMapper.upsertSelective(tb.selective(Tb.Column.field1, Tb.Column.field2));
|
||||
this.tbMapper.upsertByExampleSelective(tb.selective(Tb.Column.field1, Tb.Column.field2),
|
||||
new TbExample()
|
||||
.createCriteria()
|
||||
.andField3EqualTo(1)
|
||||
.example()
|
||||
);
|
||||
|
||||
// 2. 更新某些字段为null
|
||||
this.tbMapper.updateByPrimaryKeySelective(
|
||||
new Tb.Builder()
|
||||
.id(1l)
|
||||
.field1(null) // 方便展示,不用设也可以
|
||||
.build()
|
||||
.selective(Tb.Column.field1)
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.6</version>
|
||||
<version>1.0.7-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
@@ -175,7 +175,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.6</tag>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
@@ -125,7 +125,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
|
||||
method.addParameter(new Parameter(type1, "insertColumns", "@Param(\"insertColumns\")", true));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
@@ -142,7 +142,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
|
||||
type.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
|
||||
method.addParameter(new Parameter(type, "list", "@Param(\"list\")"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
@@ -171,7 +171,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
|
||||
CommentTools.addComment(element);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(element, introspectedTable);
|
||||
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
|
||||
|
||||
// choose 节点
|
||||
XmlElement choose = new XmlElement("choose");
|
||||
@@ -246,7 +246,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
|
||||
CommentTools.addComment(element);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(element, introspectedTable);
|
||||
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
|
||||
|
||||
// 普通插入语句
|
||||
this.addNormalBatchInsertXml(element, introspectedTable);
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
@@ -28,13 +29,10 @@ import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -71,27 +69,9 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
|
||||
|
||||
// 插件使用前提是使用了ModelColumnPlugin插件
|
||||
try {
|
||||
Context ctx = getContext();
|
||||
// 利用反射获取pluginConfigurations属性
|
||||
java.lang.reflect.Field field = Context.class.getDeclaredField("pluginConfigurations");
|
||||
field.setAccessible(true);
|
||||
List<PluginConfiguration> list = (List<PluginConfiguration>) field.get(ctx);
|
||||
// 检查是否配置了ModelColumnPlugin插件
|
||||
boolean unFind = true;
|
||||
for (PluginConfiguration config: list) {
|
||||
if (ModelColumnPlugin.class.getName().equals(config.getConfigurationType())){
|
||||
unFind = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 建议使用ModelColumnPlugin插件
|
||||
if (unFind){
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage());
|
||||
if (!PluginTools.checkDependencyPlugin(ModelColumnPlugin.class, getContext())) {
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO
|
||||
@@ -121,7 +101,7 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
tList.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
|
||||
mBatchInsert.addParameter(new Parameter(tList, "list", "@Param(\"list\")"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mBatchInsert, introspectedTable);
|
||||
CommentTools.addMethodComment(mBatchInsert, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mBatchInsert);
|
||||
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
|
||||
@@ -134,7 +114,7 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
mBatchInsertSelective.addParameter(new Parameter(tList, "list", "@Param(\"list\")"));
|
||||
mBatchInsertSelective.addParameter(new Parameter(tSelective, "selective", "@Param(\"selective\")", true));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mBatchInsertSelective, introspectedTable);
|
||||
CommentTools.addMethodComment(mBatchInsertSelective, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mBatchInsertSelective);
|
||||
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。");
|
||||
@@ -161,38 +141,10 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
CommentTools.addComment(batchInsertEle);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(batchInsertEle, introspectedTable);
|
||||
|
||||
StringBuilder insertClause = new StringBuilder();
|
||||
StringBuilder valuesClause = new StringBuilder();
|
||||
|
||||
insertClause.append("insert into "); //$NON-NLS-1$
|
||||
insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
|
||||
insertClause.append(" ("); //$NON-NLS-1$
|
||||
|
||||
valuesClause.append(" ("); //$NON-NLS-1$
|
||||
|
||||
List<String> valuesClauses = new ArrayList<String>();
|
||||
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
|
||||
// 生成foreach下插入values
|
||||
valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
|
||||
if (i + 1 < columns.size()) {
|
||||
insertClause.append(", "); //$NON-NLS-1$
|
||||
valuesClause.append(", "); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
insertClause.append(')');
|
||||
batchInsertEle.addElement(new TextElement(insertClause.toString()));
|
||||
|
||||
valuesClause.append(')');
|
||||
valuesClauses.add(valuesClause.toString());
|
||||
XmlElementGeneratorTools.useGeneratedKeys(batchInsertEle, introspectedTable);
|
||||
|
||||
batchInsertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
batchInsertEle.addElement(XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())));
|
||||
|
||||
// 添加foreach节点
|
||||
XmlElement foreachElement = new XmlElement("foreach");
|
||||
@@ -200,9 +152,8 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
foreachElement.addAttribute(new Attribute("item", "item"));
|
||||
foreachElement.addAttribute(new Attribute("separator", ","));
|
||||
|
||||
for (String clause : valuesClauses) {
|
||||
foreachElement.addElement(new TextElement(clause));
|
||||
}
|
||||
foreachElement.addElement(XmlElementGeneratorTools.generateValues(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item."));
|
||||
|
||||
|
||||
// values 构建
|
||||
batchInsertEle.addElement(new TextElement("values"));
|
||||
@@ -221,7 +172,7 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
CommentTools.addComment(element);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(element, introspectedTable);
|
||||
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
|
||||
|
||||
element.addElement(new TextElement("insert into "+introspectedTable.getFullyQualifiedTableNameAtRuntime()+" ("));
|
||||
|
||||
@@ -253,6 +204,7 @@ public class BatchInsertPlugin extends PluginAdapter {
|
||||
foreachInsertColumnsCheck.addAttribute(new Attribute("separator", ","));
|
||||
|
||||
// 所有表字段
|
||||
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
|
||||
List<IntrospectedColumn> columns1 = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
|
||||
for (int i = 0; i < columns1.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
@@ -96,7 +96,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(topLevelClass.getType());
|
||||
method.addBodyLine("return this.example;");
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加工厂方法example");
|
||||
}
|
||||
@@ -120,7 +120,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
|
||||
Method addMethod = new Method("add");
|
||||
addMethod.setReturnType(innerClass.getType());
|
||||
addMethod.addParameter(new Parameter(innerClass.getType(), "add"));
|
||||
CommentTools.addGeneralMethodComment(addMethod, introspectedTable);
|
||||
CommentTools.addMethodComment(addMethod, introspectedTable);
|
||||
criteriaAddInterface.addMethod(addMethod);
|
||||
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"."+criteriaAddInterface.getType().getShortName()+"增加方法add");
|
||||
|
||||
@@ -138,7 +138,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
|
||||
method.addBodyLine("add.add(this);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return this;");
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加方法andIf");
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
|
||||
method.addBodyLine("this.setOrderByClause(orderByClause);");
|
||||
method.addBodyLine("return this;");
|
||||
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
topLevelClass.addMethod(method);
|
||||
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"增加方法orderBy");
|
||||
|
||||
@@ -179,7 +179,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
|
||||
mOrderByMore.addBodyLine("this.setOrderByClause(sb.toString());");
|
||||
mOrderByMore.addBodyLine("return this;");
|
||||
|
||||
CommentTools.addGeneralMethodComment(mOrderByMore, introspectedTable);
|
||||
CommentTools.addMethodComment(mOrderByMore, introspectedTable);
|
||||
topLevelClass.addMethod(mOrderByMore);
|
||||
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"增加方法orderBy(String ... orderByClauses)");
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setOffset.setName("setOffset");
|
||||
setOffset.addParameter(new Parameter(integerWrapper, "offset"));
|
||||
setOffset.addBodyLine("this.offset = offset;");
|
||||
CommentTools.addGeneralMethodComment(setOffset, introspectedTable);
|
||||
CommentTools.addMethodComment(setOffset, introspectedTable);
|
||||
topLevelClass.addMethod(setOffset);
|
||||
|
||||
Method getOffset = new Method();
|
||||
@@ -98,7 +98,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
getOffset.setReturnType(integerWrapper);
|
||||
getOffset.setName("getOffset");
|
||||
getOffset.addBodyLine("return offset;");
|
||||
CommentTools.addGeneralMethodComment(getOffset, introspectedTable);
|
||||
CommentTools.addMethodComment(getOffset, introspectedTable);
|
||||
topLevelClass.addMethod(getOffset);
|
||||
|
||||
Method setRows = new Method();
|
||||
@@ -106,7 +106,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setRows.setName("setRows");
|
||||
setRows.addParameter(new Parameter(integerWrapper, "rows"));
|
||||
setRows.addBodyLine("this.rows = rows;");
|
||||
CommentTools.addGeneralMethodComment(setRows, introspectedTable);
|
||||
CommentTools.addMethodComment(setRows, introspectedTable);
|
||||
topLevelClass.addMethod(setRows);
|
||||
|
||||
Method getRows = new Method();
|
||||
@@ -114,7 +114,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
getRows.setReturnType(integerWrapper);
|
||||
getRows.setName("getRows");
|
||||
getRows.addBodyLine("return rows;");
|
||||
CommentTools.addGeneralMethodComment(getRows, introspectedTable);
|
||||
CommentTools.addMethodComment(getRows, introspectedTable);
|
||||
topLevelClass.addMethod(getRows);
|
||||
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows的getter和setter实现。");
|
||||
|
||||
@@ -126,7 +126,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setLimit.addParameter(new Parameter(integerWrapper, "rows"));
|
||||
setLimit.addBodyLine("this.rows = rows;");
|
||||
setLimit.addBodyLine("return this;");
|
||||
CommentTools.addGeneralMethodComment(setLimit, introspectedTable);
|
||||
CommentTools.addMethodComment(setLimit, introspectedTable);
|
||||
topLevelClass.addMethod(setLimit);
|
||||
|
||||
Method setLimit2 = new Method();
|
||||
@@ -138,7 +138,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setLimit2.addBodyLine("this.offset = offset;");
|
||||
setLimit2.addBodyLine("this.rows = rows;");
|
||||
setLimit2.addBodyLine("return this;");
|
||||
CommentTools.addGeneralMethodComment(setLimit2, introspectedTable);
|
||||
CommentTools.addMethodComment(setLimit2, introspectedTable);
|
||||
topLevelClass.addMethod(setLimit2);
|
||||
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加limit方法。");
|
||||
|
||||
@@ -151,7 +151,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setPage.addBodyLine("this.offset = page * pageSize;");
|
||||
setPage.addBodyLine("this.rows = pageSize;");
|
||||
setPage.addBodyLine("return this;");
|
||||
CommentTools.addGeneralMethodComment(setPage, introspectedTable);
|
||||
CommentTools.addMethodComment(setPage, introspectedTable);
|
||||
topLevelClass.addMethod(setPage);
|
||||
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法");
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
@@ -50,13 +51,16 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
|
||||
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 DEL_FLAG_NAME = "DEL_FLAG"; // 逻辑删除标志位常量名称
|
||||
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 METHOD_LOGICAL_DELETE = "andDeleted"; // 逻辑删除查询方法
|
||||
|
||||
private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列
|
||||
private String logicalDeleteValue; // 逻辑删除值
|
||||
private String logicalUnDeleteValue; // 逻辑删除值(未删除)
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
@@ -84,6 +88,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
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);
|
||||
// 2. 获取表单独配置,如果有则覆盖全局配置
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){
|
||||
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY);
|
||||
@@ -91,6 +96,9 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){
|
||||
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY);
|
||||
}
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){
|
||||
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY);
|
||||
}
|
||||
// 3. 判断该表是否存在逻辑删除列
|
||||
this.logicalDeleteColumn = null;
|
||||
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
|
||||
@@ -124,6 +132,11 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){
|
||||
logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!");
|
||||
}
|
||||
|
||||
// 4. 判断逻辑删除值是否配置了
|
||||
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){
|
||||
logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +159,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
|
||||
mLogicalDeleteByExample.addParameter(new Parameter(type, "example"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
|
||||
CommentTools.addMethodComment(mLogicalDeleteByExample, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mLogicalDeleteByExample);
|
||||
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByExample。");
|
||||
@@ -191,7 +204,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
}
|
||||
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
|
||||
CommentTools.addMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addImportedTypes(importedTypes);
|
||||
interfaze.addMethod(mLogicalDeleteByPrimaryKey);
|
||||
@@ -330,27 +343,47 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
// 添加删除标志位常量
|
||||
Field field = new Field(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType());
|
||||
field.setVisibility(JavaVisibility.PUBLIC);
|
||||
field.setStatic(true);
|
||||
field.setFinal(true);
|
||||
|
||||
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
|
||||
field.setInitializationString("null");
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
|
||||
field.setInitializationString("\"" + this.logicalDeleteValue + "\"");
|
||||
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){
|
||||
field.setInitializationString((this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false");
|
||||
} else {
|
||||
field.setInitializationString(this.logicalDeleteValue);
|
||||
}
|
||||
CommentTools.addFieldComment(field, introspectedTable);
|
||||
|
||||
// 常量插入到第一位
|
||||
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
|
||||
fields.add(0, field);
|
||||
|
||||
// TODO 过期的
|
||||
Field field2 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG", this.logicalDeleteColumn.getFullyQualifiedJavaType(), DEL_FLAG_NAME, introspectedTable);
|
||||
field2.addAnnotation("@Deprecated");
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field2);
|
||||
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG的常量。");
|
||||
|
||||
// 添加删除标志位常量 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;
|
||||
}
|
||||
Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue, 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;
|
||||
}
|
||||
Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue, introspectedTable);
|
||||
// 常量插入到第一位
|
||||
fields.add(0, field1);
|
||||
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_ON的常量。");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -375,7 +408,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
|
||||
if ("Criteria".equals(innerClass.getType().getShortName())) {
|
||||
// 增加逻辑删除条件
|
||||
Method method = new Method(METHOD_LOGICAL_DELETE);
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(innerClass.getType());
|
||||
method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "deleted"));
|
||||
|
||||
@@ -83,7 +83,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
constructor.setVisibility(JavaVisibility.PUBLIC);
|
||||
constructor.setConstructor(true);
|
||||
constructor.addBodyLine(new StringBuilder("this.obj = new ").append(topLevelClass.getType().getShortName()).append("();").toString());
|
||||
CommentTools.addGeneralMethodComment(constructor, introspectedTable);
|
||||
CommentTools.addMethodComment(constructor, introspectedTable);
|
||||
innerClass.addMethod(constructor);
|
||||
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加的构造方法。");
|
||||
|
||||
@@ -100,7 +100,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
method.addBodyLine(new StringBuilder().append("obj.").append(setterMethod.getName())
|
||||
.append("(").append(field.getName()).append(");").toString());
|
||||
method.addBodyLine(new StringBuilder().append("return this;").toString());
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加"+method.getName()+"方法(复合主键)。");
|
||||
}
|
||||
@@ -118,7 +118,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
method.addBodyLine(new StringBuilder().append("obj.").append(field.getName())
|
||||
.append(" = ").append(field.getName()).append(";").toString());
|
||||
method.addBodyLine(new StringBuilder().append("return this;").toString());
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加"+method.getName()+"方法。");
|
||||
}
|
||||
@@ -127,7 +127,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
build.setReturnType(topLevelClass.getType());
|
||||
build.setVisibility(JavaVisibility.PUBLIC);
|
||||
build.addBodyLine("return this.obj;");
|
||||
CommentTools.addGeneralMethodComment(build, introspectedTable);
|
||||
CommentTools.addMethodComment(build, introspectedTable);
|
||||
innerClass.addMethod(build);
|
||||
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加build方法。");
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.List;
|
||||
* @time:2017/1/17 11:20
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class ModelColumnPlugin extends PluginAdapter {
|
||||
public class ModelColumnPlugin extends PluginAdapter {
|
||||
public static final String ENUM_NAME = "Column"; // 内部Enum名
|
||||
private static final Logger logger = LoggerFactory.getLogger(ModelColumnPlugin.class);
|
||||
|
||||
@@ -46,8 +46,8 @@ public class ModelColumnPlugin extends PluginAdapter {
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 插件使用前提是targetRuntime为MyBatis3
|
||||
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false ){
|
||||
logger.warn("itfsw:插件"+this.getClass().getTypeName()+"要求运行targetRuntime必须为MyBatis3!");
|
||||
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false) {
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -56,7 +56,6 @@ public class ModelColumnPlugin extends PluginAdapter {
|
||||
/**
|
||||
* Model Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
@@ -70,7 +69,7 @@ public class ModelColumnPlugin extends PluginAdapter {
|
||||
innerEnum.setVisibility(JavaVisibility.PUBLIC);
|
||||
innerEnum.setStatic(true);
|
||||
CommentTools.addInnerEnumComment(innerEnum, introspectedTable);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):"+topLevelClass.getType().getShortName()+"增加内部Builder类。");
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
|
||||
|
||||
// 生成属性和构造函数
|
||||
Field columnField = new Field("column", FullyQualifiedJavaType.getStringInstance());
|
||||
@@ -83,23 +82,23 @@ public class ModelColumnPlugin extends PluginAdapter {
|
||||
mValue.setVisibility(JavaVisibility.PUBLIC);
|
||||
mValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
|
||||
mValue.addBodyLine("return this.column;");
|
||||
CommentTools.addGeneralMethodComment(mValue, introspectedTable);
|
||||
CommentTools.addMethodComment(mValue, introspectedTable);
|
||||
innerEnum.addMethod(mValue);
|
||||
|
||||
Method mGetValue = new Method("getValue");
|
||||
mGetValue.setVisibility(JavaVisibility.PUBLIC);
|
||||
mGetValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
|
||||
mGetValue.addBodyLine("return this.column;");
|
||||
CommentTools.addGeneralMethodComment(mGetValue, introspectedTable);
|
||||
CommentTools.addMethodComment(mGetValue, introspectedTable);
|
||||
innerEnum.addMethod(mGetValue);
|
||||
|
||||
Method constructor = new Method(ENUM_NAME);
|
||||
constructor.setConstructor(true);
|
||||
constructor.addBodyLine("this.column = column;");
|
||||
constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
|
||||
CommentTools.addGeneralMethodComment(constructor, introspectedTable);
|
||||
CommentTools.addMethodComment(constructor, introspectedTable);
|
||||
innerEnum.addMethod(constructor);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):"+topLevelClass.getType().getShortName()+".Column增加构造方法和column属性。");
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。");
|
||||
|
||||
// Enum枚举
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
@@ -120,14 +119,14 @@ public class ModelColumnPlugin extends PluginAdapter {
|
||||
desc.setVisibility(JavaVisibility.PUBLIC);
|
||||
desc.setReturnType(FullyQualifiedJavaType.getStringInstance());
|
||||
desc.addBodyLine("return this.column + \" DESC\";");
|
||||
CommentTools.addGeneralMethodComment(desc, introspectedTable);
|
||||
CommentTools.addMethodComment(desc, introspectedTable);
|
||||
innerEnum.addMethod(desc);
|
||||
|
||||
Method asc = new Method("asc");
|
||||
asc.setVisibility(JavaVisibility.PUBLIC);
|
||||
asc.setReturnType(FullyQualifiedJavaType.getStringInstance());
|
||||
asc.addBodyLine("return this.column + \" ASC\";");
|
||||
CommentTools.addGeneralMethodComment(asc, introspectedTable);
|
||||
CommentTools.addMethodComment(asc, introspectedTable);
|
||||
innerEnum.addMethod(asc);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。");
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
|
||||
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
|
||||
method.addParameter(new Parameter(type, "example"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
CommentTools.addMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.*;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Selective 增强插件
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/4/20 15:39
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class SelectiveEnhancedPlugin extends PluginAdapter {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SelectiveEnhancedPlugin.class);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 插件使用前提是targetRuntime为MyBatis3
|
||||
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false) {
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 插件使用前提是使用了ModelColumnPlugin插件
|
||||
if (!PluginTools.checkDependencyPlugin(ModelColumnPlugin.class, getContext())) {
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 插件配置位置最好是在末尾
|
||||
if (PluginTools.getConfigPlugins(getContext()).size() - 1 != PluginTools.getPluginIndex(SelectiveEnhancedPlugin.class, getContext())){
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件建议配置在所有插件末尾以便最后调用,否则某些Selective方法得不到增强!");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// import
|
||||
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewMapInstance());
|
||||
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewHashMapInstance());
|
||||
|
||||
// field
|
||||
Field selectiveColumnsField = new Field("selectiveColumns", new FullyQualifiedJavaType("Map<String, Boolean>"));
|
||||
CommentTools.addFieldComment(selectiveColumnsField, introspectedTable);
|
||||
selectiveColumnsField.setVisibility(JavaVisibility.PRIVATE);
|
||||
selectiveColumnsField.setInitializationString("new HashMap<String, Boolean>()");
|
||||
topLevelClass.addField(selectiveColumnsField);
|
||||
|
||||
// Method isSelective
|
||||
Method mIsSelective = new Method("isSelective");
|
||||
CommentTools.addMethodComment(mIsSelective, introspectedTable);
|
||||
mIsSelective.setVisibility(JavaVisibility.PUBLIC);
|
||||
mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
|
||||
mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
|
||||
topLevelClass.addMethod(mIsSelective);
|
||||
|
||||
// Method isSelective
|
||||
Method mIsSelective1 = new Method("isSelective");
|
||||
CommentTools.addMethodComment(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 selective
|
||||
Method mSelective = new Method("selective");
|
||||
CommentTools.addMethodComment(mSelective, introspectedTable);
|
||||
mSelective.setVisibility(JavaVisibility.PUBLIC);
|
||||
mSelective.setReturnType(topLevelClass.getType());
|
||||
mSelective.addParameter(new Parameter(new FullyQualifiedJavaType(ModelColumnPlugin.ENUM_NAME), "columns", true));
|
||||
mSelective.addBodyLine("this.selectiveColumns.clear();");
|
||||
mSelective.addBodyLine("if (columns != null) {");
|
||||
mSelective.addBodyLine("for (" + ModelColumnPlugin.ENUM_NAME + " column : columns) {");
|
||||
mSelective.addBodyLine("this.selectiveColumns.put(column.value(), true);");
|
||||
mSelective.addBodyLine("}");
|
||||
mSelective.addBodyLine("}");
|
||||
mSelective.addBodyLine("return this;");
|
||||
topLevelClass.addMethod(mSelective);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
List<Element> rootElements = document.getRootElement().getElements();
|
||||
for (Element rootElement : rootElements) {
|
||||
if (rootElement instanceof XmlElement) {
|
||||
XmlElement xmlElement = (XmlElement) rootElement;
|
||||
List<Attribute> attributes = xmlElement.getAttributes();
|
||||
// 查找ID
|
||||
String id = "";
|
||||
for (Attribute attribute : attributes) {
|
||||
if (attribute.getName().equals("id")) {
|
||||
id = attribute.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================== 1. insertSelective ======================================
|
||||
if ("insertSelective".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
for (XmlElement ele : eles) {
|
||||
this.replaceEle(ele, "_parameter.");
|
||||
}
|
||||
}
|
||||
// ====================================== 2. updateByExampleSelective ======================================
|
||||
if ("updateByExampleSelective".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "set");
|
||||
for (XmlElement ele : eles) {
|
||||
this.replaceEle(ele, "record.");
|
||||
}
|
||||
}
|
||||
// ====================================== 3. updateByPrimaryKeySelective ======================================
|
||||
if ("updateByPrimaryKeySelective".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "set");
|
||||
for (XmlElement ele : eles) {
|
||||
this.replaceEle(ele, "_parameter.");
|
||||
}
|
||||
}
|
||||
// ====================================== 4. upsertSelective ======================================
|
||||
if ("upsertSelective".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
for (XmlElement ele : eles) {
|
||||
this.replaceEle(ele, "_parameter.");
|
||||
}
|
||||
}
|
||||
// ====================================== 5. upsertByExampleSelective ======================================
|
||||
if ("upsertByExampleSelective".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
this.replaceEle(eles.get(0), "record.");
|
||||
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
|
||||
this.replaceEleForUpsertByExampleSelective(eles.get(1), "record.", introspectedTable);
|
||||
|
||||
List<XmlElement> eles1 = this.findEle(xmlElement, "set");
|
||||
for (XmlElement ele : eles1) {
|
||||
this.replaceEle(ele, "record.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找当前节点下的指定节点
|
||||
* @param element
|
||||
* @param eleName
|
||||
* @return
|
||||
*/
|
||||
private List<XmlElement> findEle(XmlElement element, String eleName) {
|
||||
List<XmlElement> list = new ArrayList<>();
|
||||
List<Element> elements = element.getElements();
|
||||
for (Element ele : elements) {
|
||||
if (ele instanceof XmlElement) {
|
||||
XmlElement xmlElement = (XmlElement) ele;
|
||||
if (eleName.equalsIgnoreCase(xmlElement.getName())) {
|
||||
list.add(xmlElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换节点if信息
|
||||
* @param element
|
||||
* @param prefix
|
||||
*/
|
||||
private void replaceEle(XmlElement element, String prefix) {
|
||||
// choose
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
// when
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
|
||||
for (Element ele : element.getElements()) {
|
||||
// if的text节点
|
||||
XmlElement xmlElement = (XmlElement) ele;
|
||||
TextElement textElement = (TextElement) xmlElement.getElements().get(0);
|
||||
|
||||
// 找出field 名称
|
||||
String text = textElement.getContent().trim();
|
||||
String field = "";
|
||||
if (text.matches(".*\\s*=\\s*#\\{.*\\},?")) {
|
||||
Pattern pattern = Pattern.compile("(.*?)\\s*=\\s*#\\{.*},?");
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
if (matcher.find()){
|
||||
field = matcher.group(1);
|
||||
}
|
||||
} else if (text.matches("#\\{.*\\},?")) {
|
||||
Pattern pattern = Pattern.compile("#\\{(.*?),.*\\},?");
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
if (matcher.find()){
|
||||
field = matcher.group(1);
|
||||
}
|
||||
} else {
|
||||
field = text.replaceAll(",", "");
|
||||
}
|
||||
|
||||
XmlElement ifEle = new XmlElement("if");
|
||||
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + field + "\')"));
|
||||
ifEle.addElement(textElement);
|
||||
whenEle.addElement(ifEle);
|
||||
}
|
||||
|
||||
// otherwise
|
||||
XmlElement otherwiseEle = new XmlElement("otherwise");
|
||||
for (Element ele : element.getElements()) {
|
||||
otherwiseEle.addElement(ele);
|
||||
}
|
||||
|
||||
chooseEle.addElement(whenEle);
|
||||
chooseEle.addElement(otherwiseEle);
|
||||
|
||||
// 清空原始节点,新增choose节点
|
||||
element.getElements().clear();
|
||||
element.addElement(chooseEle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换节点upsertByExampleSelective if信息
|
||||
* @param element
|
||||
* @param prefix
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void replaceEleForUpsertByExampleSelective(XmlElement element, String prefix, IntrospectedTable introspectedTable) {
|
||||
// choose
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
// when
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
XmlElement eleIf = new XmlElement("if");
|
||||
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + "\')"));
|
||||
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
|
||||
whenEle.addElement(eleIf);
|
||||
}
|
||||
|
||||
// otherwise
|
||||
XmlElement otherwiseEle = new XmlElement("otherwise");
|
||||
for (Element ele : element.getElements()) {
|
||||
otherwiseEle.addElement(ele);
|
||||
}
|
||||
|
||||
chooseEle.addElement(whenEle);
|
||||
chooseEle.addElement(otherwiseEle);
|
||||
|
||||
// 清空原始节点,新增choose节点
|
||||
element.getElements().clear();
|
||||
element.addElement(chooseEle);
|
||||
}
|
||||
}
|
||||
@@ -16,19 +16,22 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.*;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
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.ListUtilities;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -42,6 +45,11 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
private static final Logger logger = LoggerFactory.getLogger(BatchInsertPlugin.class);
|
||||
public static final String METHOD_UPSERT = "upsert"; // 方法名
|
||||
public static final String METHOD_UPSERT_SELECTIVE = "upsertSelective"; // 方法名
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
|
||||
|
||||
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
|
||||
private boolean allowMultiQueries = false; // 是否允许多sql提交
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
@@ -60,6 +68,15 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 插件是否开启了多sql提交
|
||||
Properties properties = this.getProperties();
|
||||
String allowMultiQueries = properties.getProperty(PRE_ALLOW_MULTI_QUERIES);
|
||||
this.allowMultiQueries = allowMultiQueries == null ? false : StringUtility.isTrue(allowMultiQueries);
|
||||
if (this.allowMultiQueries){
|
||||
// 提示用户注意信息
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -81,7 +98,7 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
// 添加参数
|
||||
mUpsert.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mUpsert, introspectedTable);
|
||||
CommentTools.addMethodComment(mUpsert, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsert);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
|
||||
@@ -93,11 +110,39 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
// 添加参数
|
||||
mUpsertSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record"));
|
||||
// 添加方法说明
|
||||
CommentTools.addGeneralMethodComment(mUpsertSelective, introspectedTable);
|
||||
CommentTools.addMethodComment(mUpsertSelective, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
|
||||
|
||||
if (this.allowMultiQueries){
|
||||
// ====================================== 3. upsertByExample ======================================
|
||||
Method mUpsertByExample = new Method(METHOD_UPSERT_BY_EXAMPLE);
|
||||
// 返回值类型
|
||||
mUpsertByExample.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
// 添加参数
|
||||
mUpsertByExample.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"));
|
||||
mUpsertByExample.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")"));
|
||||
// 添加方法说明
|
||||
CommentTools.addMethodComment(mUpsertByExample, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertByExample);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
|
||||
|
||||
// ====================================== 4. upsertByExampleSelective ======================================
|
||||
Method mUpsertByExampleSelective = new Method(METHOD_UPSERT_BY_EXAMPLE_SELECTIVE);
|
||||
// 返回值类型
|
||||
mUpsertByExampleSelective.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
// 添加参数
|
||||
mUpsertByExampleSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"));
|
||||
mUpsertByExampleSelective.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")"));
|
||||
// 添加方法说明
|
||||
CommentTools.addMethodComment(mUpsertByExampleSelective, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertByExampleSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -122,15 +167,15 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
eleUpsert.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(eleUpsert, introspectedTable);
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsert, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsert.addElement(this.generateInsertClause(introspectedTable));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
eleUpsert.addElement(new TextElement("values"));
|
||||
eleUpsert.addElement(this.generateValuesClause(introspectedTable));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns()));
|
||||
eleUpsert.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsert.addElement(this.generateDuplicateClause(introspectedTable));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns()));
|
||||
|
||||
document.getRootElement().addElement(eleUpsert);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
|
||||
@@ -145,190 +190,105 @@ public class UpsertPlugin extends PluginAdapter {
|
||||
eleUpsertSelective.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
CommTools.useGeneratedKeys(eleUpsertSelective, introspectedTable);
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertSelective, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsertSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertSelective.addElement(this.generateInsertSelectiveClause(introspectedTable));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelective.addElement(new TextElement("values"));
|
||||
eleUpsertSelective.addElement(this.generateValuesSelectiveClause(introspectedTable));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelective.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsertSelective.addElement(this.generateDuplicateSelectiveClause(introspectedTable));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(introspectedTable.getAllColumns(), null, false));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
|
||||
if (this.allowMultiQueries){
|
||||
// ====================================== 2. upsertByExample ======================================
|
||||
XmlElement eleUpsertByExample = new XmlElement("insert");
|
||||
eleUpsertByExample.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE));
|
||||
// 参数类型
|
||||
eleUpsertByExample.addAttribute(new Attribute("parameterType", "map"));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
CommentTools.addComment(eleUpsertByExample);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExample, introspectedTable, "record.");
|
||||
|
||||
// insert
|
||||
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExample, false);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExample.addElement(new TextElement(";"));
|
||||
|
||||
// update
|
||||
eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExample.addElement(new TextElement("set"));
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
// update where
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertByExample);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
|
||||
// ====================================== 4. upsertByExampleSelective ======================================
|
||||
XmlElement eleUpsertByExampleSelective = new XmlElement("insert");
|
||||
eleUpsertByExampleSelective.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_SELECTIVE));
|
||||
// 参数类型
|
||||
eleUpsertByExampleSelective.addAttribute(new Attribute("parameterType", "map"));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
CommentTools.addComment(eleUpsertByExampleSelective);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
|
||||
|
||||
// insert
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns(), "record."));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExampleSelective.addElement(new TextElement(";"));
|
||||
|
||||
// update
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("set"));
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
|
||||
// update where
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertByExampleSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通insert
|
||||
* exists 语句
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
* @param element
|
||||
* @param selective
|
||||
*/
|
||||
private Element generateInsertClause(IntrospectedTable introspectedTable){
|
||||
StringBuilder insertClause = new StringBuilder();
|
||||
|
||||
insertClause.append(" (");
|
||||
|
||||
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
|
||||
if (i + 1 < columns.size()) {
|
||||
insertClause.append(", ");
|
||||
}
|
||||
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective){
|
||||
element.addElement(new TextElement("select"));
|
||||
if (selective){
|
||||
element.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns(), "record.", false));
|
||||
} else {
|
||||
element.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns(), "record.", false));
|
||||
}
|
||||
element.addElement(new TextElement("from dual where not exists"));
|
||||
element.addElement(new TextElement("("));
|
||||
element.addElement(new TextElement("select 1 from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
|
||||
insertClause.append(") ");
|
||||
// if example
|
||||
element.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
return new TextElement(insertClause.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通 values
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private Element generateValuesClause(IntrospectedTable introspectedTable){
|
||||
StringBuilder valuesClause = new StringBuilder();
|
||||
|
||||
valuesClause.append(" (");
|
||||
|
||||
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
if (i + 1 < columns.size()) {
|
||||
valuesClause.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
valuesClause.append(") ");
|
||||
|
||||
return new TextElement(valuesClause.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通duplicate
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private Element generateDuplicateClause(IntrospectedTable introspectedTable){
|
||||
StringBuilder duplicateClause = new StringBuilder();
|
||||
|
||||
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
duplicateClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
duplicateClause.append(" = ");
|
||||
duplicateClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
|
||||
if (i + 1 < columns.size()) {
|
||||
duplicateClause.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
return new TextElement(duplicateClause.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通insert
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private Element generateInsertSelectiveClause(IntrospectedTable introspectedTable){
|
||||
XmlElement insertTrimEle = new XmlElement("trim");
|
||||
insertTrimEle.addAttribute(new Attribute("prefix", "("));
|
||||
insertTrimEle.addAttribute(new Attribute("suffix", ")"));
|
||||
insertTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
|
||||
XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
sb.setLength(0);
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append(" != null");
|
||||
insertNotNullElement.addAttribute(new Attribute("test", sb.toString()));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
sb.append(',');
|
||||
insertNotNullElement.addElement(new TextElement(sb.toString()));
|
||||
insertTrimEle.addElement(insertNotNullElement);
|
||||
}
|
||||
|
||||
return insertTrimEle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通 values
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private Element generateValuesSelectiveClause(IntrospectedTable introspectedTable){
|
||||
XmlElement valuesTrimEle = new XmlElement("trim");
|
||||
valuesTrimEle.addAttribute(new Attribute("prefix", "("));
|
||||
valuesTrimEle.addAttribute(new Attribute("suffix", ")"));
|
||||
valuesTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
|
||||
XmlElement valuesNotNullElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
sb.setLength(0);
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append(" != null");
|
||||
valuesNotNullElement.addAttribute(new Attribute("test", sb.toString()));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
sb.append(',');
|
||||
valuesNotNullElement.addElement(new TextElement(sb.toString()));
|
||||
valuesTrimEle.addElement(valuesNotNullElement);
|
||||
}
|
||||
return valuesTrimEle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通duplicate
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private Element generateDuplicateSelectiveClause(IntrospectedTable introspectedTable){
|
||||
XmlElement duplicateTrimEle = new XmlElement("trim");
|
||||
duplicateTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
|
||||
XmlElement duplicateNotNullElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
sb.setLength(0);
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append(" != null");
|
||||
duplicateNotNullElement.addAttribute(new Attribute("test", sb.toString()));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
|
||||
sb.append(",");
|
||||
|
||||
duplicateNotNullElement.addElement(new TextElement(sb.toString()));
|
||||
duplicateTrimEle.addElement(duplicateNotNullElement);
|
||||
}
|
||||
return duplicateTrimEle;
|
||||
element.addElement(new TextElement(")"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.config.GeneratedKey;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 一些通用工具集
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/3/22 14:03
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class CommTools {
|
||||
|
||||
/**
|
||||
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
*/
|
||||
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){
|
||||
GeneratedKey gk = introspectedTable.getGeneratedKey();
|
||||
if (gk != null) {
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
|
||||
// if the column is null, then it's a configuration error. The
|
||||
// warning has already been reported
|
||||
if (introspectedColumn != null) {
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
element.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
element.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
|
||||
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,17 +40,17 @@ public class CommentTools {
|
||||
*/
|
||||
public static void addFieldComment(Field field, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
field.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
field.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的属性(请勿删除)."); //$NON-NLS-1$
|
||||
sb.append(" * This field corresponds to the database table "); //$NON-NLS-1$
|
||||
field.addJavaDocLine("/**");
|
||||
field.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的属性(请勿删除).");
|
||||
sb.append(" * This field corresponds to the database table ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
field.addJavaDocLine(sb.toString());
|
||||
|
||||
field.addJavaDocLine(" *");
|
||||
field.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
field.addJavaDocLine(" * @author hewei");
|
||||
field.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
|
||||
|
||||
field.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
field.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,15 +61,16 @@ public class CommentTools {
|
||||
*/
|
||||
public static void addInnerClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
innerClass.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的类(请勿删除)."); //$NON-NLS-1$
|
||||
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
|
||||
innerClass.addJavaDocLine("/**");
|
||||
innerClass.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的类(请勿删除).");
|
||||
sb.append(" * This class corresponds to the database table ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
innerClass.addJavaDocLine(sb.toString());
|
||||
innerClass.addJavaDocLine(" *");
|
||||
innerClass.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
innerClass.addJavaDocLine(" * @author hewei");
|
||||
innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
innerClass.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
|
||||
|
||||
innerClass.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,15 +81,16 @@ public class CommentTools {
|
||||
*/
|
||||
public static void addInnerEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
innerEnum.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的枚举(请勿删除)."); //$NON-NLS-1$
|
||||
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
|
||||
innerEnum.addJavaDocLine("/**");
|
||||
innerEnum.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的枚举(请勿删除).");
|
||||
sb.append(" * This class corresponds to the database table ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
innerEnum.addJavaDocLine(sb.toString());
|
||||
innerEnum.addJavaDocLine(" *");
|
||||
innerEnum.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
innerEnum.addJavaDocLine(" * @author hewei");
|
||||
innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
innerEnum.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
|
||||
|
||||
innerEnum.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,17 +99,18 @@ public class CommentTools {
|
||||
* @param method 方法
|
||||
* @param introspectedTable 表
|
||||
*/
|
||||
public static void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
|
||||
public static void addMethodComment(Method method, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
method.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
method.addJavaDocLine("/**");
|
||||
method.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的方法(请勿删除).");
|
||||
sb.append(" * This method corresponds to the database table ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
method.addJavaDocLine(" *");
|
||||
method.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
method.addJavaDocLine(" * @author hewei");
|
||||
method.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
method.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
|
||||
|
||||
method.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,16 +120,16 @@ public class CommentTools {
|
||||
*/
|
||||
public static void addComment(XmlElement xmlElement) {
|
||||
|
||||
xmlElement.addElement(new TextElement("<!--")); //$NON-NLS-1$
|
||||
xmlElement.addElement(new TextElement("<!--"));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" WARNING - "); //$NON-NLS-1$
|
||||
sb.append(" WARNING - ");
|
||||
sb.append(MergeConstants.NEW_ELEMENT_TAG);
|
||||
xmlElement.addElement(new TextElement(sb.toString()));
|
||||
xmlElement.addElement(new TextElement(" 这个节点为代码生成工具生成,请不要修改!")); //$NON-NLS-1$
|
||||
xmlElement.addElement(new TextElement(" @author hewei"));
|
||||
xmlElement.addElement(new TextElement(" 这个节点为代码生成工具生成,请不要修改!"));
|
||||
xmlElement.addElement(new TextElement(" @project https://github.com/itfsw/mybatis-generator-plugin"));
|
||||
|
||||
xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$
|
||||
xmlElement.addElement(new TextElement("-->"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,15 +140,16 @@ public class CommentTools {
|
||||
*/
|
||||
public static void addInterfaceComment(Interface interf, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
interf.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
interf.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的接口(请勿删除)."); //$NON-NLS-1$
|
||||
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
|
||||
interf.addJavaDocLine("/**");
|
||||
interf.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的接口(请勿删除).");
|
||||
sb.append(" * This class corresponds to the database table ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
interf.addJavaDocLine(sb.toString());
|
||||
interf.addJavaDocLine(" *");
|
||||
interf.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
interf.addJavaDocLine(" * @author hewei");
|
||||
interf.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
interf.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
|
||||
|
||||
interf.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.Field;
|
||||
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
|
||||
import org.mybatis.generator.api.dom.java.JavaVisibility;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Java ele 生成工具
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/4/21 16:22
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class JavaElementGeneratorTools {
|
||||
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
|
||||
Field field = new Field(fieldName, javaType);
|
||||
CommentTools.addFieldComment(field, introspectedTable);
|
||||
field.setVisibility(JavaVisibility.PUBLIC);
|
||||
field.setStatic(true);
|
||||
field.setFinal(true);
|
||||
if (initString != null){
|
||||
field.setInitializationString(initString);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 插件工具集
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/4/20 15:43
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class PluginTools {
|
||||
private static final Logger logger = LoggerFactory.getLogger(PluginTools.class);
|
||||
|
||||
/**
|
||||
* 检查插件依赖
|
||||
* @param plugin 插件
|
||||
* @param ctx 上下文
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkDependencyPlugin(Class plugin, Context ctx) {
|
||||
return getPluginIndex(plugin, ctx) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件所在位置
|
||||
*
|
||||
* @param plugin 插件
|
||||
* @param ctx 上下文
|
||||
* @return -1:未找到
|
||||
*/
|
||||
public static int getPluginIndex(Class plugin, Context ctx) {
|
||||
List<PluginConfiguration> list = getConfigPlugins(ctx);
|
||||
// 检查是否配置了ModelColumnPlugin插件
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
PluginConfiguration config = list.get(i);
|
||||
if (plugin.getName().equals(config.getConfigurationType())) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件列表
|
||||
* @param ctx 上下文
|
||||
* @return
|
||||
*/
|
||||
public static List<PluginConfiguration> getConfigPlugins(Context ctx) {
|
||||
try {
|
||||
// 利用反射获取pluginConfigurations属性
|
||||
Field field = Context.class.getDeclaredField("pluginConfigurations");
|
||||
field.setAccessible(true);
|
||||
return (List<PluginConfiguration>) field.get(ctx);
|
||||
} catch (Exception e) {
|
||||
logger.error("插件检查反射异常", e);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
+306
-26
@@ -19,10 +19,15 @@ package com.itfsw.mybatis.generator.plugins.utils;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.Element;
|
||||
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.config.GeneratedKey;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator
|
||||
@@ -36,22 +41,19 @@ public class XmlElementGeneratorTools {
|
||||
/**
|
||||
* This method should return an XmlElement for the select key used to
|
||||
* automatically generate keys.
|
||||
*
|
||||
* @param introspectedColumn
|
||||
* the column related to the select key statement
|
||||
* @param generatedKey
|
||||
* the generated key for the current table
|
||||
* @param introspectedColumn the column related to the select key statement
|
||||
* @param generatedKey the generated key for the current table
|
||||
* @return the selectKey element
|
||||
*/
|
||||
public static XmlElement getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
|
||||
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
|
||||
String identityColumnType = introspectedColumn
|
||||
.getFullyQualifiedJavaType().getFullyQualifiedName();
|
||||
|
||||
XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
|
||||
answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
|
||||
XmlElement answer = new XmlElement("selectKey");
|
||||
answer.addAttribute(new Attribute("resultType", identityColumnType));
|
||||
answer.addAttribute(new Attribute(
|
||||
"keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
|
||||
answer.addAttribute(new Attribute("order", //$NON-NLS-1$
|
||||
"keyProperty", introspectedColumn.getJavaProperty()));
|
||||
answer.addAttribute(new Attribute("order",
|
||||
generatedKey.getMyBatis3Order()));
|
||||
|
||||
answer.addElement(new TextElement(generatedKey
|
||||
@@ -60,41 +62,319 @@ public class XmlElementGeneratorTools {
|
||||
return answer;
|
||||
}
|
||||
|
||||
public static XmlElement getBaseColumnListElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
|
||||
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
|
||||
public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement answer = new XmlElement("include");
|
||||
answer.addAttribute(new Attribute("refid",
|
||||
introspectedTable.getBaseColumnListId()));
|
||||
return answer;
|
||||
}
|
||||
|
||||
public static XmlElement getBlobColumnListElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
|
||||
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
|
||||
public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement answer = new XmlElement("include");
|
||||
answer.addAttribute(new Attribute("refid",
|
||||
introspectedTable.getBlobColumnListId()));
|
||||
return answer;
|
||||
}
|
||||
|
||||
public static XmlElement getExampleIncludeElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
public static Element getExampleIncludeElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement ifElement = new XmlElement("if");
|
||||
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
|
||||
|
||||
XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
|
||||
includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
|
||||
XmlElement includeElement = new XmlElement("include");
|
||||
includeElement.addAttribute(new Attribute("refid",
|
||||
introspectedTable.getExampleWhereClauseId()));
|
||||
ifElement.addElement(includeElement);
|
||||
|
||||
return ifElement;
|
||||
}
|
||||
|
||||
public static XmlElement getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
public static Element getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) {
|
||||
XmlElement ifElement = new XmlElement("if");
|
||||
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
|
||||
|
||||
XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
|
||||
includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
|
||||
XmlElement includeElement = new XmlElement("include");
|
||||
includeElement.addAttribute(new Attribute("refid",
|
||||
introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
|
||||
ifElement.addElement(includeElement);
|
||||
|
||||
return ifElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
*/
|
||||
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){
|
||||
useGeneratedKeys(element, introspectedTable, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
* @param prefix
|
||||
*/
|
||||
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix){
|
||||
GeneratedKey gk = introspectedTable.getGeneratedKey();
|
||||
if (gk != null) {
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
|
||||
// if the column is null, then it's a configuration error. The
|
||||
// warning has already been reported
|
||||
if (introspectedColumn != null) {
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
element.addAttribute(new Attribute("useGeneratedKeys", "true"));
|
||||
element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
|
||||
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成keys Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateKeys(List<IntrospectedColumn> columns) {
|
||||
return generateKeys(columns, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成keys Ele
|
||||
* @param columns
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateKeys(List<IntrospectedColumn> columns, boolean bracket) {
|
||||
return generateCommColumns(columns, null, bracket, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成keys Selective Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateKeysSelective(List<IntrospectedColumn> columns) {
|
||||
return generateKeysSelective(columns, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成keys Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @return
|
||||
*/
|
||||
public static Element generateKeysSelective(List<IntrospectedColumn> columns, String prefix) {
|
||||
return generateKeysSelective(columns, prefix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成keys Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateKeysSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
|
||||
return generateCommColumnsSelective(columns, prefix, bracket, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValues(List<IntrospectedColumn> columns) {
|
||||
return generateValues(columns, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValues(List<IntrospectedColumn> columns, String prefix) {
|
||||
return generateValues(columns, prefix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValues(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
|
||||
return generateCommColumns(columns, prefix, bracket, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Selective Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValuesSelective(List<IntrospectedColumn> columns) {
|
||||
return generateValuesSelective(columns, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValuesSelective(List<IntrospectedColumn> columns, String prefix) {
|
||||
return generateValuesSelective(columns, prefix, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成values Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateValuesSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
|
||||
return generateCommColumnsSelective(columns, prefix, bracket, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSets(List<IntrospectedColumn> columns) {
|
||||
return generateSets(columns, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSets(List<IntrospectedColumn> columns, String prefix) {
|
||||
return generateSets(columns, prefix, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
|
||||
return generateCommColumns(columns, prefix, bracket, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Selective Ele
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSetsSelective(List<IntrospectedColumn> columns) {
|
||||
return generateSetsSelective(columns, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSetsSelective(List<IntrospectedColumn> columns, String prefix) {
|
||||
return generateSetsSelective(columns, prefix, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成sets Selective Ele
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @return
|
||||
*/
|
||||
public static Element generateSetsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
|
||||
return generateCommColumnsSelective(columns, prefix, bracket, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用遍历columns
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @param type 1:key,2:value,3:set
|
||||
* @return
|
||||
*/
|
||||
private static Element generateCommColumns(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
|
||||
StringBuffer sb = new StringBuffer(bracket ? "(" : "");
|
||||
Iterator<IntrospectedColumn> columnIterator = columns.iterator();
|
||||
while (columnIterator.hasNext()) {
|
||||
IntrospectedColumn introspectedColumn = columnIterator.next();
|
||||
|
||||
switch (type) {
|
||||
case 3:
|
||||
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
|
||||
break;
|
||||
case 2:
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
|
||||
break;
|
||||
case 1:
|
||||
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
break;
|
||||
}
|
||||
|
||||
if (columnIterator.hasNext()) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
return new TextElement(sb.append(bracket ? ")" : "").toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用遍历columns
|
||||
* @param columns
|
||||
* @param prefix
|
||||
* @param bracket
|
||||
* @param type 1:key,2:value,3:set
|
||||
* @return
|
||||
*/
|
||||
private static Element generateCommColumnsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
|
||||
XmlElement eleTrim = new XmlElement("trim");
|
||||
if (bracket) {
|
||||
eleTrim.addAttribute(new Attribute("prefix", "("));
|
||||
eleTrim.addAttribute(new Attribute("suffix", ")"));
|
||||
eleTrim.addAttribute(new Attribute("suffixOverrides", ","));
|
||||
} else {
|
||||
eleTrim.addAttribute(new Attribute("suffixOverrides", ","));
|
||||
}
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : columns) {
|
||||
XmlElement eleIf = new XmlElement("if");
|
||||
eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null"));
|
||||
|
||||
switch (type) {
|
||||
case 3:
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
|
||||
break;
|
||||
case 2:
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
|
||||
break;
|
||||
case 1:
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ","));
|
||||
break;
|
||||
}
|
||||
|
||||
eleTrim.addElement(eleIf);
|
||||
}
|
||||
|
||||
return eleTrim;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user