Skip to content

Commit

Permalink
Merge pull request zouzg#193 from slankka/slankka
Browse files Browse the repository at this point in the history
优化界面,增强功能
  • Loading branch information
zouzg authored Jun 10, 2018
2 parents 952b046 + 3dffffe commit a8f93a5
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 80 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文件。

![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繁琐的学习与配置过程
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/zzg/mybatis/generator/MainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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())) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<String> warnings = new ArrayList<>();
Expand All @@ -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("/");
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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"));
}
}
});
Expand All @@ -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"));
Expand All @@ -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() {
Expand Down Expand Up @@ -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());
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

}
}
Loading

0 comments on commit a8f93a5

Please sign in to comment.