Compare commits

...
9 Commits
6 changed files with 124 additions and 37 deletions
+3 -3
View File
@@ -19,7 +19,7 @@
* [自定义注释插件(CommentPlugin)](#13-自定义注释插件)
* [增量插件(IncrementsPlugin)](#14-增量插件)
* [查询结果选择性返回插件(SelectSelectivePlugin)](#15-查询结果选择性返回插件)
* [官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)](#16-官方constructorbased配置bug临时修正插件)
* [~~官方ConstructorBased配置BUG临时修正插件(ConstructorBasedBugFixPlugin)~~](#16-官方constructorbased配置bug临时修正插件)
---------------------------------------
Maven引用:
@@ -27,7 +27,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.18</version>
<version>1.1.0</version>
</dependency>
```
---------------------------------------
@@ -37,7 +37,7 @@ MyBatis Generator 参考配置(插件依赖应该配置在mybatis-generator-ma
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<version>1.3.6</version>
<configuration>
<!-- 配置文件 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
+3 -3
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.18</version>
<version>1.1.0</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@@ -32,7 +32,7 @@
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<version>1.3.6</version>
<scope>provided</scope>
</dependency>
<!-- 日志 -->
@@ -212,7 +212,7 @@
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
<tag>V1.0.18</tag>
<tag>V1.1.0</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -40,6 +40,7 @@ import java.util.regex.Pattern;
* ---------------------------------------------------------------------------
*/
public class SelectiveEnhancedPlugin extends BasePlugin {
public static final String METHOD_HAS_SELECTIVE = "hasSelective"; // 方法名
/**
* {@inheritDoc}
@@ -80,22 +81,22 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
selectiveColumnsField.setInitializationString("new HashMap<String, Boolean>()");
topLevelClass.addField(selectiveColumnsField);
// Method isSelective
Method mIsSelective = new Method("isSelective");
commentGenerator.addGeneralMethodComment(mIsSelective, introspectedTable);
mIsSelective.setVisibility(JavaVisibility.PUBLIC);
mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
topLevelClass.addMethod(mIsSelective);
// Method hasSelective
Method mHasSelective = new Method(METHOD_HAS_SELECTIVE);
commentGenerator.addGeneralMethodComment(mHasSelective, introspectedTable);
mHasSelective.setVisibility(JavaVisibility.PUBLIC);
mHasSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mHasSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
topLevelClass.addMethod(mHasSelective);
// Method isSelective
Method mIsSelective1 = new Method("isSelective");
commentGenerator.addGeneralMethodComment(mIsSelective1, introspectedTable);
mIsSelective1.setVisibility(JavaVisibility.PUBLIC);
mIsSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mIsSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
mIsSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
topLevelClass.addMethod(mIsSelective1);
// Method hasSelective
Method mHasSelective1 = new Method(METHOD_HAS_SELECTIVE);
commentGenerator.addGeneralMethodComment(mHasSelective1, introspectedTable);
mHasSelective1.setVisibility(JavaVisibility.PUBLIC);
mHasSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
mHasSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
mHasSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
topLevelClass.addMethod(mHasSelective1);
// Method selective
Method mSelective = new Method("selective");
@@ -195,7 +196,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
XmlElement chooseEle = new XmlElement("choose");
// when
XmlElement whenEle = new XmlElement("when");
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
for (Element ele : element.getElements()) {
// 对于字符串主键,是没有if判断节点的
if (ele instanceof XmlElement) {
@@ -232,7 +233,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
XmlElement ifEle = new XmlElement("if");
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + column.getActualColumnName() + "\')"));
ifEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + column.getActualColumnName() + "\')"));
for (Element ifChild : xmlElement.getElements()) {
ifEle.addElement(ifChild);
}
@@ -269,10 +270,10 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
XmlElement chooseEle = new XmlElement("choose");
// when
XmlElement whenEle = new XmlElement("when");
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
for (IntrospectedColumn introspectedColumn : (allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns())) {
XmlElement eleIf = new XmlElement("if");
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + introspectedColumn.getActualColumnName() + "\')"));
eleIf.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + introspectedColumn.getActualColumnName() + "\')"));
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
whenEle.addElement(eleIf);
@@ -24,20 +24,23 @@ import org.dom4j.io.SAXReader;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
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.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.*;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
/**
* ---------------------------------------------------------------------------
@@ -52,6 +55,10 @@ public class TemplateCommentGenerator implements CommentGenerator {
private Map<EnumNode, Template> templates = new HashMap<>(); // 模板
private boolean suppressDate = false;
private boolean suppressAllComments = false;
/**
* 构造函数
* @param templatePath 模板路径
@@ -196,7 +203,11 @@ public class TemplateCommentGenerator implements CommentGenerator {
*/
@Override
public void addConfigurationProperties(Properties properties) {
suppressDate = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
}
/**
@@ -466,6 +477,80 @@ public class TemplateCommentGenerator implements CommentGenerator {
addXmlElementComment(rootElement, map, EnumNode.ADD_ROOT_COMMENT);
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
method.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source field: " //$NON-NLS-1$
+ introspectedTable.getFullyQualifiedTable().toString()
+ "." //$NON-NLS-1$
+ introspectedColumn.getActualColumnName();
method.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
field.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source field: " //$NON-NLS-1$
+ introspectedTable.getFullyQualifiedTable().toString()
+ "." //$NON-NLS-1$
+ introspectedColumn.getActualColumnName();
field.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
innerClass.addAnnotation(getGeneratedAnnotation(comment));
}
private String getGeneratedAnnotation(String comment) {
StringBuilder buffer = new StringBuilder();
buffer.append("@Generated("); //$NON-NLS-1$
if (suppressAllComments) {
buffer.append('\"');
} else {
buffer.append("value=\""); //$NON-NLS-1$
}
buffer.append(MyBatisGenerator.class.getName());
buffer.append('\"');
if (!suppressDate && !suppressAllComments) {
buffer.append(", date=\""); //$NON-NLS-1$
buffer.append(DatatypeConverter.printDateTime(Calendar.getInstance()));
buffer.append('\"');
}
if (!suppressAllComments) {
buffer.append(", comments=\""); //$NON-NLS-1$
buffer.append(comment);
buffer.append('\"');
}
buffer.append(')');
return buffer.toString();
}
/**
* 评论模板节点ID
*/
@@ -79,10 +79,10 @@ public class SelectiveEnhancedPluginTest {
Array.set(columns, 1, TbColumnTsIncF2.getObject());
tb.invoke("selective", columns);
Assert.assertTrue((Boolean) tb.invoke("isSelective"));
Assert.assertTrue((Boolean) tb.invoke("isSelective", "field_1"));
Assert.assertFalse((Boolean) tb.invoke("isSelective", "inc_f1"));
Assert.assertTrue((Boolean) tb.invoke("isSelective", "inc_f2"));
Assert.assertTrue((Boolean) tb.invoke("hasSelective"));
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "field_1"));
Assert.assertFalse((Boolean) tb.invoke("hasSelective", "inc_f1"));
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "inc_f2"));
}
});
}
@@ -67,7 +67,6 @@ public abstract class AbstractShellCallback implements ShellCallback {
/**
* 重载项目
*
* @param sqlSession
* @param loader
* @param packagz
@@ -92,7 +91,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
}
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(targetPackage, ".");
StringTokenizer st = new StringTokenizer(targetPackage, ".");
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
sb.append(File.separatorChar);
@@ -112,6 +111,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
/* (non-Javadoc)
* @see org.mybatis.generator.api.ShellCallback#isMergeSupported()
*/
@Override
public boolean isMergeSupported() {
return false;
}
@@ -119,6 +119,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
/* (non-Javadoc)
* @see org.mybatis.generator.api.ShellCallback#isOverwriteEnabled()
*/
@Override
public boolean isOverwriteEnabled() {
return true;
}
@@ -126,9 +127,9 @@ public abstract class AbstractShellCallback implements ShellCallback {
/* (non-Javadoc)
* @see org.mybatis.generator.api.ShellCallback#mergeJavaFile(java.lang.String, java.lang.String, java.lang.String[], java.lang.String)
*/
public String mergeJavaFile(String newFileSource,
String existingFileFullPath, String[] javadocTags, String fileEncoding)
throws ShellException {
@Override
public String mergeJavaFile(String newFileSource, File existingFile,
String[] javadocTags, String fileEncoding) throws ShellException {
throw new UnsupportedOperationException();
}
}