Skip to content

Commit

Permalink
1. remove useless DAO
Browse files Browse the repository at this point in the history
2. Add a override XML choiceBox when generate XML
  • Loading branch information
zouzg committed Mar 31, 2018
1 parent c9a4dd6 commit 7dfbedb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mybatis-generator-gui

mybatis-generator-gui是基于[mybatis generator](http://www.mybatis.org/generator/index.html)开发一款界面工具, 本工具可以使你非常容易及快速生成Mybatis的Java POJO文件及数据库Mapping文件。

![MainUI](https://cloud.githubusercontent.com/assets/3505708/22870467/834ed1c2-f1e2-11e6-9ae5-650b88722a7c.png)
![image](https://user-images.githubusercontent.com/3505708/38157772-8cd08a7e-34bc-11e8-948b-bccbfa59848f.png)

### 核心特性
* 按照界面步骤轻松生成代码,省去XML繁琐的学习与配置过程
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.zzg</groupId>
<artifactId>mybatis-generator-gui</artifactId>
<version>0.8.4</version>
<version>0.8.5</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -86,9 +87,6 @@ public void generate() throws Exception {
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), selectedDatabaseConfig.getDbType(), true, null));
}

if (generatorConfig.getMapperName() != null) {
tableConfig.setMapperName(generatorConfig.getMapperName());
}
// add ignore columns
if (ignoredColumns != null) {
ignoredColumns.stream().forEach(ignoredColumn -> {
Expand All @@ -103,6 +101,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())) {
Expand Down Expand Up @@ -180,8 +183,29 @@ public void generate() throws Exception {
Set<String> contexts = new HashSet<>();
ShellCallback shellCallback = new DefaultShellCallback(true); // override=true
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, shellCallback, warnings);
// if overrideXML selected, delete oldXML ang generate new one
if (generatorConfig.isOverrideXML()) {
String mappingXMLFilePath = getMappingXMLFilePath(generatorConfig);
File mappingXMLFile = new File(mappingXMLFilePath);
if (mappingXMLFile.exists()) {
mappingXMLFile.delete();
}
}

myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
}

private String getMappingXMLFilePath(GeneratorConfig generatorConfig) {
StringBuilder sb = new StringBuilder();
sb.append(generatorConfig.getProjectFolder()).append("/");
sb.append(generatorConfig.getMappingXMLTargetFolder()).append("/");
String mappingXMLPackage = generatorConfig.getMappingXMLPackage();
if (StringUtils.isNotEmpty(mappingXMLPackage)) {
sb.append(mappingXMLPackage.replace(".", "/")).append("/");
}
sb.append(generatorConfig.getDomainObjectName()).append("Mapper.xml");
return sb.toString();
}

public void setProgressCallback(ProgressCallback progressCallback) {
this.progressCallback = progressCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,26 @@ public class MainUIController extends BaseFXController {
@FXML
private TextField domainObjectNameField;
@FXML
private TextField generateKeysField; //添加输入框
private TextField generateKeysField; //主键ID
@FXML
private TextField modelTargetProject;
@FXML
private TextField mappingTargetProject;
@FXML
private TextField daoTargetProject;
@FXML
private TextField mapperName;
@FXML
private TextField projectFolderField;
@FXML
private CheckBox offsetLimitCheckBox;
@FXML
private CheckBox commentCheckBox;
@FXML
private CheckBox overrideXML;
@FXML
private CheckBox needToStringHashcodeEquals;
@FXML
private CheckBox useTableNameAliasCheckbox;
@FXML
private CheckBox annotationCheckBox;
@FXML
private CheckBox useActualColumnNamesCheckbox;
Expand Down Expand Up @@ -186,9 +188,17 @@ public void initialize(URL location, ResourceBundle resources) {
return cell;
});
loadLeftDBTree();
encodingChoice.setItems(FXCollections.observableArrayList("UTF-8"));
encodingChoice.setValue("UTF-8");
}
setTooltip();
}

private void setTooltip() {
encodingChoice.setTooltip(new Tooltip("生成文件的编码,必选"));
generateKeysField.setTooltip(new Tooltip("insert时可以返回主键ID"));
offsetLimitCheckBox.setTooltip(new Tooltip("是否要生成分页查询代码"));
commentCheckBox.setTooltip(new Tooltip("使用数据库的列注释作为实体类字段名的Java注释 "));
useActualColumnNamesCheckbox.setTooltip(new Tooltip("是否使用数据库实际的列名作为实体类域的名称"));
useTableNameAliasCheckbox.setTooltip(new Tooltip("在Mapper XML文件中表名使用别名,并且列全部使用as查询"));
}

void loadLeftDBTree() {
TreeItem rootTreeItem = leftDBTree.getRoot();
Expand Down Expand Up @@ -298,14 +308,15 @@ 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());
generatorConfig.setDomainObjectName(domainObjectNameField.getText());
generatorConfig.setOffsetLimit(offsetLimitCheckBox.isSelected());
generatorConfig.setComment(commentCheckBox.isSelected());
generatorConfig.setOverrideXML(overrideXML.isSelected());
generatorConfig.setNeedToStringHashcodeEquals(needToStringHashcodeEquals.isSelected());
generatorConfig.setUseTableNameAlias(useTableNameAliasCheckbox.isSelected());
generatorConfig.setAnnotation(annotationCheckBox.isSelected());
generatorConfig.setUseActualColumnNames(useActualColumnNamesCheckbox.isSelected());
generatorConfig.setEncoding(encodingChoice.getValue());
Expand Down
32 changes: 24 additions & 8 deletions src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class GeneratorConfig {
private boolean offsetLimit;

private boolean comment;

private boolean overrideXML;

private boolean needToStringHashcodeEquals;

Expand All @@ -51,6 +53,8 @@ public class GeneratorConfig {
private String generateKeys;

private String encoding;

private boolean useTableNameAlias;

public boolean isUseExampe() {
return useExampe;
Expand Down Expand Up @@ -188,14 +192,6 @@ 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;
}
Expand All @@ -211,4 +207,24 @@ public String getEncoding() {
public void setEncoding(String encoding) {
this.encoding = encoding;
}

public boolean getUseTableNameAlias() {
return useTableNameAlias;
}

public void setUseTableNameAlias(boolean useTableNameAlias) {
this.useTableNameAlias = useTableNameAlias;
}

public boolean isUseTableNameAlias() {
return useTableNameAlias;
}

public boolean isOverrideXML() {
return overrideXML;
}

public void setOverrideXML(boolean overrideXML) {
this.overrideXML = overrideXML;
}
}
27 changes: 15 additions & 12 deletions src/main/resources/fxml/MainUI.fxml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<?import javafx.collections.FXCollections?>
<?import java.lang.*?>

<BorderPane prefHeight="613.0" prefWidth="918.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.MainUIController">
Expand Down Expand Up @@ -53,8 +55,7 @@
<ColumnConstraints hgrow="SOMETIMES" maxWidth="688.0" minWidth="10.0" prefWidth="170.0" />
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" maxWidth="688.0" minWidth="74.0" prefWidth="76.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="688.0" minWidth="10.0" prefWidth="166.0" />
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" maxWidth="688.0" minWidth="10.0" prefWidth="129.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="688.0" minWidth="10.0" prefWidth="95.0" />
<ColumnConstraints halignment="LEFT" hgrow="SOMETIMES" maxWidth="688.0" minWidth="10.0" prefWidth="129.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
Expand Down Expand Up @@ -155,7 +156,7 @@
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
<Label text="映射XML文件包名" GridPane.rowIndex="6" />
<Label text="Mapper XML文件包名" GridPane.rowIndex="6" />
<TextField fx:id="mapperTargetPackage" prefHeight="27.0" prefWidth="248.0" promptText="com.example" GridPane.columnIndex="1" GridPane.rowIndex="6">
<HBox.margin>
<Insets right="5.0" />
Expand All @@ -174,7 +175,13 @@
<HBox alignment="CENTER_LEFT">
<children>
<Label text="生成文件的编码" />
<ChoiceBox fx:id="encodingChoice" prefHeight="23.0" prefWidth="71.0" />
<ChoiceBox fx:id="encodingChoice" prefHeight="23.0" prefWidth="71.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="UTF-8" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="8">
Expand All @@ -192,6 +199,7 @@
<children>
<CheckBox fx:id="annotationCheckBox" mnemonicParsing="false" selected="false" text="生成JPA注解" />
<CheckBox fx:id="useActualColumnNamesCheckbox" mnemonicParsing="false" selected="false" text="使用实际的列名" />
<CheckBox fx:id="useTableNameAliasCheckbox" mnemonicParsing="false" selected="false" text="启用as别名查询" />
<CheckBox fx:id="useExample" mnemonicParsing="false" selected="true" text="使用Example" />
</children>
</HBox>
Expand All @@ -214,15 +222,10 @@
</styleClass></Button>
</children>
</HBox>
<Label text="自定义接口名称(选填)" GridPane.columnIndex="4" GridPane.rowIndex="5">
<padding>
<Insets left="5.0" />
</padding>
</Label>
<TextField fx:id="mapperName" prefHeight="27.0" prefWidth="138.0" promptText="PersonDAO" GridPane.columnIndex="5" GridPane.rowIndex="5">
<CheckBox fx:id="overrideXML" mnemonicParsing="false" selected="true" text="覆盖原XML" GridPane.columnIndex="4" GridPane.rowIndex="6">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></TextField>
<Insets left="10.0" />
</GridPane.margin></CheckBox>
</children>
</GridPane>
</children>
Expand Down

0 comments on commit 7dfbedb

Please sign in to comment.