Compare commits

...
12 Commits
Author SHA1 Message Date
hewei 1d99a99edd [maven-release-plugin] prepare release V1.0.8 2017-05-10 10:31:27 +08:00
hewei 6e9a283ad2 fixed Issue #3 紧急修正逻辑删除插件引入的异常 2017-05-10 10:30:16 +08:00
hewei 476b27832d [maven-release-plugin] prepare for next development iteration 2017-05-03 10:21:21 +08:00
hewei 8d6d20ab1d [maven-release-plugin] prepare release V1.0.7 2017-05-03 10:21:03 +08:00
hewei 59975cd126 V1.0.7 2017-05-03 10:19:41 +08:00
hewei b83cc93bce Merge remote-tracking branch 'origin/master'
# Conflicts:
#	README.md
2017-04-28 16:53:15 +08:00
hewei f9e14cb0e5 readme 页类跳转 2017-04-28 16:51:46 +08:00
何伟 240f79f090 Update README.md 2017-04-28 16:48:24 +08:00
hewei e0bee6b8e8 一些代码精简 2017-04-28 16:28:48 +08:00
hewei 6a153b23fa 一些代码精简 2017-04-28 16:11:38 +08:00
hewei ce96a44f9c 一些代码精简 2017-04-28 11:21:54 +08:00
hewei 18d70f25fd 一些代码精简 2017-04-24 14:32:12 +08:00
20 changed files with 845 additions and 670 deletions
+14 -18
View File
@@ -4,16 +4,16 @@
---------------------------------------
插件列表:
* 查询单条数据插件(SelectOneByExamplePlugin)
* MySQL分页插件(LimitPlugin)
* 数据Model链式构建插件(ModelBuilderPlugin)
* Example Criteria 增强插件(ExampleEnhancedPlugin(CriteriaBuilderPlugin这个是老版本叫法,已经弃用))
* Example 目标包修改插件(ExampleTargetPlugin)
* 批量插入插件(BatchInsertPlugin)
* 逻辑删除插件(LogicalDeletePlugin)
* 数据Model属性对应Column获取插件(ModelColumnPlugin)
* 存在即更新插件(UpsertPlugin)
* Selective选择插入更新增强插件(SelectiveEnhancedPlugin(1.0.7-SNAPSHOT))
* [查询单条数据插件(SelectOneByExamplePlugin)](#1-查询单条数据插件)
* [MySQL分页插件(LimitPlugin)](#2-mysql分页插件)
* [数据Model链式构建插件(ModelBuilderPlugin)](#3-数据model链式构建插件)
* [Example Criteria 增强插件(ExampleEnhancedPlugin)](#4-example-增强插件exampleandiforderby)
* [Example 目标包修改插件(ExampleTargetPlugin)](#5-example-目标包修改插件)
* [批量插入插件(BatchInsertPlugin)](#6-批量插入插件)
* [逻辑删除插件(LogicalDeletePlugin)](#7-逻辑删除插件)
* [数据Model属性对应Column获取插件(ModelColumnPlugin)](#8-数据model属性对应column获取插件)
* [存在即更新插件(UpsertPlugin)](#9-存在即更新插件)
* [Selective选择插入更新增强插件(SelectiveEnhancedPlugin)](#10-selective选择插入更新增强插件)
---------------------------------------
Maven引用:
@@ -21,10 +21,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<!-- 稳定版本 -->
<version>1.0.6</version>
<!-- 快照版本(最新提交到中央库,增加了部分功能,欢迎测试反馈) -->
<version>1.0.7-SNAPSHOT</version>
<version>1.0.8</version>
</dependency>
```
---------------------------------------
@@ -299,7 +296,7 @@ public class Test {
因为很多实际项目数据都不允许物理删除,多采用逻辑删除,所以单独为逻辑删除做了一个插件,方便使用。
- 增加logicalDeleteByExample和logicalDeleteByPrimaryKey方法;
- 查询构造工具中增加逻辑删除条件andDeleted(boolean);
- 增加逻辑删除常量DEL_FLAG_OFF(已删除)、DEL_FLAG_ON(未删除)(1.0.7-SNAPSHOT);
- 增加逻辑删除常量DEL_FLAG_OFF(已删除)、DEL_FLAG_ON(未删除);
插件:
```xml
@@ -421,7 +418,7 @@ public class Test {
```
### 9. 存在即更新插件
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))。
2. 在开启allowMultiQueries=true(默认不会开启)情况下支持upsertByExample,upsertByExampleSelective操作,但强力建议不要使用(需保证团队没有使用statement提交sql,否则会存在sql注入风险)([[issues#2]](https://github.com/itfsw/mybatis-generator-plugin/issues/2))。
插件:
```xml
@@ -430,7 +427,6 @@ public class Test {
<!--
支持upsertByExample,upsertByExampleSelective操作
!需开启allowMultiQueries=true多条sql提交操作,所以不建议使用!
! 目前在1.0.7-SNAPSHOT支持,有需要的可以下载试用
-->
<property name="allowMultiQueries" value="true"/>
</plugin>
@@ -485,7 +481,7 @@ public class Test {
}
}
```
### 10. Selective选择插入更新增强插件(1.0.7-SNAPSHOT)
### 10. Selective选择插入更新增强插件
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
WAREN:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.7-SNAPSHOT</version>
<version>1.0.8</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>HEAD</tag>
<tag>V1.0.8</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -16,11 +16,10 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
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;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
@@ -30,9 +29,6 @@ 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;
@@ -46,8 +42,7 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
@Deprecated
public class BatchInsertOldPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(BatchInsertOldPlugin.class);
public class BatchInsertOldPlugin extends BasePlugin {
public static final String METHOD_BATCH_INSERT = "batchInsert"; // 方法名
private boolean hasModelBuilderPlugin; // 是否配置了ModelBuilderPlugin插件
@@ -56,12 +51,6 @@ public class BatchInsertOldPlugin 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!");
return false;
}
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.microsoft.jdbc.sqlserver.SQLServer".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
@@ -72,7 +61,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件已过期,不再维护请使用com.itfsw.mybatis.generator.plugins.BatchInsertPlugin区分batchInsert和batchInsertSelective!");
return true;
return super.validate(warnings);
}
/**
@@ -125,7 +114,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.addMethodComment(method, introspectedTable);
commentGenerator.addGeneralMethodComment(method, introspectedTable);
// interface 增加方法
interfaze.addMethod(method);
@@ -142,7 +131,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
type.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
method.addParameter(new Parameter(type, "list", "@Param(\"list\")"));
// 添加方法说明
CommentTools.addMethodComment(method, introspectedTable);
commentGenerator.addGeneralMethodComment(method, introspectedTable);
// interface 增加方法
interfaze.addMethod(method);
@@ -168,7 +157,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
// 参数类型
element.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(element);
commentGenerator.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
@@ -243,7 +232,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
element.addAttribute(new Attribute("parameterType", FullyQualifiedJavaType.getNewListInstance().getFullyQualifiedName()));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(element);
commentGenerator.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
@@ -16,12 +16,12 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
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;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
@@ -29,9 +29,6 @@ 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.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -43,8 +40,7 @@ import java.util.List;
* @time:2017/1/13 9:33
* ---------------------------------------------------------------------------
*/
public class BatchInsertPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(BatchInsertPlugin.class);
public class BatchInsertPlugin extends BasePlugin {
public static final String METHOD_BATCH_INSERT = "batchInsert"; // 方法名
public static final String METHOD_BATCH_INSERT_SELECTIVE = "batchInsertSelective"; // 方法名
@@ -53,11 +49,6 @@ public class BatchInsertPlugin 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!");
return false;
}
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
@@ -77,7 +68,7 @@ public class BatchInsertPlugin extends PluginAdapter {
// TODO
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件如之前使用1.0.5-的插件可继续使用com.itfsw.mybatis.generator.plugins.BatchInsertOldPlugin或者修改batchInsert(@Param(\"list\") List<Tb> list, @Param(\"insertColumns\") Tb.Column ... selective)调用为batchInsertSelective!");
return true;
return super.validate(warnings);
}
@@ -94,27 +85,30 @@ public class BatchInsertPlugin extends PluginAdapter {
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 1. batchInsert
Method mBatchInsert = new Method(METHOD_BATCH_INSERT);
mBatchInsert.setReturnType(FullyQualifiedJavaType.getIntInstance());
// 添加参数
FullyQualifiedJavaType tList = FullyQualifiedJavaType.getNewListInstance();
tList.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
mBatchInsert.addParameter(new Parameter(tList, "list", "@Param(\"list\")"));
// 添加方法说明
CommentTools.addMethodComment(mBatchInsert, introspectedTable);
FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance();
listType.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
Method mBatchInsert = JavaElementGeneratorTools.generateMethod(
METHOD_BATCH_INSERT,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(listType, "list", "@Param(\"list\")")
);
commentGenerator.addGeneralMethodComment(mBatchInsert, introspectedTable);
// interface 增加方法
interfaze.addMethod(mBatchInsert);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
// 2. batchInsertSelective
Method mBatchInsertSelective = new Method(METHOD_BATCH_INSERT_SELECTIVE);
mBatchInsertSelective.setReturnType(FullyQualifiedJavaType.getIntInstance());
// 添加参数
FullyQualifiedJavaType tSelective = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
mBatchInsertSelective.addParameter(new Parameter(tList, "list", "@Param(\"list\")"));
mBatchInsertSelective.addParameter(new Parameter(tSelective, "selective", "@Param(\"selective\")", true));
// 添加方法说明
CommentTools.addMethodComment(mBatchInsertSelective, introspectedTable);
FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
Method mBatchInsertSelective = JavaElementGeneratorTools.generateMethod(
METHOD_BATCH_INSERT_SELECTIVE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(listType, "list", "@Param(\"list\")"),
new Parameter(selectiveType, "selective", "@Param(\"selective\")", true)
);
commentGenerator.addGeneralMethodComment(mBatchInsertSelective, introspectedTable);
// interface 增加方法
interfaze.addMethod(mBatchInsertSelective);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。");
@@ -138,7 +132,7 @@ public class BatchInsertPlugin extends PluginAdapter {
// 参数类型
batchInsertEle.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(batchInsertEle);
commentGenerator.addComment(batchInsertEle);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(batchInsertEle, introspectedTable);
@@ -169,7 +163,7 @@ public class BatchInsertPlugin extends PluginAdapter {
// 参数类型
element.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(element);
commentGenerator.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
@@ -16,9 +16,6 @@
package com.itfsw.mybatis.generator.plugins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
@@ -31,8 +28,6 @@ import java.util.List;
*/
@Deprecated
public class CriteriaBuilderPlugin extends ExampleEnhancedPlugin{
private static final Logger logger = LoggerFactory.getLogger(CriteriaBuilderPlugin.class);
/**
* {@inheritDoc}
*/
@@ -1,14 +1,11 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.InnerInterface;
import com.itfsw.mybatis.generator.plugins.utils.InnerInterfaceWrapperToInnerClass;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterface;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterfaceWrapperToInnerClass;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -20,25 +17,11 @@ import java.util.List;
* @time:2017/1/16 16:28
* ---------------------------------------------------------------------------
*/
public class ExampleEnhancedPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(ExampleEnhancedPlugin.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;
}
return true;
}
public class ExampleEnhancedPlugin extends BasePlugin {
/**
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
@@ -60,7 +43,7 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
if (!"createCriteriaInternal".equals(method.getName()))
continue;
method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);");
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"修改createCriteriaInternal方法,修改构造Criteria时传入Example对象");
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "修改createCriteriaInternal方法,修改构造Criteria时传入Example对象");
}
// orderBy方法
@@ -71,15 +54,20 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
/**
* 添加工厂方法
*
* @param topLevelClass
* @param innerClass
* @param introspectedTable
*/
private void addFactoryMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) {
Field f = new Field("example", topLevelClass.getType());
f.setVisibility(JavaVisibility.PRIVATE);
innerClass.addField(f);
// example field
Field exampleField = JavaElementGeneratorTools.generateField(
"example",
JavaVisibility.PRIVATE,
topLevelClass.getType(),
null
);
commentGenerator.addFieldComment(exampleField, introspectedTable);
innerClass.addField(exampleField);
// overwrite constructor
List<Method> methods = innerClass.getMethods();
@@ -87,100 +75,118 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
if (method.isConstructor()) {
method.addParameter(new Parameter(topLevelClass.getType(), "example"));
method.addBodyLine("this.example = example;");
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"修改构造方法,增加example参数");
commentGenerator.addGeneralMethodComment(method, introspectedTable);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "修改构造方法,增加example参数");
}
}
// add factory method "example"
Method method = new Method("example");
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(topLevelClass.getType());
method.addBodyLine("return this.example;");
CommentTools.addMethodComment(method, introspectedTable);
innerClass.addMethod(method);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加工厂方法example");
// 添加example工厂方法
Method exampleMethod = JavaElementGeneratorTools.generateMethod(
"example",
JavaVisibility.PUBLIC,
topLevelClass.getType()
);
commentGenerator.addGeneralMethodComment(exampleMethod, introspectedTable);
exampleMethod = JavaElementGeneratorTools.generateMethodBody(
exampleMethod,
"return this.example;"
);
innerClass.addMethod(exampleMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加工厂方法example");
}
/**
* 增强Criteria的链式调用,添加andIf(boolean addIf, CriteriaAdd add)方法,实现链式调用中按条件增加查询语句
*
* @param topLevelClass
* @param innerClass
* @param introspectedTable
*/
private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable){
private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) {
// 添加接口CriteriaAdd
InnerInterface criteriaAddInterface = new InnerInterface("ICriteriaAdd");
criteriaAddInterface.setVisibility(JavaVisibility.PUBLIC);
CommentTools.addInterfaceComment(criteriaAddInterface, introspectedTable);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加接口ICriteriaAdd");
commentGenerator.addInterfaceComment(criteriaAddInterface, introspectedTable);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加接口ICriteriaAdd");
// ICriteriaAdd增加接口add
Method addMethod = new Method("add");
addMethod.setReturnType(innerClass.getType());
addMethod.addParameter(new Parameter(innerClass.getType(), "add"));
CommentTools.addMethodComment(addMethod, introspectedTable);
Method addMethod = JavaElementGeneratorTools.generateMethod(
"add",
JavaVisibility.DEFAULT,
innerClass.getType(),
new Parameter(innerClass.getType(), "add")
);
commentGenerator.addGeneralMethodComment(addMethod, introspectedTable);
criteriaAddInterface.addMethod(addMethod);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"."+criteriaAddInterface.getType().getShortName()+"增加方法add");
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "." + criteriaAddInterface.getType().getShortName() + "增加方法add");
InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass(criteriaAddInterface);
innerClass.addInnerClass(innerClassWrapper);
// 添加andIf方法
Method method = new Method("andIf");
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(innerClass.getType());
method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "ifAdd"));
method.addParameter(new Parameter(criteriaAddInterface.getType(), "add"));
method.addBodyLine("if (ifAdd) {");
method.addBodyLine("add.add(this);");
method.addBodyLine("}");
method.addBodyLine("return this;");
CommentTools.addMethodComment(method, introspectedTable);
innerClass.addMethod(method);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"."+innerClass.getType().getShortName()+"增加方法andIf");
Method andIfMethod = JavaElementGeneratorTools.generateMethod(
"andIf",
JavaVisibility.PUBLIC,
innerClass.getType(),
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "ifAdd"),
new Parameter(criteriaAddInterface.getType(), "add")
);
commentGenerator.addGeneralMethodComment(andIfMethod, introspectedTable);
andIfMethod = JavaElementGeneratorTools.generateMethodBody(
andIfMethod,
"if (ifAdd) {",
"add.add(this);",
"}",
"return this;"
);
innerClass.addMethod(andIfMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加方法andIf");
}
/**
* Example增强了setOrderByClause方法,新增orderBy(String orderByClause)方法直接返回example,增强链式调用,可以一路.下去了。
*
* @param topLevelClass
* @param introspectedTable
*/
private void addOrderByMethodToExample(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){
private void addOrderByMethodToExample(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 添加orderBy
Method method = new Method("orderBy");
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(topLevelClass.getType());
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause"));
method.addBodyLine("this.setOrderByClause(orderByClause);");
method.addBodyLine("return this;");
CommentTools.addMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"增加方法orderBy");
Method orderByMethod = JavaElementGeneratorTools.generateMethod(
"orderBy",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")
);
commentGenerator.addGeneralMethodComment(orderByMethod, introspectedTable);
orderByMethod = JavaElementGeneratorTools.generateMethodBody(
orderByMethod,
"this.setOrderByClause(orderByClause);",
"return this;"
);
topLevelClass.addMethod(orderByMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy");
// 添加orderBy
Method mOrderByMore = new Method("orderBy");
mOrderByMore.setVisibility(JavaVisibility.PUBLIC);
mOrderByMore.setReturnType(topLevelClass.getType());
mOrderByMore.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClauses", true));
Method orderByMethod1 = JavaElementGeneratorTools.generateMethod(
"orderBy",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClauses", true)
);
commentGenerator.addGeneralMethodComment(orderByMethod1, introspectedTable);
orderByMethod1 = JavaElementGeneratorTools.generateMethodBody(
orderByMethod1,
"StringBuffer sb = new StringBuffer();",
"for (int i = 0; i < orderByClauses.length; i++) {",
"sb.append(orderByClauses[i]);",
"if (i < orderByClauses.length - 1) {",
"sb.append(\" , \");",
"}",
"}",
"this.setOrderByClause(sb.toString());",
"return this;"
);
mOrderByMore.addBodyLine("StringBuffer sb = new StringBuffer();");
mOrderByMore.addBodyLine("for (int i = 0; i < orderByClauses.length; i++) {");
mOrderByMore.addBodyLine("sb.append(orderByClauses[i]);");
mOrderByMore.addBodyLine("if (i < orderByClauses.length - 1) {");
mOrderByMore.addBodyLine("sb.append(\" , \");");
mOrderByMore.addBodyLine("}");
mOrderByMore.addBodyLine("}");
mOrderByMore.addBodyLine("this.setOrderByClause(sb.toString());");
mOrderByMore.addBodyLine("return this;");
CommentTools.addMethodComment(mOrderByMore, introspectedTable);
topLevelClass.addMethod(mOrderByMore);
logger.debug("itfsw(Example增强插件):"+topLevelClass.getType().getShortName()+"增加方法orderBy(String ... orderByClauses)");
topLevelClass.addMethod(orderByMethod1);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy(String ... orderByClauses)");
}
}
@@ -18,13 +18,10 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Properties;
@@ -37,10 +34,8 @@ import java.util.Properties;
* @time:2017/1/12 12:36
* ---------------------------------------------------------------------------
*/
public class ExampleTargetPlugin extends PluginAdapter {
public class ExampleTargetPlugin extends BasePlugin {
public static final String TARGET_PACKAGE_KEY = "targetPackage"; // 配置targetPackage名
private static final Logger logger = LoggerFactory.getLogger(ExampleTargetPlugin.class);
private static String targetPackage; // 目标包
/**
@@ -48,11 +43,6 @@ public class ExampleTargetPlugin 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!");
return false;
}
// 获取配置的目标package
Properties properties = getProperties();
this.targetPackage = properties.getProperty(TARGET_PACKAGE_KEY);
@@ -60,7 +50,7 @@ public class ExampleTargetPlugin extends PluginAdapter {
logger.warn("请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!");
return false;
}
return true;
return super.validate(warnings);
}
/**
@@ -16,16 +16,13 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
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.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -37,24 +34,18 @@ import java.util.List;
* @time:2016/12/29 18:14
* ---------------------------------------------------------------------------
*/
public class LimitPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(LimitPlugin.class);
public class LimitPlugin extends BasePlugin {
/**
* {@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;
}
// 该插件只支持MYSQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){
logger.warn("itfsw:插件"+this.getClass().getTypeName()+"只支持MySQL数据库!");
return false;
}
return true;
return super.validate(warnings);
}
/**
@@ -69,89 +60,89 @@ public class LimitPlugin extends PluginAdapter {
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
// 添加offset和rows字段
Field offset = new Field();
offset.setName("offset");
offset.setVisibility(JavaVisibility.PROTECTED);
offset.setType(integerWrapper);
CommentTools.addFieldComment(offset, introspectedTable);
topLevelClass.addField(offset);
Field offsetField = JavaElementGeneratorTools.generateField(
"offset",
JavaVisibility.PROTECTED,
integerWrapper,
null
);
commentGenerator.addFieldComment(offsetField, introspectedTable);
topLevelClass.addField(offsetField);
Field rows = new Field();
rows.setName("rows");
rows.setVisibility(JavaVisibility.PROTECTED);
rows.setType(integerWrapper);
CommentTools.addFieldComment(rows, introspectedTable);
topLevelClass.addField(rows);
Field rowsField = JavaElementGeneratorTools.generateField(
"rows",
JavaVisibility.PROTECTED,
integerWrapper,
null
);
commentGenerator.addFieldComment(rowsField, introspectedTable);
topLevelClass.addField(rowsField);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows字段");
// 增加getter && setter 方法
Method setOffset = new Method();
setOffset.setVisibility(JavaVisibility.PUBLIC);
setOffset.setName("setOffset");
setOffset.addParameter(new Parameter(integerWrapper, "offset"));
setOffset.addBodyLine("this.offset = offset;");
CommentTools.addMethodComment(setOffset, introspectedTable);
topLevelClass.addMethod(setOffset);
Method mSetOffset = JavaElementGeneratorTools.generateSetterMethod(offsetField);
commentGenerator.addGeneralMethodComment(mSetOffset, introspectedTable);
topLevelClass.addMethod(mSetOffset);
Method getOffset = new Method();
getOffset.setVisibility(JavaVisibility.PUBLIC);
getOffset.setReturnType(integerWrapper);
getOffset.setName("getOffset");
getOffset.addBodyLine("return offset;");
CommentTools.addMethodComment(getOffset, introspectedTable);
topLevelClass.addMethod(getOffset);
Method mGetOffset = JavaElementGeneratorTools.generateGetterMethod(offsetField);
commentGenerator.addGeneralMethodComment(mGetOffset, introspectedTable);
topLevelClass.addMethod(mGetOffset);
Method setRows = new Method();
setRows.setVisibility(JavaVisibility.PUBLIC);
setRows.setName("setRows");
setRows.addParameter(new Parameter(integerWrapper, "rows"));
setRows.addBodyLine("this.rows = rows;");
CommentTools.addMethodComment(setRows, introspectedTable);
topLevelClass.addMethod(setRows);
Method mSetRows = JavaElementGeneratorTools.generateSetterMethod(rowsField);
commentGenerator.addGeneralMethodComment(mSetRows, introspectedTable);
topLevelClass.addMethod(mSetRows);
Method getRows = new Method();
getRows.setVisibility(JavaVisibility.PUBLIC);
getRows.setReturnType(integerWrapper);
getRows.setName("getRows");
getRows.addBodyLine("return rows;");
CommentTools.addMethodComment(getRows, introspectedTable);
topLevelClass.addMethod(getRows);
Method mGetRows = JavaElementGeneratorTools.generateGetterMethod(rowsField);
commentGenerator.addGeneralMethodComment(mGetRows, introspectedTable);
topLevelClass.addMethod(mGetRows);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows的getter和setter实现。");
// 提供几个快捷方法
Method setLimit = new Method();
setLimit.setVisibility(JavaVisibility.PUBLIC);
setLimit.setReturnType(topLevelClass.getType());
setLimit.setName("limit");
setLimit.addParameter(new Parameter(integerWrapper, "rows"));
setLimit.addBodyLine("this.rows = rows;");
setLimit.addBodyLine("return this;");
CommentTools.addMethodComment(setLimit, introspectedTable);
Method setLimit = JavaElementGeneratorTools.generateMethod(
"limit",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(integerWrapper, "rows")
);
commentGenerator.addGeneralMethodComment(setLimit, introspectedTable);
setLimit = JavaElementGeneratorTools.generateMethodBody(
setLimit,
"this.rows = rows;",
"return this;"
);
topLevelClass.addMethod(setLimit);
Method setLimit2 = new Method();
setLimit2.setVisibility(JavaVisibility.PUBLIC);
setLimit2.setReturnType(topLevelClass.getType());
setLimit2.setName("limit");
setLimit2.addParameter(new Parameter(integerWrapper, "offset"));
setLimit2.addParameter(new Parameter(integerWrapper, "rows"));
setLimit2.addBodyLine("this.offset = offset;");
setLimit2.addBodyLine("this.rows = rows;");
setLimit2.addBodyLine("return this;");
CommentTools.addMethodComment(setLimit2, introspectedTable);
Method setLimit2 = JavaElementGeneratorTools.generateMethod(
"limit",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(integerWrapper, "offset"),
new Parameter(integerWrapper, "rows")
);
commentGenerator.addGeneralMethodComment(setLimit2, introspectedTable);
setLimit2 = JavaElementGeneratorTools.generateMethodBody(
setLimit2,
"this.offset = offset;",
"this.rows = rows;",
"return this;"
);
topLevelClass.addMethod(setLimit2);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加limit方法。");
Method setPage = new Method();
setPage.setVisibility(JavaVisibility.PUBLIC);
setPage.setReturnType(topLevelClass.getType());
setPage.setName("page");
setPage.addParameter(new Parameter(integerWrapper, "page"));
setPage.addParameter(new Parameter(integerWrapper, "pageSize"));
setPage.addBodyLine("this.offset = page * pageSize;");
setPage.addBodyLine("this.rows = pageSize;");
setPage.addBodyLine("return this;");
CommentTools.addMethodComment(setPage, introspectedTable);
Method setPage = JavaElementGeneratorTools.generateMethod(
"page",
JavaVisibility.PUBLIC,
topLevelClass.getType(),
new Parameter(integerWrapper, "page"),
new Parameter(integerWrapper, "pageSize")
);
commentGenerator.addGeneralMethodComment(setPage, introspectedTable);
setPage = JavaElementGeneratorTools.generateMethodBody(
setPage,
"this.offset = page * pageSize;",
"this.rows = pageSize;",
"return this;"
);
topLevelClass.addMethod(setPage);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法");
@@ -16,21 +16,17 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
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;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
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.MyBatis3FormattingUtilities;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.JDBCType;
import java.util.*;
@@ -43,9 +39,7 @@ import java.util.*;
* @time:2017/1/13 14:08
* ---------------------------------------------------------------------------
*/
public class LogicalDeletePlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(LogicalDeletePlugin.class);
public class LogicalDeletePlugin extends BasePlugin {
public static final String METHOD_LOGICAL_DELETE_BY_EXAMPLE = "logicalDeleteByExample"; // 方法名
public static final String METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY = "logicalDeleteByPrimaryKey"; // 方法名
@@ -62,19 +56,6 @@ public class LogicalDeletePlugin extends PluginAdapter {
private String logicalDeleteValue; // 逻辑删除值
private String logicalUnDeleteValue; // 逻辑删除值(未删除)
/**
* {@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;
}
return true;
}
/**
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@@ -152,14 +133,15 @@ public class LogicalDeletePlugin extends PluginAdapter {
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null){
// 1. 逻辑删除ByExample
Method mLogicalDeleteByExample = new Method(METHOD_LOGICAL_DELETE_BY_EXAMPLE);
// 返回值类型
mLogicalDeleteByExample.setReturnType(FullyQualifiedJavaType.getIntInstance());
// 添加参数
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
mLogicalDeleteByExample.addParameter(new Parameter(type, "example"));
Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
METHOD_LOGICAL_DELETE_BY_EXAMPLE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
);
// 添加方法说明
CommentTools.addMethodComment(mLogicalDeleteByExample, introspectedTable);
commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
// interface 增加方法
interfaze.addMethod(mLogicalDeleteByExample);
logger.debug("itfsw(逻辑删除插件):"+interfaze.getType().getShortName()+"增加方法logicalDeleteByExample。");
@@ -204,7 +186,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
}
// 添加方法说明
CommentTools.addMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
commentGenerator.addGeneralMethodComment(mLogicalDeleteByPrimaryKey, introspectedTable);
// interface 增加方法
interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(mLogicalDeleteByPrimaryKey);
@@ -229,7 +211,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
XmlElement logicalDeleteByExample = new XmlElement("update"); //$NON-NLS-1$
logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$ //$NON-NLS-2$
CommentTools.addComment(logicalDeleteByExample);
commentGenerator.addComment(logicalDeleteByExample);
StringBuilder sb = new StringBuilder();
sb.append("update "); //$NON-NLS-1$
@@ -280,7 +262,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
}
}
logicalDeleteByPrimaryKey.addAttribute(new Attribute("parameterType", parameterClass));
CommentTools.addComment(logicalDeleteByPrimaryKey);
commentGenerator.addComment(logicalDeleteByPrimaryKey);
StringBuilder sb1 = new StringBuilder();
sb1.append("update "); //$NON-NLS-1$
@@ -347,7 +329,8 @@ public class LogicalDeletePlugin extends PluginAdapter {
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
// TODO 过期的
Field field2 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG", this.logicalDeleteColumn.getFullyQualifiedJavaType(), DEL_FLAG_NAME, introspectedTable);
Field field2 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG", this.logicalDeleteColumn.getFullyQualifiedJavaType(), DEL_FLAG_NAME);
commentGenerator.addFieldComment(field2, introspectedTable);
field2.addAnnotation("@Deprecated");
// 常量插入到第一位
fields.add(0, field2);
@@ -364,7 +347,8 @@ public class LogicalDeletePlugin extends PluginAdapter {
} else {
delFlagOnValue = this.logicalDeleteValue;
}
Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue, introspectedTable);
Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue);
commentGenerator.addFieldComment(field, introspectedTable);
// 常量插入到第一位
fields.add(0, field);
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_OFF的常量。");
@@ -380,7 +364,8 @@ public class LogicalDeletePlugin extends PluginAdapter {
} else {
unDelFlagOnValue = this.logicalUnDeleteValue;
}
Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue, introspectedTable);
Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue);
commentGenerator.addFieldComment(field1, introspectedTable);
// 常量插入到第一位
fields.add(0, field1);
logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_ON的常量。");
@@ -408,7 +393,7 @@ public class LogicalDeletePlugin extends PluginAdapter {
if ("Criteria".equals(innerClass.getType().getShortName())) {
// 增加逻辑删除条件
Method method = new Method(METHOD_LOGICAL_DELETE);
CommentTools.addMethodComment(method, introspectedTable);
commentGenerator.addGeneralMethodComment(method, introspectedTable);
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(innerClass.getType());
method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "deleted"));
@@ -16,15 +16,12 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
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.internal.util.JavaBeansUtil;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -36,27 +33,12 @@ import java.util.List;
* @time:2016/12/28 14:56
* ---------------------------------------------------------------------------
*/
public class ModelBuilderPlugin extends PluginAdapter {
public class ModelBuilderPlugin extends BasePlugin {
public static final String BUILDER_CLASS_NAME = "Builder"; // Builder 类名
private static final Logger logger = LoggerFactory.getLogger(ModelBuilderPlugin.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;
}
return true;
}
/**
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
@@ -69,13 +51,12 @@ public class ModelBuilderPlugin extends PluginAdapter {
InnerClass innerClass = new InnerClass(BUILDER_CLASS_NAME);
innerClass.setVisibility(JavaVisibility.PUBLIC);
innerClass.setStatic(true);
CommentTools.addInnerClassComment(innerClass, introspectedTable);
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+"增加内部Builder类。");
commentGenerator.addClassComment(innerClass, introspectedTable);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
// 构建内部obj变量
Field f = new Field("obj", topLevelClass.getType());
f.setVisibility(JavaVisibility.PRIVATE);
CommentTools.addFieldComment(f, introspectedTable);
Field f = JavaElementGeneratorTools.generateField("obj", JavaVisibility.PRIVATE, topLevelClass.getType(), null);
commentGenerator.addFieldComment(f, introspectedTable);
innerClass.addField(f);
// 构造构造方法
@@ -83,53 +64,40 @@ 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.addMethodComment(constructor, introspectedTable);
commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
innerClass.addMethod(constructor);
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加的构造方法。");
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。");
// !!可能Model存在复合主键情况,字段要加上这些
if (topLevelClass.getSuperClass() != null && topLevelClass.getSuperClass().compareTo(new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType())) == 0){
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
Method setterMethod = JavaBeansUtil.getJavaBeansSetter(introspectedColumn, context, introspectedTable);
Method method = new Method(field.getName());
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(innerClass.getType());
method.addParameter(new Parameter(field.getType(), field.getName()));
method.addBodyLine(new StringBuilder().append("obj.").append(setterMethod.getName())
.append("(").append(field.getName()).append(");").toString());
method.addBodyLine(new StringBuilder().append("return this;").toString());
CommentTools.addMethodComment(method, introspectedTable);
innerClass.addMethod(method);
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加"+method.getName()+"方法(复合主键)。");
}
}
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
Method setterMethod = JavaBeansUtil.getJavaBeansSetter(introspectedColumn, context, introspectedTable);
// 根据Model属性生成链式赋值方法
for (Field field : fields) {
if (field.isStatic())
continue;
Method method = new Method(field.getName());
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(innerClass.getType());
method.addParameter(new Parameter(field.getType(), field.getName()));
method.addBodyLine(new StringBuilder().append("obj.").append(field.getName())
.append(" = ").append(field.getName()).append(";").toString());
method.addBodyLine(new StringBuilder().append("return this;").toString());
CommentTools.addMethodComment(method, introspectedTable);
Method method = JavaElementGeneratorTools.generateMethod(
field.getName(),
JavaVisibility.PUBLIC,
innerClass.getType(),
new Parameter(field.getType(), field.getName())
);
commentGenerator.addSetterComment(method, introspectedTable, introspectedColumn);
method = JavaElementGeneratorTools.generateMethodBody(
method,
"obj." + setterMethod.getName() + "(" + field.getName() + ");",
"return this;"
);
innerClass.addMethod(method);
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加"+method.getName()+"方法。");
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加" + method.getName() + "方法(复合主键)。");
}
Method build = new Method("build");
build.setReturnType(topLevelClass.getType());
build.setVisibility(JavaVisibility.PUBLIC);
Method build = JavaElementGeneratorTools.generateMethod(
"build",
JavaVisibility.PUBLIC,
topLevelClass.getType()
);
build.addBodyLine("return this.obj;");
CommentTools.addMethodComment(build, introspectedTable);
commentGenerator.addGeneralMethodComment(build, introspectedTable);
innerClass.addMethod(build);
logger.debug("itfsw(数据Model链式构建插件):"+topLevelClass.getType().getShortName()+".Builder增加build方法。");
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");
topLevelClass.addInnerClass(innerClass);
return true;
@@ -16,15 +16,11 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
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.internal.util.JavaBeansUtil;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@@ -36,22 +32,8 @@ import java.util.List;
* @time:2017/1/17 11:20
* ---------------------------------------------------------------------------
*/
public class ModelColumnPlugin extends PluginAdapter {
public class ModelColumnPlugin extends BasePlugin {
public static final String ENUM_NAME = "Column"; // 内部Enum名
private static final Logger logger = LoggerFactory.getLogger(ModelColumnPlugin.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;
}
return true;
}
/**
* Model Methods 生成
@@ -68,35 +50,35 @@ public class ModelColumnPlugin extends PluginAdapter {
InnerEnum innerEnum = new InnerEnum(new FullyQualifiedJavaType(ENUM_NAME));
innerEnum.setVisibility(JavaVisibility.PUBLIC);
innerEnum.setStatic(true);
CommentTools.addInnerEnumComment(innerEnum, introspectedTable);
commentGenerator.addEnumComment(innerEnum, introspectedTable);
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
// 生成属性和构造函数
Field columnField = new Field("column", FullyQualifiedJavaType.getStringInstance());
columnField.setVisibility(JavaVisibility.PRIVATE);
columnField.setFinal(true);
CommentTools.addFieldComment(columnField, introspectedTable);
commentGenerator.addFieldComment(columnField, introspectedTable);
innerEnum.addField(columnField);
Method mValue = new Method("value");
mValue.setVisibility(JavaVisibility.PUBLIC);
mValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
mValue.addBodyLine("return this.column;");
CommentTools.addMethodComment(mValue, introspectedTable);
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
innerEnum.addMethod(mValue);
Method mGetValue = new Method("getValue");
mGetValue.setVisibility(JavaVisibility.PUBLIC);
mGetValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
mGetValue.addBodyLine("return this.column;");
CommentTools.addMethodComment(mGetValue, introspectedTable);
commentGenerator.addGeneralMethodComment(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.addMethodComment(constructor, introspectedTable);
commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
innerEnum.addMethod(constructor);
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。");
@@ -119,14 +101,14 @@ public class ModelColumnPlugin extends PluginAdapter {
desc.setVisibility(JavaVisibility.PUBLIC);
desc.setReturnType(FullyQualifiedJavaType.getStringInstance());
desc.addBodyLine("return this.column + \" DESC\";");
CommentTools.addMethodComment(desc, introspectedTable);
commentGenerator.addGeneralMethodComment(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.addMethodComment(asc, introspectedTable);
commentGenerator.addGeneralMethodComment(asc, introspectedTable);
innerEnum.addMethod(asc);
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。");
@@ -16,20 +16,15 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
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.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.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
@@ -41,22 +36,8 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* @time:2016/12/28 14:56
* ---------------------------------------------------------------------------
*/
public class SelectOneByExamplePlugin extends PluginAdapter {
public static final String METHOD_NAME = "selectOneByExample"; // 方法名
private static final Logger logger = LoggerFactory.getLogger(SelectOneByExamplePlugin.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;
}
return true;
}
public class SelectOneByExamplePlugin extends BasePlugin {
public static final String METHOD_SELECT_ONE_BY_EXAMPLE = "selectOneByExample"; // 方法名
/**
* Java Client Methods 生成
@@ -70,17 +51,13 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 方法生成
Method method = new Method(METHOD_NAME);
// 方法可见性 interface会忽略
// method.setVisibility(JavaVisibility.PUBLIC);
// 返回值类型
FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
method.setReturnType(returnType);
// 添加参数
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
method.addParameter(new Parameter(type, "example"));
// 添加方法说明
CommentTools.addMethodComment(method, introspectedTable);
Method method = JavaElementGeneratorTools.generateMethod(
METHOD_SELECT_ONE_BY_EXAMPLE,
JavaVisibility.DEFAULT,
introspectedTable.getRules().calculateAllFieldsClass(),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
);
commentGenerator.addGeneralMethodComment(method, introspectedTable);
// interface 增加方法
interfaze.addMethod(method);
@@ -104,10 +81,10 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
// 生成查询语句
XmlElement selectOneElement = new XmlElement("select");
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(selectOneElement);
commentGenerator.addComment(selectOneElement);
// 添加ID
selectOneElement.addAttribute(new Attribute("id", METHOD_NAME));
selectOneElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE));
// ----------------------------------------- 表中是否有blob类型字段 ---------------------------------------
if (introspectedTable.hasBLOBColumns()){
@@ -16,17 +16,13 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
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;
@@ -41,19 +37,13 @@ import java.util.regex.Pattern;
* @time:2017/4/20 15:39
* ---------------------------------------------------------------------------
*/
public class SelectiveEnhancedPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(SelectiveEnhancedPlugin.class);
public class SelectiveEnhancedPlugin extends BasePlugin {
/**
* {@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())) {
@@ -66,7 +56,7 @@ public class SelectiveEnhancedPlugin extends PluginAdapter {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件建议配置在所有插件末尾以便最后调用,否则某些Selective方法得不到增强!");
}
return true;
return super.validate(warnings);
}
/**
@@ -84,14 +74,14 @@ public class SelectiveEnhancedPlugin extends PluginAdapter {
// field
Field selectiveColumnsField = new Field("selectiveColumns", new FullyQualifiedJavaType("Map<String, Boolean>"));
CommentTools.addFieldComment(selectiveColumnsField, introspectedTable);
commentGenerator.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);
commentGenerator.addGeneralMethodComment(mIsSelective, introspectedTable);
mIsSelective.setVisibility(JavaVisibility.PUBLIC);
mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
@@ -99,7 +89,7 @@ public class SelectiveEnhancedPlugin extends PluginAdapter {
// Method isSelective
Method mIsSelective1 = new Method("isSelective");
CommentTools.addMethodComment(mIsSelective1, introspectedTable);
commentGenerator.addGeneralMethodComment(mIsSelective1, introspectedTable);
mIsSelective1.setVisibility(JavaVisibility.PUBLIC);
mIsSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mIsSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
@@ -108,7 +98,7 @@ public class SelectiveEnhancedPlugin extends PluginAdapter {
// Method selective
Method mSelective = new Method("selective");
CommentTools.addMethodComment(mSelective, introspectedTable);
commentGenerator.addGeneralMethodComment(mSelective, introspectedTable);
mSelective.setVisibility(JavaVisibility.PUBLIC);
mSelective.setReturnType(topLevelClass.getType());
mSelective.addParameter(new Parameter(new FullyQualifiedJavaType(ModelColumnPlugin.ENUM_NAME), "columns", true));
@@ -16,10 +16,10 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
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.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
@@ -27,8 +27,6 @@ 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;
@@ -41,8 +39,7 @@ import java.util.Properties;
* @time:2017/3/21 10:59
* ---------------------------------------------------------------------------
*/
public class UpsertPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(BatchInsertPlugin.class);
public class UpsertPlugin extends BasePlugin {
public static final String METHOD_UPSERT = "upsert"; // 方法名
public static final String METHOD_UPSERT_SELECTIVE = "upsertSelective"; // 方法名
public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名
@@ -56,11 +53,6 @@ public class UpsertPlugin 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!");
return false;
}
// 插件使用前提是数据库为MySQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){
@@ -77,7 +69,7 @@ public class UpsertPlugin extends PluginAdapter {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
}
return true;
return super.validate(warnings);
}
/**
@@ -92,52 +84,52 @@ public class UpsertPlugin extends PluginAdapter {
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// ====================================== 1. upsert ======================================
Method mUpsert = new Method(METHOD_UPSERT);
// 返回值类型
mUpsert.setReturnType(FullyQualifiedJavaType.getIntInstance());
// 添加参数
mUpsert.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record"));
// 添加方法说明
CommentTools.addMethodComment(mUpsert, introspectedTable);
Method mUpsert = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
);
commentGenerator.addGeneralMethodComment(mUpsert, introspectedTable);
// interface 增加方法
interfaze.addMethod(mUpsert);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
// ====================================== 2. upsertSelective ======================================
Method mUpsertSelective = new Method(METHOD_UPSERT_SELECTIVE);
// 返回值类型
mUpsertSelective.setReturnType(FullyQualifiedJavaType.getIntInstance());
// 添加参数
mUpsertSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record"));
// 添加方法说明
CommentTools.addMethodComment(mUpsertSelective, introspectedTable);
Method mUpsertSelective = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_SELECTIVE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
);
commentGenerator.addGeneralMethodComment(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);
Method mUpsertByExample = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_BY_EXAMPLE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
);
commentGenerator.addGeneralMethodComment(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);
Method mUpsertByExampleSelective = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
);
commentGenerator.addGeneralMethodComment(mUpsertByExampleSelective, introspectedTable);
// interface 增加方法
interfaze.addMethod(mUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
@@ -161,7 +153,7 @@ public class UpsertPlugin extends PluginAdapter {
XmlElement eleUpsert = new XmlElement("insert");
eleUpsert.addAttribute(new Attribute("id", METHOD_UPSERT));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(eleUpsert);
commentGenerator.addComment(eleUpsert);
// 参数类型
eleUpsert.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
@@ -184,7 +176,7 @@ public class UpsertPlugin extends PluginAdapter {
XmlElement eleUpsertSelective = new XmlElement("insert");
eleUpsertSelective.addAttribute(new Attribute("id", METHOD_UPSERT_SELECTIVE));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(eleUpsertSelective);
commentGenerator.addComment(eleUpsertSelective);
// 参数类型
eleUpsertSelective.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
@@ -210,7 +202,7 @@ public class UpsertPlugin extends PluginAdapter {
// 参数类型
eleUpsertByExample.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(eleUpsertByExample);
commentGenerator.addComment(eleUpsertByExample);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExample, introspectedTable, "record.");
@@ -239,7 +231,7 @@ public class UpsertPlugin extends PluginAdapter {
// 参数类型
eleUpsertByExampleSelective.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(eleUpsertByExampleSelective);
commentGenerator.addComment(eleUpsertByExampleSelective);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
@@ -0,0 +1,52 @@
/*
* 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 com.itfsw.mybatis.generator.plugins.utils.enhanced.IWCommentGenerator;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* ---------------------------------------------------------------------------
* 基础plugin
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/4/28 13:57
* ---------------------------------------------------------------------------
*/
public class BasePlugin extends PluginAdapter {
protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志
protected IWCommentGenerator commentGenerator = new IWCommentGenerator(); // 注释工具
/**
* {@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;
}
return true;
}
}
@@ -1,155 +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.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
/**
* ---------------------------------------------------------------------------
* 插件评论生成工具
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/28 17:57
* ---------------------------------------------------------------------------
*/
public class CommentTools {
/**
* 生成通用属性注解
*
* @param field
* @param introspectedTable
*/
public static void addFieldComment(Field field, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
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(" * @project https://github.com/itfsw/mybatis-generator-plugin");
field.addJavaDocLine(" */");
}
/**
* 生成通用内部类注解
*
* @param innerClass 类
* @param introspectedTable 表
*/
public static void addInnerClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
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(" * @project https://github.com/itfsw/mybatis-generator-plugin");
innerClass.addJavaDocLine(" */");
}
/**
* 生成通用内部Enum注释
*
* @param innerEnum 类
* @param introspectedTable 表
*/
public static void addInnerEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
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(" * @project https://github.com/itfsw/mybatis-generator-plugin");
innerEnum.addJavaDocLine(" */");
}
/**
* 生成通用方法注解
*
* @param method 方法
* @param introspectedTable 表
*/
public static void addMethodComment(Method method, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
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(" * @project https://github.com/itfsw/mybatis-generator-plugin");
method.addJavaDocLine(" */");
}
/**
* 生成xml element 注释
*
* @param xmlElement the xml element
*/
public static void addComment(XmlElement xmlElement) {
xmlElement.addElement(new TextElement("<!--"));
StringBuilder sb = new StringBuilder();
sb.append(" WARNING - ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
xmlElement.addElement(new TextElement(sb.toString()));
xmlElement.addElement(new TextElement(" 这个节点为代码生成工具生成,请不要修改!"));
xmlElement.addElement(new TextElement(" @project https://github.com/itfsw/mybatis-generator-plugin"));
xmlElement.addElement(new TextElement("-->"));
}
/**
* 生成通用接口注解
*
* @param interf 接口
* @param introspectedTable 表
*/
public static void addInterfaceComment(Interface interf, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
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(" * @project https://github.com/itfsw/mybatis-generator-plugin");
interf.addJavaDocLine(" */");
}
}
@@ -16,10 +16,7 @@
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;
import org.mybatis.generator.api.dom.java.*;
/**
* ---------------------------------------------------------------------------
@@ -30,9 +27,17 @@ import org.mybatis.generator.api.dom.java.JavaVisibility;
* ---------------------------------------------------------------------------
*/
public class JavaElementGeneratorTools {
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
/**
* 生成静态常量
*
* @param fieldName 常量名称
* @param javaType 类型
* @param initString 初始化字段
* @return
*/
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString){
Field field = new Field(fieldName, javaType);
CommentTools.addFieldComment(field, introspectedTable);
field.setVisibility(JavaVisibility.PUBLIC);
field.setStatic(true);
field.setFinal(true);
@@ -41,4 +46,91 @@ public class JavaElementGeneratorTools {
}
return field;
}
/**
* 生成属性
*
* @param fieldName 常量名称
* @param visibility 可见性
* @param javaType 类型
* @param initString 初始化字段
* @return
*/
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString){
Field field = new Field(fieldName, javaType);
field.setVisibility(visibility);
if (initString != null){
field.setInitializationString(initString);
}
return field;
}
/**
* 生成方法
*
* @param methodName 方法名
* @param visibility 可见性
* @param returnType 返回值类型
* @param parameters 参数列表
* @return
*/
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter ... parameters){
Method method = new Method(methodName);
method.setVisibility(visibility);
method.setReturnType(returnType);
if (parameters != null){
for (Parameter parameter: parameters) {
method.addParameter(parameter);
}
}
return method;
}
/**
* 生成方法实现体
*
* @param method 方法
* @param bodyLines 方法实现行
* @return
*/
public static Method generateMethodBody(Method method, String ... bodyLines){
if (bodyLines != null){
for (String bodyLine: bodyLines){
method.addBodyLine(bodyLine);
}
}
return method;
}
/**
* 生成Filed的Set方法
*
* @param field field
* @return
*/
public static Method generateSetterMethod(Field field){
Method method = generateMethod(
"set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
null,
new Parameter(field.getType(), field.getName())
);
return generateMethodBody(method, "this." + field.getName() + " = " + field.getName() + ";");
}
/**
* 生成Filed的Get方法
*
* @param field field
* @return
*/
public static Method generateGetterMethod(Field field){
Method method = generateMethod(
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
field.getType()
);
return generateMethodBody(method, "return this." + field.getName() + ";");
}
}
@@ -0,0 +1,335 @@
/*
* Copyright (c) 2017.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.itfsw.mybatis.generator.plugins.utils.enhanced;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.internal.util.StringUtility;
import java.util.Properties;
/**
* ---------------------------------------------------------------------------
* 注释生成工具
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/4/28 15:11
* ---------------------------------------------------------------------------
*/
public class IWCommentGenerator implements CommentGenerator {
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addJavaFileComment(org.mybatis.generator.api.dom.java.CompilationUnit)
*/
public void addJavaFileComment(CompilationUnit compilationUnit) {
// add no file level comments by default
}
/**
* Adds a suitable comment to warn users that the element was generated, and when it was generated.
* @param xmlElement the xml element
*/
public void addComment(XmlElement xmlElement) {
xmlElement.addElement(new TextElement("<!--"));
StringBuilder sb = new StringBuilder();
sb.append(" WARNING - ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
xmlElement.addElement(new TextElement(sb.toString()));
xmlElement.addElement(new TextElement(" This element is automatically generated by MyBatis Generator, do not modify."));
xmlElement.addElement(new TextElement(" @project https://github.com/itfsw/mybatis-generator-plugin"));
xmlElement.addElement(new TextElement("-->"));
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addRootComment(org.mybatis.generator.api.dom.xml.XmlElement)
*/
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addConfigurationProperties(java.util.Properties)
*/
public void addConfigurationProperties(Properties properties) {
}
/**
* This method adds the custom javadoc tag for. You may do nothing if you do not wish to include the Javadoc tag -
* however, if you do not include the Javadoc tag then the Java merge capability of the eclipse plugin will break.
* @param javaElement the java element
* @param markAsDoNotDelete the mark as do not delete
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge");
}
javaElement.addJavaDocLine(sb.toString());
javaElement.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable)
*/
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
sb.append(" * This class corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, false);
innerClass.addJavaDocLine(" */");
}
/**
* inner inter face 注释
*
* @param innerInterface
* @param introspectedTable
*/
public void addInterfaceComment(Interface innerInterface, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
innerInterface.addJavaDocLine("/**");
innerInterface.addJavaDocLine(" * This interface was generated by MyBatis Generator.");
sb.append(" * This interface corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerInterface.addJavaDocLine(sb.toString());
addJavadocTag(innerInterface, false);
innerInterface.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addTopLevelClassComment(org.mybatis.generator.api.dom.java.TopLevelClass, org.mybatis.generator.api.IntrospectedTable)
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
topLevelClass.addJavaDocLine("/**");
String remarks = introspectedTable.getRemarks();
if (StringUtility.stringHasValue(remarks)) {
topLevelClass.addJavaDocLine(" * Database Table Remarks:");
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
topLevelClass.addJavaDocLine(" * " + remarkLine);
}
}
topLevelClass.addJavaDocLine(" *");
topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
sb.append(" * This class corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(sb.toString());
addJavadocTag(topLevelClass, true);
topLevelClass.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addEnumComment(org.mybatis.generator.api.dom.java.InnerEnum, org.mybatis.generator.api.IntrospectedTable)
*/
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**");
innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator.");
sb.append(" * This enum corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString());
addJavadocTag(innerEnum, false);
innerEnum.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn)
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
field.addJavaDocLine("/**");
String remarks = introspectedColumn.getRemarks();
if (StringUtility.stringHasValue(remarks)) {
field.addJavaDocLine(" * Database Column Remarks:");
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
field.addJavaDocLine(" * " + remarkLine);
}
}
field.addJavaDocLine(" *");
field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
StringBuilder sb = new StringBuilder();
sb.append(" * This field corresponds to the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable)
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
sb.append(" * This field corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString());
addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addGeneralMethodComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable)
*/
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
sb.append(" * This method corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addGetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn)
*/
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
sb.append(" * This method returns the value of the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *");
sb.setLength(0);
sb.append(" * @return the value of ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addSetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn)
*/
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
sb.append(" * This method sets the value of the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *");
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.append(parm.getName());
sb.append(" the value for ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
/* (non-Javadoc)
* @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable, boolean)
*/
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
sb.append(" * This class corresponds to the database table ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */");
}
}
@@ -1,22 +1,20 @@
/*
* Copyright (c) 2017.
*
* * 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.
* 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;
package com.itfsw.mybatis.generator.plugins.utils.enhanced;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.java.*;
@@ -1,22 +1,20 @@
/*
* Copyright (c) 2017.
*
* * 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.
* 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;
package com.itfsw.mybatis.generator.plugins.utils.enhanced;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;