Commit 18d70f25 authored by hewei's avatar hewei

一些代码精简

parent c062929a
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools; 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.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
...@@ -94,27 +95,30 @@ public class BatchInsertPlugin extends PluginAdapter { ...@@ -94,27 +95,30 @@ public class BatchInsertPlugin extends PluginAdapter {
@Override @Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 1. batchInsert // 1. batchInsert
Method mBatchInsert = new Method(METHOD_BATCH_INSERT); FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance();
mBatchInsert.setReturnType(FullyQualifiedJavaType.getIntInstance()); listType.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
// 添加参数 Method mBatchInsert = JavaElementGeneratorTools.generateMethod(
FullyQualifiedJavaType tList = FullyQualifiedJavaType.getNewListInstance(); METHOD_BATCH_INSERT,
tList.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass()); JavaVisibility.DEFAULT,
mBatchInsert.addParameter(new Parameter(tList, "list", "@Param(\"list\")")); FullyQualifiedJavaType.getIntInstance(),
// 添加方法说明 introspectedTable,
CommentTools.addMethodComment(mBatchInsert, introspectedTable); new Parameter(listType, "list", "@Param(\"list\")")
);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mBatchInsert); interfaze.addMethod(mBatchInsert);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。"); logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
// 2. batchInsertSelective // 2. batchInsertSelective
Method mBatchInsertSelective = new Method(METHOD_BATCH_INSERT_SELECTIVE); FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
mBatchInsertSelective.setReturnType(FullyQualifiedJavaType.getIntInstance()); Method mBatchInsertSelective = JavaElementGeneratorTools.generateMethod(
// 添加参数 METHOD_BATCH_INSERT_SELECTIVE,
FullyQualifiedJavaType tSelective = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME); JavaVisibility.DEFAULT,
mBatchInsertSelective.addParameter(new Parameter(tList, "list", "@Param(\"list\")")); FullyQualifiedJavaType.getIntInstance(),
mBatchInsertSelective.addParameter(new Parameter(tSelective, "selective", "@Param(\"selective\")", true)); introspectedTable,
// 添加方法说明 new Parameter(listType, "list", "@Param(\"list\")"),
CommentTools.addMethodComment(mBatchInsertSelective, introspectedTable); new Parameter(selectiveType, "selective", "@Param(\"selective\")", true)
);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mBatchInsertSelective); interfaze.addMethod(mBatchInsertSelective);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。"); logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。");
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package com.itfsw.mybatis.generator.plugins; 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.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
...@@ -69,89 +69,79 @@ public class LimitPlugin extends PluginAdapter { ...@@ -69,89 +69,79 @@ public class LimitPlugin extends PluginAdapter {
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();
// 添加offset和rows字段 // 添加offset和rows字段
Field offset = new Field(); Field offsetField = JavaElementGeneratorTools.generateField(
offset.setName("offset"); "offset",
offset.setVisibility(JavaVisibility.PROTECTED); JavaVisibility.PROTECTED,
offset.setType(integerWrapper); integerWrapper,
CommentTools.addFieldComment(offset, introspectedTable); null,
topLevelClass.addField(offset); introspectedTable
);
Field rows = new Field(); topLevelClass.addField(offsetField);
rows.setName("rows");
rows.setVisibility(JavaVisibility.PROTECTED); Field rowsField = JavaElementGeneratorTools.generateField(
rows.setType(integerWrapper); "rows",
CommentTools.addFieldComment(rows, introspectedTable); JavaVisibility.PROTECTED,
topLevelClass.addField(rows); integerWrapper,
null,
introspectedTable
);
topLevelClass.addField(rowsField);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows字段"); logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows字段");
// 增加getter && setter 方法 // 增加getter && setter 方法
Method setOffset = new Method(); topLevelClass.addMethod(JavaElementGeneratorTools.generateSetterMethod(offsetField, introspectedTable));
setOffset.setVisibility(JavaVisibility.PUBLIC); topLevelClass.addMethod(JavaElementGeneratorTools.generateGetterMethod(offsetField, introspectedTable));
setOffset.setName("setOffset");
setOffset.addParameter(new Parameter(integerWrapper, "offset")); topLevelClass.addMethod(JavaElementGeneratorTools.generateSetterMethod(rowsField, introspectedTable));
setOffset.addBodyLine("this.offset = offset;"); topLevelClass.addMethod(JavaElementGeneratorTools.generateGetterMethod(rowsField, introspectedTable));
CommentTools.addMethodComment(setOffset, introspectedTable);
topLevelClass.addMethod(setOffset);
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);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows的getter和setter实现。"); logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows的getter和setter实现。");
// 提供几个快捷方法 // 提供几个快捷方法
Method setLimit = new Method(); Method setLimit = JavaElementGeneratorTools.generateMethod(
setLimit.setVisibility(JavaVisibility.PUBLIC); "limit",
setLimit.setReturnType(topLevelClass.getType()); JavaVisibility.PUBLIC,
setLimit.setName("limit"); topLevelClass.getType(),
setLimit.addParameter(new Parameter(integerWrapper, "rows")); introspectedTable,
setLimit.addBodyLine("this.rows = rows;"); new Parameter(integerWrapper, "rows")
setLimit.addBodyLine("return this;"); );
CommentTools.addMethodComment(setLimit, introspectedTable); setLimit = JavaElementGeneratorTools.generateMethodBody(
setLimit,
"this.rows = rows;",
"return this;"
);
topLevelClass.addMethod(setLimit); topLevelClass.addMethod(setLimit);
Method setLimit2 = new Method(); Method setLimit2 = JavaElementGeneratorTools.generateMethod(
setLimit2.setVisibility(JavaVisibility.PUBLIC); "limit",
setLimit2.setReturnType(topLevelClass.getType()); JavaVisibility.PUBLIC,
setLimit2.setName("limit"); topLevelClass.getType(),
setLimit2.addParameter(new Parameter(integerWrapper, "offset")); introspectedTable,
setLimit2.addParameter(new Parameter(integerWrapper, "rows")); new Parameter(integerWrapper, "offset"),
setLimit2.addBodyLine("this.offset = offset;"); new Parameter(integerWrapper, "rows")
setLimit2.addBodyLine("this.rows = rows;"); );
setLimit2.addBodyLine("return this;"); setLimit2 = JavaElementGeneratorTools.generateMethodBody(
CommentTools.addMethodComment(setLimit2, introspectedTable); setLimit2,
"this.offset = offset;",
"this.rows = rows;",
"return this;"
);
topLevelClass.addMethod(setLimit2); topLevelClass.addMethod(setLimit2);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加limit方法。"); logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加limit方法。");
Method setPage = new Method(); Method setPage = JavaElementGeneratorTools.generateMethod(
setPage.setVisibility(JavaVisibility.PUBLIC); "page",
setPage.setReturnType(topLevelClass.getType()); JavaVisibility.PUBLIC,
setPage.setName("page"); topLevelClass.getType(),
setPage.addParameter(new Parameter(integerWrapper, "page")); introspectedTable,
setPage.addParameter(new Parameter(integerWrapper, "pageSize")); new Parameter(integerWrapper, "page"),
setPage.addBodyLine("this.offset = page * pageSize;"); new Parameter(integerWrapper, "pageSize")
setPage.addBodyLine("this.rows = pageSize;"); );
setPage.addBodyLine("return this;"); setPage = JavaElementGeneratorTools.generateMethodBody(
CommentTools.addMethodComment(setPage, introspectedTable); setPage,
"this.offset = page * pageSize;",
"this.rows = pageSize;",
"return this;"
);
topLevelClass.addMethod(setPage); topLevelClass.addMethod(setPage);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法"); logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法");
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools; 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 com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.PluginAdapter;
...@@ -42,7 +43,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; ...@@ -42,7 +43,7 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class SelectOneByExamplePlugin extends PluginAdapter { 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); private static final Logger logger = LoggerFactory.getLogger(SelectOneByExamplePlugin.class);
/** /**
...@@ -70,17 +71,13 @@ public class SelectOneByExamplePlugin extends PluginAdapter { ...@@ -70,17 +71,13 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
@Override @Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 方法生成 // 方法生成
Method method = new Method(METHOD_NAME); Method method = JavaElementGeneratorTools.generateMethod(
// 方法可见性 interface会忽略 METHOD_SELECT_ONE_BY_EXAMPLE,
// method.setVisibility(JavaVisibility.PUBLIC); JavaVisibility.DEFAULT,
// 返回值类型 introspectedTable.getRules().calculateAllFieldsClass(),
FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass(); introspectedTable,
method.setReturnType(returnType); new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
// 添加参数 );
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
method.addParameter(new Parameter(type, "example"));
// 添加方法说明
CommentTools.addMethodComment(method, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(method); interfaze.addMethod(method);
...@@ -107,7 +104,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter { ...@@ -107,7 +104,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
CommentTools.addComment(selectOneElement); CommentTools.addComment(selectOneElement);
// 添加ID // 添加ID
selectOneElement.addAttribute(new Attribute("id", METHOD_NAME)); selectOneElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE));
// ----------------------------------------- 表中是否有blob类型字段 --------------------------------------- // ----------------------------------------- 表中是否有blob类型字段 ---------------------------------------
if (introspectedTable.hasBLOBColumns()){ if (introspectedTable.hasBLOBColumns()){
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools; 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 com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.PluginAdapter;
...@@ -48,6 +49,11 @@ public class UpsertPlugin extends 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 = "upsertByExample"; // 方法名
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名 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 public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
private boolean allowMultiQueries = false; // 是否允许多sql提交 private boolean allowMultiQueries = false; // 是否允许多sql提交
...@@ -92,52 +98,52 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -92,52 +98,52 @@ public class UpsertPlugin extends PluginAdapter {
@Override @Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// ====================================== 1. upsert ====================================== // ====================================== 1. upsert ======================================
Method mUpsert = new Method(METHOD_UPSERT); Method mUpsert = JavaElementGeneratorTools.generateMethod(
// 返回值类型 METHOD_UPSERT,
mUpsert.setReturnType(FullyQualifiedJavaType.getIntInstance()); JavaVisibility.DEFAULT,
// 添加参数 FullyQualifiedJavaType.getIntInstance(),
mUpsert.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")); introspectedTable,
// 添加方法说明 new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
CommentTools.addMethodComment(mUpsert, introspectedTable); );
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsert); interfaze.addMethod(mUpsert);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
// ====================================== 2. upsertSelective ====================================== // ====================================== 2. upsertSelective ======================================
Method mUpsertSelective = new Method(METHOD_UPSERT_SELECTIVE); Method mUpsertSelective = JavaElementGeneratorTools.generateMethod(
// 返回值类型 METHOD_UPSERT_SELECTIVE,
mUpsertSelective.setReturnType(FullyQualifiedJavaType.getIntInstance()); JavaVisibility.DEFAULT,
// 添加参数 FullyQualifiedJavaType.getIntInstance(),
mUpsertSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")); introspectedTable,
// 添加方法说明 new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
CommentTools.addMethodComment(mUpsertSelective, introspectedTable); );
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertSelective); interfaze.addMethod(mUpsertSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
if (this.allowMultiQueries){ if (this.allowMultiQueries){
// ====================================== 3. upsertByExample ====================================== // ====================================== 3. upsertByExample ======================================
Method mUpsertByExample = new Method(METHOD_UPSERT_BY_EXAMPLE); Method mUpsertByExample = JavaElementGeneratorTools.generateMethod(
// 返回值类型 METHOD_UPSERT_BY_EXAMPLE,
mUpsertByExample.setReturnType(FullyQualifiedJavaType.getIntInstance()); JavaVisibility.DEFAULT,
// 添加参数 FullyQualifiedJavaType.getIntInstance(),
mUpsertByExample.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")")); introspectedTable,
mUpsertByExample.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")); new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
// 添加方法说明 new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
CommentTools.addMethodComment(mUpsertByExample, introspectedTable); );
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExample); interfaze.addMethod(mUpsertByExample);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
// ====================================== 4. upsertByExampleSelective ====================================== // ====================================== 4. upsertByExampleSelective ======================================
Method mUpsertByExampleSelective = new Method(METHOD_UPSERT_BY_EXAMPLE_SELECTIVE); Method mUpsertByExampleSelective = JavaElementGeneratorTools.generateMethod(
// 返回值类型 METHOD_UPSERT_BY_EXAMPLE_SELECTIVE,
mUpsertByExampleSelective.setReturnType(FullyQualifiedJavaType.getIntInstance()); JavaVisibility.DEFAULT,
// 添加参数 FullyQualifiedJavaType.getIntInstance(),
mUpsertByExampleSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")")); introspectedTable,
mUpsertByExampleSelective.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")); new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
// 添加方法说明 new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
CommentTools.addMethodComment(mUpsertByExampleSelective, introspectedTable); );
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExampleSelective); interfaze.addMethod(mUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
......
...@@ -17,9 +17,7 @@ ...@@ -17,9 +17,7 @@
package com.itfsw.mybatis.generator.plugins.utils; package com.itfsw.mybatis.generator.plugins.utils;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
/** /**
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
...@@ -30,6 +28,16 @@ import org.mybatis.generator.api.dom.java.JavaVisibility; ...@@ -30,6 +28,16 @@ import org.mybatis.generator.api.dom.java.JavaVisibility;
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class JavaElementGeneratorTools { public class JavaElementGeneratorTools {
/**
* 生成静态常量
*
* @param fieldName 常量名称
* @param javaType 类型
* @param initString 初始化字段
* @param introspectedTable 表
* @return
*/
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){ public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
Field field = new Field(fieldName, javaType); Field field = new Field(fieldName, javaType);
CommentTools.addFieldComment(field, introspectedTable); CommentTools.addFieldComment(field, introspectedTable);
...@@ -41,4 +49,99 @@ public class JavaElementGeneratorTools { ...@@ -41,4 +49,99 @@ public class JavaElementGeneratorTools {
} }
return field; 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() + ";");
}
} }
/* /*
* 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.
* * Licensed under the Apache License, Version 2.0 (the "License"); * You may obtain a copy of the License at
* * 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.
* *
* 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.OutputUtilities;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
......
/* /*
* 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.
* * Licensed under the Apache License, Version 2.0 (the "License"); * You may obtain a copy of the License at
* * 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.
* *
* 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.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment