diff --git a/README.md b/README.md index b84924f8..1ef176bc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ mybatis-generator-gui mybatis-generator-gui是基于[mybatis generator](http://www.mybatis.org/generator/index.html)开发一款界面工具, 本工具可以使你非常容易及快速生成Mybatis的Java POJO文件及数据库Mapping文件。 -![image](https://user-images.githubusercontent.com/3505708/38157772-8cd08a7e-34bc-11e8-948b-bccbfa59848f.png) +![image](https://raw.githubusercontent.com/slankka/mybatis-generator-gui/slankka-pic/mybatisGeneratorUI_20180603025329.png) ### 核心特性 * 按照界面步骤轻松生成代码,省去XML繁琐的学习与配置过程 diff --git a/src/main/java/com/zzg/mybatis/generator/MainUI.java b/src/main/java/com/zzg/mybatis/generator/MainUI.java index 3bd63565..19ac9f1b 100644 --- a/src/main/java/com/zzg/mybatis/generator/MainUI.java +++ b/src/main/java/com/zzg/mybatis/generator/MainUI.java @@ -6,7 +6,6 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; -import javafx.scene.image.Image; import javafx.stage.Stage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java b/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java index aabdce73..8ae98841 100644 --- a/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java +++ b/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java @@ -3,6 +3,7 @@ import com.zzg.mybatis.generator.model.DatabaseConfig; import com.zzg.mybatis.generator.model.DbType; import com.zzg.mybatis.generator.model.GeneratorConfig; +import com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin; import com.zzg.mybatis.generator.plugins.DbRemarksCommentGenerator; import com.zzg.mybatis.generator.util.ConfigHelper; import com.zzg.mybatis.generator.util.DbUtil; @@ -64,7 +65,7 @@ public void generate() throws Exception { TableConfiguration tableConfig = new TableConfiguration(context); tableConfig.setTableName(generatorConfig.getTableName()); tableConfig.setDomainObjectName(generatorConfig.getDomainObjectName()); - if(!generatorConfig.isUseExampe()) { + if(!generatorConfig.isUseExample()) { tableConfig.setUpdateByExampleStatementEnabled(false); tableConfig.setCountByExampleStatementEnabled(false); tableConfig.setDeleteByExampleStatementEnabled(false); @@ -93,9 +94,23 @@ public void generate() throws Exception { //添加GeneratedKey主键生成 if (StringUtils.isNoneEmpty(generatorConfig.getGenerateKeys())) { - tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), selectedDatabaseConfig.getDbType(), true, null)); + String dbType = selectedDatabaseConfig.getDbType(); + if (DbType.MySQL.name().equals(dbType)) { + dbType = "JDBC"; + //dbType为JDBC,且配置中开启useGeneratedKeys时,Mybatis会使用Jdbc3KeyGenerator, + //使用该KeyGenerator的好处就是直接在一次INSERT 语句内,通过resultSet获取得到 生成的主键值, + //并很好的支持设置了读写分离代理的数据库 + //例如阿里云RDS + 读写分离代理 + //无需指定主库 + //当使用SelectKey时,Mybatis会使用SelectKeyGenerator,INSERT之后,多发送一次查询语句,获得主键值 + //在上述读写分离被代理的情况下,会得不到正确的主键 + } + tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType, true, null)); } + if (generatorConfig.getMapperName() != null) { + tableConfig.setMapperName(generatorConfig.getMapperName()); + } // add ignore columns if (ignoredColumns != null) { ignoredColumns.stream().forEach(ignoredColumn -> { @@ -110,11 +125,11 @@ public void generate() throws Exception { if (generatorConfig.isUseActualColumnNames()) { tableConfig.addProperty("useActualColumnNames", "true"); } - + if(generatorConfig.isUseTableNameAlias()){ tableConfig.setAlias(generatorConfig.getTableName()); } - + JDBCConnectionConfiguration jdbcConfig = new JDBCConnectionConfiguration(); // http://www.mybatis.org/generator/usage/mysql.html if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())) { @@ -158,7 +173,7 @@ public void generate() throws Exception { context.setCommentGeneratorConfiguration(commentConfig); // set java file encoding context.addProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING, generatorConfig.getEncoding()); - + //实体添加序列化 PluginConfiguration serializablePluginConfiguration = new PluginConfiguration(); serializablePluginConfiguration.addProperty("type", "org.mybatis.generator.plugins.SerializablePlugin"); @@ -191,6 +206,36 @@ public void generate() throws Exception { javaTypeResolverConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.JavaTypeResolverJsr310Impl"); context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration); } + //forUpdate 插件 + if(generatorConfig.isNeedForUpdate()) { + if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType()) + || DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) { + PluginConfiguration pluginConfiguration = new PluginConfiguration(); + pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.MySQLForUpdatePlugin"); + pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.MySQLForUpdatePlugin"); + context.addPluginConfiguration(pluginConfiguration); + } + } + //repository 插件 + if(generatorConfig.isAnnotationDAO()) { + if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType()) + || DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) { + PluginConfiguration pluginConfiguration = new PluginConfiguration(); + pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.RepositoryPlugin"); + pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.RepositoryPlugin"); + context.addPluginConfiguration(pluginConfiguration); + } + } + if (generatorConfig.isUseDAOExtendStyle()) { + if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType()) + || DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) { + PluginConfiguration pluginConfiguration = new PluginConfiguration(); + pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin"); + pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin"); + context.addPluginConfiguration(pluginConfiguration); + } + } + context.setTargetRuntime("MyBatis3"); List warnings = new ArrayList<>(); @@ -206,10 +251,10 @@ public void generate() throws Exception { mappingXMLFile.delete(); } } - + myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables); } - + private String getMappingXMLFilePath(GeneratorConfig generatorConfig) { StringBuilder sb = new StringBuilder(); sb.append(generatorConfig.getProjectFolder()).append("/"); @@ -218,7 +263,12 @@ private String getMappingXMLFilePath(GeneratorConfig generatorConfig) { if (StringUtils.isNotEmpty(mappingXMLPackage)) { sb.append(mappingXMLPackage.replace(".", "/")).append("/"); } - sb.append(generatorConfig.getDomainObjectName()).append("Mapper.xml"); + if (generatorConfig.getMapperName() != null) { + sb.append(generatorConfig.getMapperName()).append(".xml"); + }else { + sb.append(generatorConfig.getDomainObjectName()).append("Mapper.xml"); + } + return sb.toString(); } diff --git a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java index 8b3fe6ce..481006ff 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -13,6 +13,9 @@ import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.*; +import javafx.scene.control.Label; +import javafx.scene.control.MenuItem; +import javafx.scene.control.TextField; import javafx.scene.control.cell.TextFieldTreeCell; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; @@ -27,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.awt.*; import java.io.File; import java.net.URL; import java.sql.SQLRecoverableException; @@ -63,6 +67,8 @@ public class MainUIController extends BaseFXController { @FXML private TextField daoTargetProject; @FXML + private TextField mapperName; + @FXML private TextField projectFolderField; @FXML private CheckBox offsetLimitCheckBox; @@ -73,6 +79,10 @@ public class MainUIController extends BaseFXController { @FXML private CheckBox needToStringHashcodeEquals; @FXML + private CheckBox forUpdateCheckBox; + @FXML + private CheckBox annotationDAOCheckBox; + @FXML private CheckBox useTableNameAliasCheckbox; @FXML private CheckBox annotationCheckBox; @@ -81,6 +91,8 @@ public class MainUIController extends BaseFXController { @FXML private CheckBox useExample; @FXML + private CheckBox useDAOExtendStyle; + @FXML private CheckBox useSchemaPrefix; @FXML private CheckBox jsr310Support; @@ -154,6 +166,9 @@ public void initialize(URL location, ResourceBundle resources) { cell.setContextMenu(contextMenu); } if (event.getClickCount() == 2) { + if(treeItem == null) { + return ; + } treeItem.setExpanded(true); if (level == 1) { System.out.println("index: " + leftDBTree.getSelectionModel().getSelectedIndex()); @@ -186,6 +201,7 @@ public void initialize(URL location, ResourceBundle resources) { this.tableName = tableName; tableNameField.setText(tableName); domainObjectNameField.setText(MyStringUtils.dbStringToCamelStyle(tableName)); + mapperName.setText(domainObjectNameField.getText().concat("DAO")); } } }); @@ -196,7 +212,7 @@ public void initialize(URL location, ResourceBundle resources) { //默认选中第一个,否则如果忘记选择,没有对应错误提示 encodingChoice.getSelectionModel().selectFirst(); } - + private void setTooltip() { encodingChoice.setTooltip(new Tooltip("生成文件的编码,必选")); generateKeysField.setTooltip(new Tooltip("insert时可以返回主键ID")); @@ -205,6 +221,8 @@ private void setTooltip() { useActualColumnNamesCheckbox.setTooltip(new Tooltip("是否使用数据库实际的列名作为实体类域的名称")); useTableNameAliasCheckbox.setTooltip(new Tooltip("在Mapper XML文件中表名使用别名,并且列全部使用as查询")); overrideXML.setTooltip(new Tooltip("重新生成时把原XML文件覆盖,否则是追加")); + useDAOExtendStyle.setTooltip(new Tooltip("将通用接口方法放在公共接口中,DAO接口留空")); + forUpdateCheckBox.setTooltip(new Tooltip("在Select语句中增加for update后缀")); } void loadLeftDBTree() { @@ -315,6 +333,7 @@ public GeneratorConfig getGeneratorConfigFromUI() { generatorConfig.setModelPackageTargetFolder(modelTargetProject.getText()); generatorConfig.setDaoPackage(daoTargetPackage.getText()); generatorConfig.setDaoTargetFolder(daoTargetProject.getText()); + generatorConfig.setMapperName(mapperName.getText()); generatorConfig.setMappingXMLPackage(mapperTargetPackage.getText()); generatorConfig.setMappingXMLTargetFolder(mappingTargetProject.getText()); generatorConfig.setTableName(tableNameField.getText()); @@ -324,10 +343,13 @@ public GeneratorConfig getGeneratorConfigFromUI() { generatorConfig.setOverrideXML(overrideXML.isSelected()); generatorConfig.setNeedToStringHashcodeEquals(needToStringHashcodeEquals.isSelected()); generatorConfig.setUseTableNameAlias(useTableNameAliasCheckbox.isSelected()); + generatorConfig.setNeedForUpdate(forUpdateCheckBox.isSelected()); + generatorConfig.setAnnotationDAO(annotationDAOCheckBox.isSelected()); generatorConfig.setAnnotation(annotationCheckBox.isSelected()); generatorConfig.setUseActualColumnNames(useActualColumnNamesCheckbox.isSelected()); generatorConfig.setEncoding(encodingChoice.getValue()); - generatorConfig.setUseExampe(useExample.isSelected()); + generatorConfig.setUseExample(useExample.isSelected()); + generatorConfig.setUseDAOExtendStyle(useDAOExtendStyle.isSelected()); generatorConfig.setUseSchemaPrefix(useSchemaPrefix.isSelected()); generatorConfig.setJsr310Support(jsr310Support.isSelected()); return generatorConfig; @@ -415,4 +437,15 @@ private boolean checkDirs(GeneratorConfig config) { return true; } + @FXML + public void openTargetFolder() { + GeneratorConfig generatorConfig = getGeneratorConfigFromUI(); + String projectFolder = generatorConfig.getProjectFolder(); + try { + Desktop.getDesktop().browse(new File(projectFolder).toURI()); + }catch (Exception e) { + AlertUtil.showErrorAlert("打开目录失败,请检查目录是否填写正确" + e.getMessage()); + } + + } } diff --git a/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java b/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java index 0b80be52..3e68df4b 100644 --- a/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java +++ b/src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java @@ -39,49 +39,55 @@ public class GeneratorConfig { private boolean offsetLimit; private boolean comment; - + private boolean overrideXML; private boolean needToStringHashcodeEquals; + private boolean needForUpdate; + + private boolean annotationDAO; + private boolean annotation; private boolean useActualColumnNames; - private boolean useExampe; + private boolean useExample; private String generateKeys; private String encoding; - + private boolean useTableNameAlias; - private boolean useSchemaPrefix; + private boolean useDAOExtendStyle; - private boolean jsr310Support; + private boolean useSchemaPrefix; - public boolean isJsr310Support() { - return jsr310Support; - } + private boolean jsr310Support; - public void setJsr310Support(boolean jsr310Support) { - this.jsr310Support = jsr310Support; - } + public boolean isJsr310Support() { + return jsr310Support; + } - public boolean isUseSchemaPrefix() { - return useSchemaPrefix; - } + public void setJsr310Support(boolean jsr310Support) { + this.jsr310Support = jsr310Support; + } - public void setUseSchemaPrefix(boolean useSchemaPrefix) { - this.useSchemaPrefix = useSchemaPrefix; - } + public boolean isUseSchemaPrefix() { + return useSchemaPrefix; + } + + public void setUseSchemaPrefix(boolean useSchemaPrefix) { + this.useSchemaPrefix = useSchemaPrefix; + } - public boolean isUseExampe() { - return useExampe; + public boolean isUseExample() { + return useExample; } - public void setUseExampe(boolean useExampe) { - this.useExampe = useExampe; + public void setUseExample(boolean useExample) { + this.useExample = useExample; } public String getName() { @@ -196,7 +202,23 @@ public void setNeedToStringHashcodeEquals(boolean needToStringHashcodeEquals) { this.needToStringHashcodeEquals = needToStringHashcodeEquals; } - public boolean isAnnotation() { + public boolean isNeedForUpdate() { + return needForUpdate; + } + + public void setNeedForUpdate(boolean needForUpdate) { + this.needForUpdate = needForUpdate; + } + + public boolean isAnnotationDAO() { + return annotationDAO; + } + + public void setAnnotationDAO(boolean annotationDAO) { + this.annotationDAO = annotationDAO; + } + + public boolean isAnnotation() { return annotation; } @@ -212,6 +234,14 @@ public void setUseActualColumnNames(boolean useActualColumnNames) { this.useActualColumnNames = useActualColumnNames; } + public String getMapperName() { + return mapperName; + } + + public void setMapperName(String mapperName) { + this.mapperName = mapperName; + } + public String getGenerateKeys() { return generateKeys; } @@ -227,7 +257,7 @@ public String getEncoding() { public void setEncoding(String encoding) { this.encoding = encoding; } - + public boolean getUseTableNameAlias() { return useTableNameAlias; } @@ -239,7 +269,7 @@ public void setUseTableNameAlias(boolean useTableNameAlias) { public boolean isUseTableNameAlias() { return useTableNameAlias; } - + public boolean isOverrideXML() { return overrideXML; } @@ -247,4 +277,12 @@ public boolean isOverrideXML() { public void setOverrideXML(boolean overrideXML) { this.overrideXML = overrideXML; } + + public void setUseDAOExtendStyle(boolean useDAOExtendStyle) { + this.useDAOExtendStyle = useDAOExtendStyle; + } + + public boolean isUseDAOExtendStyle() { + return useDAOExtendStyle; + } } diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/CommonDAOInterfacePlugin.java b/src/main/java/com/zzg/mybatis/generator/plugins/CommonDAOInterfacePlugin.java new file mode 100644 index 00000000..5e6508a0 --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/plugins/CommonDAOInterfacePlugin.java @@ -0,0 +1,284 @@ +package com.zzg.mybatis.generator.plugins; + +import org.mybatis.generator.api.*; +import org.mybatis.generator.api.dom.java.*; +import org.mybatis.generator.exception.ShellException; +import org.mybatis.generator.internal.DefaultShellCallback; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; + +/** + * Project: mybatis-generator-gui + * + * @author slankka on 2018/3/11. + */ +public class CommonDAOInterfacePlugin extends PluginAdapter { + + private static final String DEFAULT_DAO_SUPER_CLASS = ".MyBatisBaseDao"; + private static final FullyQualifiedJavaType PARAM_ANNOTATION_TYPE = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"); + private static final FullyQualifiedJavaType LIST_TYPE = FullyQualifiedJavaType.getNewListInstance(); + private static final FullyQualifiedJavaType SERIALIZEBLE_TYPE = new FullyQualifiedJavaType("java.io.Serializable"); + + private List methods = new ArrayList<>(); + + // 扩展 + private String expandDaoSuperClass; + + private ShellCallback shellCallback = null; + + public CommonDAOInterfacePlugin() { + shellCallback = new DefaultShellCallback(false); + } + + @Override + public List contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) { + + JavaFormatter javaFormatter = context.getJavaFormatter(); + String daoTargetDir = context.getJavaClientGeneratorConfiguration().getTargetProject(); + String daoTargetPackage = context.getJavaClientGeneratorConfiguration().getTargetPackage(); + List mapperJavaFiles = new ArrayList(); + String javaFileEncoding = context.getProperty("javaFileEncoding"); + Interface mapperInterface = new Interface(daoTargetPackage + DEFAULT_DAO_SUPER_CLASS); + GeneratedJavaFile mapperJavafile = null; + + if (stringHasValue(daoTargetPackage)) { + mapperInterface.addImportedType(PARAM_ANNOTATION_TYPE); + mapperInterface.addImportedType(LIST_TYPE); + mapperInterface.addImportedType(SERIALIZEBLE_TYPE); + + mapperInterface.setVisibility(JavaVisibility.PUBLIC); + mapperInterface.addJavaDocLine("/**"); + mapperInterface.addJavaDocLine(" * " + "DAO公共基类,由MybatisGenerator自动生成请勿修改"); + mapperInterface.addJavaDocLine(" * " + "@param The Model Class"); + mapperInterface.addJavaDocLine(" * " + "@param The Primary Key Class"); + mapperInterface.addJavaDocLine(" * " + "@param The Example Class"); + mapperInterface.addJavaDocLine(" */"); + + FullyQualifiedJavaType daoBaseInterfaceJavaType = mapperInterface.getType(); + daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("Model")); + daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("PK extends Serializable")); + daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("E")); + + if (!this.methods.isEmpty()) { + + for (Method method : methods) { + mapperInterface.addMethod(method); + } + + } + + List generatedJavaFiles = introspectedTable.getGeneratedJavaFiles(); + for (GeneratedJavaFile generatedJavaFile : generatedJavaFiles) { + CompilationUnit compilationUnit = generatedJavaFile.getCompilationUnit(); + FullyQualifiedJavaType type = compilationUnit.getType(); + String modelName = type.getShortName(); + if (modelName.endsWith("DAO")) { + } + } + mapperJavafile = new GeneratedJavaFile(mapperInterface, daoTargetDir, javaFileEncoding, javaFormatter); + try { + File mapperDir = shellCallback.getDirectory(daoTargetDir, daoTargetPackage); + File mapperFile = new File(mapperDir, mapperJavafile.getFileName()); + // 文件不存在 + if (!mapperFile.exists()) { + mapperJavaFiles.add(mapperJavafile); + } + } catch (ShellException e) { + e.printStackTrace(); + } + } + return mapperJavaFiles; + } + + @Override + public boolean clientGenerated(Interface interfaze, + TopLevelClass topLevelClass, + IntrospectedTable introspectedTable) { + interfaze.addJavaDocLine("/**"); + interfaze.addJavaDocLine(" * " + interfaze.getType().getShortName() + "继承基类"); + interfaze.addJavaDocLine(" */"); + + String daoSuperClass = interfaze.getType().getPackageName() + DEFAULT_DAO_SUPER_CLASS; + FullyQualifiedJavaType daoSuperType = new FullyQualifiedJavaType(daoSuperClass); + + String targetPackage = introspectedTable.getContext().getJavaModelGeneratorConfiguration().getTargetPackage(); + + String domainObjectName = introspectedTable.getTableConfiguration().getDomainObjectName(); + FullyQualifiedJavaType baseModelJavaType = new FullyQualifiedJavaType(targetPackage + "." + domainObjectName); + daoSuperType.addTypeArgument(baseModelJavaType); + + FullyQualifiedJavaType primaryKeyTypeJavaType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType(); + daoSuperType.addTypeArgument(primaryKeyTypeJavaType); + + String exampleType = introspectedTable.getExampleType(); + FullyQualifiedJavaType exampleTypeJavaType = new FullyQualifiedJavaType(exampleType); + daoSuperType.addTypeArgument(exampleTypeJavaType); + + interfaze.addImportedType(primaryKeyTypeJavaType); + interfaze.addImportedType(exampleTypeJavaType); + interfaze.addImportedType(baseModelJavaType); + interfaze.addImportedType(daoSuperType); + interfaze.addSuperInterface(daoSuperType); + return true; + } + + @Override + public boolean validate(List list) { + return true; + } + + private void interceptExampleParam(Method method) { + method.getParameters().clear(); + method.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "example")); + methods.add(method); + } + + private void interceptPrimaryKeyParam(Method method) { + method.getParameters().clear(); + method.addParameter(new Parameter(new FullyQualifiedJavaType("PK"), "id")); + methods.add(method); + } + + private void interceptModelParam(Method method) { + method.getParameters().clear(); + method.addParameter(new Parameter(new FullyQualifiedJavaType("Model"), "record")); + methods.add(method); + } + + private void interceptModelAndExampleParam(Method method) { + List parameters = method.getParameters(); + if (parameters.size() == 1) { + interceptExampleParam(method); + }else{ + method.getParameters().clear(); + Parameter parameter1 = new Parameter(new FullyQualifiedJavaType("Model"), "record"); + parameter1.addAnnotation("@Param(\"record\")"); + method.addParameter(parameter1); + + Parameter parameter2 = new Parameter(new FullyQualifiedJavaType("E"), "example"); + parameter2.addAnnotation("@Param(\"example\")"); + method.addParameter(parameter2); + methods.add(method); + } + } + + @Override + public boolean clientCountByExampleMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { +// interfaze + interceptExampleParam(method); + return false; + } + + + @Override + public boolean clientDeleteByExampleMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptExampleParam(method); + return false; + } + + + @Override + public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptPrimaryKeyParam(method); + return false; + } + + @Override + public boolean clientInsertMethodGenerated(Method method, Interface interfaze, + IntrospectedTable introspectedTable) { + interceptModelParam(method); + return false; + } + + @Override + public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptExampleParam(method); + method.setReturnType(new FullyQualifiedJavaType("List")); + return false; + } + + @Override + public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptExampleParam(method); + method.setReturnType(new FullyQualifiedJavaType("List")); + return false; + } + + @Override + public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptPrimaryKeyParam(method); + method.setReturnType(new FullyQualifiedJavaType("Model")); + return false; + } + + @Override + public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelAndExampleParam(method); + return false; + } + + @Override + public boolean clientUpdateByExampleWithBLOBsMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelAndExampleParam(method); + return false; + } + + @Override + public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelAndExampleParam(method); + return false; + } + + @Override + public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelParam(method); + return false; + } + + @Override + public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + interceptModelAndExampleParam(method); + return false; + } + + @Override + public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + interceptModelAndExampleParam(method); + return false; + } + + @Override + public boolean clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(Method method, + Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelParam(method); + return false; + } + + @Override + public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated( + Method method, Interface interfaze, + IntrospectedTable introspectedTable) { + interceptModelParam(method); + return false; + } + + @Override + public boolean clientInsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { + interceptModelParam(method); + return false; + } +} diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/MySQLForUpdatePlugin.java b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLForUpdatePlugin.java new file mode 100644 index 00000000..ef06949c --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLForUpdatePlugin.java @@ -0,0 +1,70 @@ +package com.zzg.mybatis.generator.plugins; + +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 java.util.List; + +/** + * Project: mybatis-generator-gui + * + * @author slankka on 2017/11/4. + */ +public class MySQLForUpdatePlugin extends PluginAdapter { + + @Override + public boolean validate(List warnings) { + return true; + } + + @Override + public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + + PrimitiveTypeWrapper booleanWrapper = FullyQualifiedJavaType.getBooleanPrimitiveInstance().getPrimitiveTypeWrapper(); + Field forUpdate = new Field(); + forUpdate.setName("forUpdate"); + forUpdate.setVisibility(JavaVisibility.PRIVATE); + forUpdate.setType(booleanWrapper); + topLevelClass.addField(forUpdate); + + Method setForUpdate = new Method(); + setForUpdate.setVisibility(JavaVisibility.PUBLIC); + setForUpdate.setName("setForUpdate"); + setForUpdate.addParameter(new Parameter(booleanWrapper, "forUpdate")); + setForUpdate.addBodyLine("this.forUpdate = forUpdate;"); + topLevelClass.addMethod(setForUpdate); + + Method getForUpdate = new Method(); + getForUpdate.setVisibility(JavaVisibility.PUBLIC); + getForUpdate.setReturnType(booleanWrapper); + getForUpdate.setName("getForUpdate"); + getForUpdate.addBodyLine("return forUpdate;"); + topLevelClass.addMethod(getForUpdate); + + return true; + } + + private void appendForUpdate(XmlElement element, IntrospectedTable introspectedTable) { + XmlElement forUpdateElement = new XmlElement("if"); + forUpdateElement.addAttribute(new Attribute("test", "forUpdate != null and forUpdate == true")); + forUpdateElement.addElement(new TextElement("for update")); + element.addElement(forUpdateElement); + } + + + @Override + public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + appendForUpdate(element, introspectedTable); + return true; + } + + @Override + public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + appendForUpdate(element, introspectedTable); + return true; + } +} diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java index a428b396..8fce0eef 100644 --- a/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java +++ b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java @@ -27,6 +27,7 @@ public boolean validate(List list) { public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); + PrimitiveTypeWrapper longWrapper = new FullyQualifiedJavaType("long").getPrimitiveTypeWrapper(); Field limit = new Field(); limit.setName("limit"); @@ -51,19 +52,19 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec Field offset = new Field(); offset.setName("offset"); offset.setVisibility(JavaVisibility.PRIVATE); - offset.setType(integerWrapper); + offset.setType(longWrapper); topLevelClass.addField(offset); Method setOffset = new Method(); setOffset.setVisibility(JavaVisibility.PUBLIC); setOffset.setName("setOffset"); - setOffset.addParameter(new Parameter(integerWrapper, "offset")); + setOffset.addParameter(new Parameter(longWrapper, "offset")); setOffset.addBodyLine("this.offset = offset;"); topLevelClass.addMethod(setOffset); Method getOffset = new Method(); getOffset.setVisibility(JavaVisibility.PUBLIC); - getOffset.setReturnType(integerWrapper); + getOffset.setReturnType(longWrapper); getOffset.setName("getOffset"); getOffset.addBodyLine("return offset;"); topLevelClass.addMethod(getOffset); @@ -95,4 +96,27 @@ public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement elem return true; } + + /** + * 为Mapper.xml的selectByExampleWithBLOBs添加limit + */ + @Override + public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + XmlElement ifLimitNotNullElement = new XmlElement("if"); + ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null")); + + XmlElement ifOffsetNotNullElement = new XmlElement("if"); + ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null")); + ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNotNullElement); + + XmlElement ifOffsetNullElement = new XmlElement("if"); + ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null")); + ifOffsetNullElement.addElement(new TextElement("limit ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNullElement); + + element.addElement(ifLimitNotNullElement); + + return true; + } } diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/RepositoryPlugin.java b/src/main/java/com/zzg/mybatis/generator/plugins/RepositoryPlugin.java new file mode 100644 index 00000000..e9f06146 --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/plugins/RepositoryPlugin.java @@ -0,0 +1,34 @@ +package com.zzg.mybatis.generator.plugins; + +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.PluginAdapter; +import org.mybatis.generator.api.dom.java.*; + +import java.util.List; + +/** + * Project: mybatis-generator-gui + * + * @author slankka on 2017/12/13. + */ +public class RepositoryPlugin extends PluginAdapter { + + private FullyQualifiedJavaType annotationRepository; + private String annotation = "@Repository"; + + public RepositoryPlugin () { + annotationRepository = new FullyQualifiedJavaType("org.springframework.stereotype.Repository"); //$NON-NLS-1$ + } + + @Override + public boolean validate(List list) { + return true; + } + + @Override + public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + interfaze.addImportedType(annotationRepository); + interfaze.addAnnotation(annotation); + return true; + } +} diff --git a/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java b/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java index a23ecec8..c954ff90 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java +++ b/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java @@ -1,5 +1,9 @@ package com.zzg.mybatis.generator.util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; import java.sql.Connection; import java.sql.DriverManager; @@ -7,11 +11,13 @@ * Created by Owen on 8/21/16. */ public class ConnectionManager { - + private static final Logger _LOG = LoggerFactory.getLogger(ConnectionManager.class); private static final String DB_URL = "jdbc:sqlite:./config/sqlite3.db"; public static Connection getConnection() throws Exception { Class.forName("org.sqlite.JDBC"); + File file = new File(DB_URL.substring("jdbc:sqlite:".length())).getAbsoluteFile(); + _LOG.info("database FilePath :{}", file.getAbsolutePath()); Connection conn = DriverManager.getConnection(DB_URL); return conn; } diff --git a/src/main/resources/fxml/MainUI.fxml b/src/main/resources/fxml/MainUI.fxml index 0457fed7..3054eaf0 100644 --- a/src/main/resources/fxml/MainUI.fxml +++ b/src/main/resources/fxml/MainUI.fxml @@ -1,17 +1,27 @@ - - - - - + + + - + + + + + + + + + + + + + + + - - - + @@ -49,13 +59,14 @@ - + - - - - + + + + + @@ -64,8 +75,10 @@ - - + + + + @@ -100,7 +113,7 @@