diff --git a/README.md b/README.md
index 73d4cae724d5dcbd0cbbd3cb9cb7dcb42397c9b2..8fc802d57e1174c1300b0bb2cc08f2425d590845 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,3 @@
#### idea 插件简介
-定制化生成代码,根据建表SQL一键生成mapper、model、VO、service、controller,包括以下功能
-1. 基础增删改查、批量新增、列表的查询
-
-
-#### 注意事项
-sql 一定要带上 COMMENT
-
-#### 版本
-1.0 测试版
\ No newline at end of file
+定制化java代码开发插件,详情请在idea中搜索Java Code Helper
diff --git a/build.gradle b/build.gradle
index ca113c0bccc1a3183ad84d602a7eb2f7886cf3a9..ef2f867d72d64854e8af08b3319ae6421ebdf80a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,10 +1,10 @@
plugins {
- id 'org.jetbrains.intellij' version '1.8.0'
+ id 'org.jetbrains.intellij' version '1.9.0'
id 'java'
}
group 'org.example'
-version '1.0-SNAPSHOT'
+version '1.1.0'
repositories {
mavenCentral()
@@ -18,6 +18,7 @@ dependencies {
intellij {
version = '2021.2.3'
+ plugins = ['java']
}
test {
diff --git a/settings.gradle b/settings.gradle
index b021b3f50bf2d4d94355045f27fc4405cba28ab4..40ce7f0a0e7c0be95408c9fc5161b303a9290c79 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,2 +1,2 @@
-rootProject.name = 'reverseEngineering'
+rootProject.name = 'javaCodeHelper'
diff --git a/src/main/java/action/ClassConversionAction.java b/src/main/java/action/ClassConversionAction.java
new file mode 100644
index 0000000000000000000000000000000000000000..a8166a4bebf81bf7984d38a34868c5d4c08f802b
--- /dev/null
+++ b/src/main/java/action/ClassConversionAction.java
@@ -0,0 +1,83 @@
+package action;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.JavaPsiFacade;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiElementFactory;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiJavaFile;
+import com.intellij.psi.PsiMethod;
+import com.intellij.psi.PsiParameter;
+import com.intellij.psi.util.PsiUtilBase;
+import constant.COMMON_CONSTANT;
+import util.StringUtil;
+import util.TypeUtil;
+
+import java.util.Arrays;
+import java.util.stream.Collectors;
+
+public class ClassConversionAction extends AnAction {
+
+ @Override
+ public void actionPerformed(AnActionEvent event) {
+ //获取当前的编辑器对象
+ Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
+ //获取当前项目
+ Project project = event.getData(CommonDataKeys.PROJECT);
+ if (null == project) {
+ return;
+ }
+ PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
+ PsiJavaFile psiJavaFile = (PsiJavaFile) file;
+ if (null == psiJavaFile) {
+ return;
+ }
+ PsiClass psiClass = psiJavaFile.getClasses()[0];
+ if (psiClass.getFields().length == 0) {
+ return;
+ }
+ WriteCommandAction.runWriteCommandAction(project, () -> {
+ String parametersStr = "Object obj";
+ String objectStr = "obj";
+ PsiMethod deleteMethod = null;
+ //获取构造方法
+ PsiMethod[] constructorMethods = psiClass.getConstructors();
+ if (constructorMethods.length > 0) {
+ outCycle:
+ for (PsiMethod psiMethod : constructorMethods) {
+ //获取方法为空的构造方法
+ if (!psiMethod.getText().contains("this.")) {
+ PsiParameter[] parameterArr = psiMethod.getParameterList().getParameters();
+ for (PsiParameter parameter : parameterArr) {
+ if (TypeUtil.isObject(parameter.getType().getCanonicalText())) {
+ parametersStr = Arrays.stream(parameterArr).map(PsiParameter::getText).collect(Collectors.joining(","));
+ objectStr = parameter.getText().split(COMMON_CONSTANT.SPACE)[1];
+ deleteMethod = psiMethod;
+ break outCycle;
+ }
+ }
+ }
+ }
+ }
+ String finalObjectStr = objectStr;
+ PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
+ StringBuilder constructorMethodSb = new StringBuilder("public ").append(psiClass.getName()).append("(").append(parametersStr).append(") {");
+ constructorMethodSb.append(Arrays.stream(psiClass.getFields()).map(f -> "this." + f.getName() + " = " + finalObjectStr + ".get" + StringUtil.toUpperCaseFirst(f.getName()) + "();").collect(Collectors.joining()));
+ constructorMethodSb.append("}");
+ PsiMethod newConstructor = factory.createMethodFromText(constructorMethodSb.toString(), psiClass);
+ if (null != deleteMethod) {
+ psiClass.addAfter(newConstructor, deleteMethod);
+ deleteMethod.delete();
+ } else if (constructorMethods.length > 0) {
+ psiClass.addAfter(newConstructor, psiClass.getConstructors()[0]);
+ } else {
+ psiClass.addBefore(newConstructor, psiClass.getMethods()[0]);
+ }
+ });
+ }
+}
diff --git a/src/main/java/constant/COMMON_CONSTANT.java b/src/main/java/constant/COMMON_CONSTANT.java
index 46d13ff1683b832b6e597a96b0fd5d2d96e88d60..ffbbcadbfc9c23391f7977a608fdf22613e3f281 100644
--- a/src/main/java/constant/COMMON_CONSTANT.java
+++ b/src/main/java/constant/COMMON_CONSTANT.java
@@ -22,13 +22,15 @@ public class COMMON_CONSTANT {
public static final String FAIL = "失败";
public static final String ENCODING = "UTF-8";
public static final String DOUBLE_BACKSLASH = "\\";
+ public static final String SPACE = "\\s+";
public static final String SLASH = "/";
public static final String JAVA_FILE_PATH = "\\src\\main\\java\\";
public static final String MODEL = "Model";
public static final String TEMPLATE_SUFFIX = ".ftl";
public static final String TEMPLATE_PATH = "./templates";
- public static final List TEMPLATE_NAME_LIST = Arrays.asList("Model.java.ftl", "Mapper.java.ftl", "Service.java.ftl", "ServiceImpl.java.ftl",
- "VO.java.ftl", "Controller.java.ftl");
+ public static final List BASIC_TYPE_LIST = Arrays.asList("int,short,long,byte,float,double,boolean、char,Integer,Short,Long,Byte,Float,Double,Boolean,Character".split(","));
+ public static final List COMMON_TYPE_LIST = Arrays.asList("String,Date,Timestamp,BigDecimal".split(","));
+ public static final List TEMPLATE_NAME_LIST = Arrays.asList("Model.java.ftl", "Mapper.java.ftl", "Service.java.ftl", "ServiceImpl.java.ftl", "VO.java.ftl", "Controller.java.ftl");
public static void init(String author, String modelName, String packagePath){
pathMap = new HashMap<>();
diff --git a/src/main/java/dialog/ToolWindowDialog.java b/src/main/java/dialog/ToolWindowDialog.java
index 50e1c770a9f5bb4c87d65d50073bd7a0de4bb09d..80e9d7f90ae4af23fb8393963d23828efe9f0cf3 100644
--- a/src/main/java/dialog/ToolWindowDialog.java
+++ b/src/main/java/dialog/ToolWindowDialog.java
@@ -18,8 +18,13 @@ public class ToolWindowDialog extends JDialog {
setContentPane(contentPane);
setModal(true);
buttonOK.addActionListener(e -> {
- new CreateFileUtil().createFile(authorField.getText(),modelNameField.getText(),packagePathField.getText(),textArea.getText());
- Messages.showMessageDialog(COMMON_CONSTANT.SUCCESS, "", Messages.getInformationIcon());
+ try {
+ new CreateFileUtil().createFile(authorField.getText(),modelNameField.getText(),packagePathField.getText(),textArea.getText());
+ Messages.showMessageDialog(COMMON_CONSTANT.SUCCESS, "", Messages.getInformationIcon());
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ Messages.showMessageDialog(COMMON_CONSTANT.FAIL, "", Messages.getInformationIcon());
+ }
});
}
diff --git a/src/main/java/factory/TemplateFactory.java b/src/main/java/factory/TemplateFactory.java
index 162ddf3f2824e8b0ff2c7faa6bcda337f3da9f29..4e325440cb9d2207ea067d342e1de84745da8ac1 100644
--- a/src/main/java/factory/TemplateFactory.java
+++ b/src/main/java/factory/TemplateFactory.java
@@ -3,9 +3,13 @@ package factory;
import constant.COMMON_CONSTANT;
import freemarker.template.Configuration;
import freemarker.template.Template;
+import freemarker.template.TemplateException;
+import java.io.BufferedWriter;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
@@ -21,7 +25,7 @@ public class TemplateFactory {
private TemplateFactory() {
}
- public static TemplateFactory getInstance() {
+ public static TemplateFactory getInstance() throws IOException {
if (templateFactory == null) {
synchronized (TemplateFactory.class) {
if (templateFactory == null) {
@@ -33,14 +37,9 @@ public class TemplateFactory {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setClassLoaderForTemplateLoading(COMMON_CONSTANT.class.getClassLoader(), COMMON_CONSTANT.TEMPLATE_PATH);
configuration.setDefaultEncoding(COMMON_CONSTANT.ENCODING);
- COMMON_CONSTANT.TEMPLATE_NAME_LIST.forEach(t -> {
- try {
- templateFactory.templateList.add(configuration.getTemplate(t));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- );
+ for(String templateName : COMMON_CONSTANT.TEMPLATE_NAME_LIST){
+ templateFactory.templateList.add(configuration.getTemplate(templateName));
+ }
}
}
}
@@ -51,4 +50,10 @@ public class TemplateFactory {
return templateList;
}
+ public void create(Object dataModel,String fileBasicName) throws IOException, TemplateException {
+ for(Template template : templateFactory.getTemplateList()){
+ String filePath = COMMON_CONSTANT.FULL_PATH + fileBasicName + template.getName().replaceAll(COMMON_CONSTANT.TEMPLATE_SUFFIX,"").replaceAll(COMMON_CONSTANT.MODEL,"");
+ template.process(dataModel, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath))));
+ }
+ }
}
diff --git a/src/main/java/pojo/ColumnInfo.java b/src/main/java/pojo/ColumnInfo.java
index b797b73a3b50456749932e8faaa37ad46bd5033b..8b36c3fcbe89f4abdbb4417c6a73bd371a9fc7a5 100644
--- a/src/main/java/pojo/ColumnInfo.java
+++ b/src/main/java/pojo/ColumnInfo.java
@@ -1,7 +1,7 @@
package pojo;
import util.StringUtil;
-import util.TypeConversionUtil;
+import util.TypeUtil;
import java.util.Arrays;
import java.util.List;
@@ -31,7 +31,7 @@ public class ColumnInfo {
this.sqlColumnName = valueList.get(0);
this.firstUpperColumnName = Arrays.stream(this.sqlColumnName.split("_")).map(StringUtil::toUpperCaseFirst).collect(Collectors.joining());
this.columnName = StringUtil.toLowerCaseFirst(this.firstUpperColumnName);
- this.columnType = TypeConversionUtil.conversion(valueList.get(1));
+ this.columnType = TypeUtil.toJavaType(valueList.get(1));
this.columnComment = valueList.get(valueList.size() - 1);
}
diff --git a/src/main/java/pojo/TableInfo.java b/src/main/java/pojo/TableInfo.java
index 47910874711010e44e9494fb6b80ec9ecbd8f938..7bcb5841ca63e6dc26875a27e1b8dd120798ce17 100644
--- a/src/main/java/pojo/TableInfo.java
+++ b/src/main/java/pojo/TableInfo.java
@@ -32,7 +32,7 @@ public class TableInfo {
public TableInfo(String createTableSql) {
List lineList = List.of(createTableSql.split("\\r?\\n"));
- this.sqlTableName = lineList.get(0).split("\\s+")[2];
+ this.sqlTableName = lineList.get(0).split(COMMON_CONSTANT.SPACE)[2];
if (this.sqlTableName.contains(".")) {
this.sqlTableName = this.sqlTableName.split("\\.")[1].replaceAll("['`]", "");
}
@@ -89,8 +89,4 @@ public class TableInfo {
Gson gs = new Gson();
return gs.fromJson(gs.toJson(this), Map.class);
}
-
- public String getFilePath(String templateFileName) {
- return COMMON_CONSTANT.FULL_PATH + this.tableName + templateFileName.replaceAll(COMMON_CONSTANT.TEMPLATE_SUFFIX,"").replaceAll(COMMON_CONSTANT.MODEL,"");
- }
}
diff --git a/src/main/java/util/CreateFileUtil.java b/src/main/java/util/CreateFileUtil.java
index 18607803f01f26d7a4f5a0c34d3ebfb8e30d9ff8..cc13b5e859d97a75f699e65b780e5796b471424e 100644
--- a/src/main/java/util/CreateFileUtil.java
+++ b/src/main/java/util/CreateFileUtil.java
@@ -5,10 +5,7 @@ import factory.TemplateFactory;
import freemarker.template.TemplateException;
import pojo.TableInfo;
-import java.io.BufferedWriter;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.OutputStreamWriter;
import java.util.Map;
/**
@@ -18,7 +15,7 @@ import java.util.Map;
public class CreateFileUtil {
- public void createFile(String author, String modelName, String packagePath, String createTableSql) {
+ public void createFile(String author, String modelName, String packagePath, String createTableSql) throws IOException, TemplateException {
//初始化路径
COMMON_CONSTANT.init(author, modelName, packagePath);
//解析Sql
@@ -27,12 +24,6 @@ public class CreateFileUtil {
dataMap.putAll(COMMON_CONSTANT.pathMap);
//生成文件
TemplateFactory templateFactory = TemplateFactory.getInstance();
- templateFactory.getTemplateList().forEach(t -> {
- try {
- t.process(dataMap, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tableInfo.getFilePath(t.getName())))));
- } catch (TemplateException | IOException e) {
- e.printStackTrace();
- }
- });
+ templateFactory.create(dataMap,tableInfo.getTableName());
}
}
diff --git a/src/main/java/util/TypeConversionUtil.java b/src/main/java/util/TypeUtil.java
similarity index 57%
rename from src/main/java/util/TypeConversionUtil.java
rename to src/main/java/util/TypeUtil.java
index 192dcdaa1df801a63f451c6711655aa15515997e..77007be16fe532848e1429f24e4cee730a07c54a 100644
--- a/src/main/java/util/TypeConversionUtil.java
+++ b/src/main/java/util/TypeUtil.java
@@ -1,12 +1,14 @@
package util;
+import constant.COMMON_CONSTANT;
+
/**
* @Author zhanglinfeng
* @Date create in 2022/9/8 17:37
*/
-public class TypeConversionUtil {
+public class TypeUtil {
- public static String conversion(String sqlType) {
+ public static String toJavaType(String sqlType) {
if (sqlType.contains("int")) {
return "Integer";
} else if (sqlType.contains("timestamp")) {
@@ -18,4 +20,8 @@ public class TypeConversionUtil {
}
}
+ public static boolean isObject(String type){
+ return !COMMON_CONSTANT.BASIC_TYPE_LIST.contains(type) && !COMMON_CONSTANT.COMMON_TYPE_LIST.contains(type);
+ }
+
}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 7ae7fe3a688c13d420fd67999295e01bf0040dab..cf00fcb315f68fbaebb97b6ddd4023a8045fe281 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -1,21 +1,38 @@
- com.zlf.sql-reverse.plugin
- SqlReverse
- 1.0
- zhanglinfeng
+ com.zlf.java-code-helper.plugin
+ Java Code Helper
+ 1.1.0
+ zhanglinfeng
- 1.基础增删改查、列表的查询
+ 定制化java代码开发插件,邮箱704123055@qq.com
+ 根据建表SQL(带上COMMENT)一键生成mapper、model、VO、service、controller代码。支持基础增删查改、列表查询、批量新增
+ 一键生成类转换的构造参数,例:Object1 to Object2
]]>
1.0.0
+
+ 1.1.0
+
+ - 一键生成类转换的构造参数,例:Object1 to Object2
+
]]>
com.intellij.modules.platform
+ com.intellij.modules.lang
+ com.intellij.modules.java
+
+
+
+
+
+
\ No newline at end of file