一些代码精简

This commit is contained in:
hewei
2017-04-24 14:32:12 +08:00
parent c062929a60
commit 18d70f25fd
8 changed files with 352 additions and 232 deletions
@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn;
@@ -94,27 +95,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(),
introspectedTable,
new Parameter(listType, "list", "@Param(\"list\")")
);
// 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(),
introspectedTable,
new Parameter(listType, "list", "@Param(\"list\")"),
new Parameter(selectiveType, "selective", "@Param(\"selective\")", true)
);
// interface 增加方法
interfaze.addMethod(mBatchInsertSelective);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。");
@@ -1,8 +1,9 @@
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.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.*;
@@ -20,16 +21,17 @@ import java.util.List;
* @time:2017/1/16 16:28
* ---------------------------------------------------------------------------
*/
public class ExampleEnhancedPlugin extends PluginAdapter {
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!");
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
return false;
}
return true;
@@ -38,7 +40,6 @@ public class ExampleEnhancedPlugin extends PluginAdapter {
/**
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
@@ -60,7 +61,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 +72,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,
introspectedTable
);
innerClass.addField(exampleField);
// overwrite constructor
List<Method> methods = innerClass.getMethods();
@@ -87,100 +93,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参数");
CommentTools.addMethodComment(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(),
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");
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(),
introspectedTable,
new Parameter(innerClass.getType(), "add")
);
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(),
introspectedTable,
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "ifAdd"),
new Parameter(criteriaAddInterface.getType(), "add")
);
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(),
introspectedTable,
new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")
);
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(),
introspectedTable,
new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClauses", true)
);
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)");
}
}
@@ -16,7 +16,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
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.*;
@@ -69,89 +69,79 @@ 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,
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,
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);
topLevelClass.addMethod(JavaElementGeneratorTools.generateSetterMethod(offsetField, introspectedTable));
topLevelClass.addMethod(JavaElementGeneratorTools.generateGetterMethod(offsetField, introspectedTable));
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 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 getRows = new Method();
getRows.setVisibility(JavaVisibility.PUBLIC);
getRows.setReturnType(integerWrapper);
getRows.setName("getRows");
getRows.addBodyLine("return rows;");
CommentTools.addMethodComment(getRows, introspectedTable);
topLevelClass.addMethod(getRows);
topLevelClass.addMethod(JavaElementGeneratorTools.generateSetterMethod(rowsField, introspectedTable));
topLevelClass.addMethod(JavaElementGeneratorTools.generateGetterMethod(rowsField, introspectedTable));
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(),
introspectedTable,
new Parameter(integerWrapper, "rows")
);
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(),
introspectedTable,
new Parameter(integerWrapper, "offset"),
new Parameter(integerWrapper, "rows")
);
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(),
introspectedTable,
new Parameter(integerWrapper, "page"),
new Parameter(integerWrapper, "pageSize")
);
setPage = JavaElementGeneratorTools.generateMethodBody(
setPage,
"this.offset = page * pageSize;",
"this.rows = pageSize;",
"return this;"
);
topLevelClass.addMethod(setPage);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法");
@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
@@ -42,7 +43,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* ---------------------------------------------------------------------------
*/
public class SelectOneByExamplePlugin extends PluginAdapter {
public static final String METHOD_NAME = "selectOneByExample"; // 方法名
public static final String METHOD_SELECT_ONE_BY_EXAMPLE = "selectOneByExample"; // 方法名
private static final Logger logger = LoggerFactory.getLogger(SelectOneByExamplePlugin.class);
/**
@@ -70,17 +71,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(),
introspectedTable,
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
);
// interface 增加方法
interfaze.addMethod(method);
@@ -107,7 +104,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
CommentTools.addComment(selectOneElement);
// 添加ID
selectOneElement.addAttribute(new Attribute("id", METHOD_NAME));
selectOneElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE));
// ----------------------------------------- 表中是否有blob类型字段 ---------------------------------------
if (introspectedTable.hasBLOBColumns()){
@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
@@ -48,6 +49,11 @@ public class UpsertPlugin extends PluginAdapter {
public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
public static final String METHOD_BATCH_UPSERT = "batchUpsert"; // 方法名
public static final String METHOD_BATCH_UPSERT_SELECTIVE = "batchUpsertSelective"; // 方法名
public static final String METHOD_BATCH_UPSERT_BY_EXAMPLE = "batchUpsertByExample"; // 方法名
public static final String METHOD_BATCH_UPSERT_BY_EXAMPLE_SELECTIVE = "batchUpsertByExampleSelective"; // 方法名
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
private boolean allowMultiQueries = false; // 是否允许多sql提交
@@ -92,52 +98,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(),
introspectedTable,
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
);
// 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(),
introspectedTable,
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
);
// 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(),
introspectedTable,
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
);
// 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(),
introspectedTable,
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
);
// interface 增加方法
interfaze.addMethod(mUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
@@ -17,9 +17,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,6 +28,16 @@ import org.mybatis.generator.api.dom.java.JavaVisibility;
* ---------------------------------------------------------------------------
*/
public class JavaElementGeneratorTools {
/**
* 生成静态常量
*
* @param fieldName 常量名称
* @param javaType 类型
* @param initString 初始化字段
* @param introspectedTable 表
* @return
*/
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
Field field = new Field(fieldName, javaType);
CommentTools.addFieldComment(field, introspectedTable);
@@ -41,4 +49,99 @@ public class JavaElementGeneratorTools {
}
return field;
}
/**
* 生成属性
*
* @param fieldName 常量名称
* @param visibility 可见性
* @param javaType 类型
* @param initString 初始化字段
* @param introspectedTable 表
* @return
*/
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
Field field = new Field(fieldName, javaType);
CommentTools.addFieldComment(field, introspectedTable);
field.setVisibility(visibility);
if (initString != null){
field.setInitializationString(initString);
}
return field;
}
/**
* 生成方法
*
* @param methodName 方法名
* @param visibility 可见性
* @param returnType 返回值类型
* @param introspectedTable 表
* @param parameters 参数列表
* @return
*/
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, IntrospectedTable introspectedTable, Parameter ... parameters){
Method method = new Method(methodName);
CommentTools.addMethodComment(method, introspectedTable);
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
* @param introspectedTable 表
* @return
*/
public static Method generateSetterMethod(Field field, IntrospectedTable introspectedTable){
Method method = generateMethod(
"set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
null,
introspectedTable,
new Parameter(field.getType(), field.getName())
);
return generateMethodBody(method, "this." + field.getName() + " = " + field.getName() + ";");
}
/**
* 生成Filed的Get方法
*
* @param field field
* @param introspectedTable 表
* @return
*/
public static Method generateGetterMethod(Field field, IntrospectedTable introspectedTable){
Method method = generateMethod(
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
field.getType(),
introspectedTable
);
return generateMethodBody(method, "return this." + field.getName() + ";");
}
}
@@ -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;