Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Aug 13, 2016
2 parents b41f320 + d7a8609 commit 60bb0dd
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 93 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/ojdbc14.jar</systemPath>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/zzg/mybatis/generator/MainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 这是本软件的主入口,要运行本软件请直接运行本类就可以了,不用传入任何参数
*/
public class MainUI extends Application {

private static final Logger _LOG = LoggerFactory.getLogger(MainUI.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.File;
import java.net.URL;
import java.sql.SQLTimeoutException;
import java.util.*;

import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import com.zzg.mybatis.generator.bridge.MybatisGeneratorBridge;
import com.zzg.mybatis.generator.model.*;
import com.zzg.mybatis.generator.util.DbUtil;
Expand All @@ -14,13 +16,15 @@
import com.zzg.mybatis.generator.view.UIProgressCallback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.stage.*;
import javafx.util.Callback;
import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.XMLConfiguration;
Expand All @@ -41,11 +45,7 @@
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainUIController extends BaseFXController {

Expand Down Expand Up @@ -109,64 +109,52 @@ public void initialize(URL location, ResourceBundle resources) {
leftDBTree.setCellFactory((TreeView<String> tv) -> {
TreeCell<String> cell = defaultCellFactory.call(tv);
cell.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
int level = leftDBTree.getTreeItemLevel(cell.getTreeItem());
TreeCell<String> treeCell = (TreeCell<String>) event.getSource();
TreeItem<String> treeItem = treeCell.getTreeItem();
if (level == 1) {
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("Close Connection");
item1.setOnAction(event1 -> {
treeItem.getChildren().clear();
});
MenuItem item2 = new MenuItem("Delete");
item2.setOnAction(event1 -> {
// TODO
});
contextMenu.getItems().addAll(item1);
cell.setContextMenu(contextMenu);
}
if (event.getClickCount() == 2) {
int level = leftDBTree.getTreeItemLevel(cell.getTreeItem());
TreeCell<String> treeCell = (TreeCell<String>) event.getSource();
TreeItem<String> item = treeCell.getTreeItem();
item.setExpanded(true);
treeItem.setExpanded(true);
if (level == 1) {
DatabaseConfig selectedConfig = (DatabaseConfig) item.getGraphic().getUserData();
// Accept clicks only on node cells, and not on empty spaces of the TreeView
leftDBTree.getSelectionModel().getSelectedItem().setExpanded(true);
System.out.println("Node click: " + selectedConfig);
List<String> schemas = null;
try {
schemas = DbUtil.getSchemas(selectedConfig);
System.out.println(schemas);
if (schemas != null && schemas.size() > 0) {
ObservableList<TreeItem<String>> children = cell.getTreeItem().getChildren();
children.clear();
for (String schema : schemas) {
TreeItem<String> treeItem = new TreeItem<>();
ImageView imageView = new ImageView("icons/database.png");
imageView.setFitHeight(16);
imageView.setFitWidth(16);
treeItem.setGraphic(imageView);
treeItem.setValue(schema);
children.add(treeItem);
}
}
} catch (Exception e) {
AlertUtil.showErrorAlert(e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e));
}
} else if (level == 2) {
System.out.println("index: " + leftDBTree.getSelectionModel().getSelectedIndex());
DatabaseConfig selectedConfig = (DatabaseConfig) item.getParent().getGraphic().getUserData();
String schema = treeCell.getTreeItem().getValue();
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
try {
List<String> tables = DbUtil.getTableNames(selectedConfig, schema);
List<String> tables = DbUtil.getTableNames(selectedConfig);
if (tables != null && tables.size() > 0) {
ObservableList<TreeItem<String>> children = cell.getTreeItem().getChildren();
children.clear();
for (String tableName : tables) {
TreeItem<String> treeItem = new TreeItem<>();
TreeItem<String> newTreeItem = new TreeItem<>();
ImageView imageView = new ImageView("icons/table.png");
imageView.setFitHeight(16);
imageView.setFitWidth(16);
treeItem.setGraphic(imageView);
treeItem.setValue(tableName);
children.add(treeItem);
newTreeItem.setGraphic(imageView);
newTreeItem.setValue(tableName);
children.add(newTreeItem);
}
}
} catch (CommunicationsException e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert("Connection timeout");
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert(e.getMessage());
}
} else if (level == 3) { // left DB tree level3
} else if (level == 2) { // left DB tree level3
String tableName = treeCell.getTreeItem().getValue();
selectedDatabaseConfig = (DatabaseConfig)item.getParent().getParent().getGraphic().getUserData();
String schema = item.getParent().getValue();
selectedDatabaseConfig.setSchema(schema);
selectedDatabaseConfig = (DatabaseConfig) treeItem.getParent().getGraphic().getUserData();
this.tableName = tableName;
tableNameField.setText(tableName);
domainObjectNameField.setText(StringUtils.dbStringToCamelStyle(tableName));
Expand Down Expand Up @@ -277,7 +265,7 @@ public void openTableColumnCustomizationPage() {
try {
// If select same schema and another table, update table data
if (!tableName.equals(controller.getTableName())) {
List<UITableColumnVO> tableColumns = DbUtil.getTableColumns(selectedDatabaseConfig, selectedDatabaseConfig.getSchema(), tableName);
List<UITableColumnVO> tableColumns = DbUtil.getTableColumns(selectedDatabaseConfig, tableName);
controller.setColumnList(FXCollections.observableList(tableColumns));
controller.setTableName(tableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class NewConnectionController extends BaseFXController {
private TextField passwordField;
// @FXML
// private CheckBox savePwdCheckBox;
@FXML
private TextField schemaField;
@FXML
private ChoiceBox<String> encodingChoice;
@FXML
Expand Down Expand Up @@ -69,6 +71,7 @@ void saveConnection() {
dbConfig.setPassword(password);
// if (savePwdCheckBox.isSelected()) {
// }
dbConfig.setSchema(schemaField.getText());
dbConfig.setEncoding(encoding);
try {
XMLConfigHelper.saveDatabaseConfig(name, dbConfig);
Expand All @@ -90,13 +93,15 @@ void testConnection() {
String encoding = encodingChoice.getValue();
String dbType = dbTypeChoice.getValue();
DatabaseConfig config = new DatabaseConfig();
config.setName(name);
config.setDbType(dbType);
config.setHost(host);
config.setPort(port);
config.setUsername(userName);
config.setPassword(password);
config.setSchema(schemaField.getText());
config.setEncoding(encoding);
String url = DbUtil.getConnectionUrlWithoutSchema(config);
String url = DbUtil.getConnectionUrlWithSchema(config);
System.out.println(url);
try {
DbUtil.getConnection(config);
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/zzg/mybatis/generator/model/DbType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
* Created by Owen on 6/14/16.
*/
public enum DbType {
MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s?useUnicode=true&useSSL=false&characterEncoding=%s", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s"),
ORACLE("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s", "");

MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s"),
Oracle("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s");

private final String driverClass;
private final String connectionUrlPattern;
private final String fullConnectionUrlPattern;

DbType(String driverClass, String connectionUrlPattern, String fullConnectionUrlPattern) {
DbType(String driverClass, String connectionUrlPattern) {
this.driverClass = driverClass;
this.connectionUrlPattern = connectionUrlPattern;
this.fullConnectionUrlPattern = fullConnectionUrlPattern;
}

public String getDriverClass() {
Expand All @@ -25,7 +24,4 @@ public String getConnectionUrlPattern() {
return connectionUrlPattern;
}

public String getFullConnectionUrlPattern() {
return fullConnectionUrlPattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.zzg.mybatis.generator.plugins;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
Expand All @@ -27,6 +28,8 @@
import static org.mybatis.generator.internal.util.StringUtility.isTrue;

/**
* 此插件使用数据库表中列的注释来生成Java Model中属性的注释
*
* @author Owen Zou
*
*/
Expand Down Expand Up @@ -59,7 +62,6 @@ public void addRootComment(XmlElement rootElement) {

public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);

columnRemarks = isTrue(properties
.getProperty("columnRemarks"));
}
Expand All @@ -75,12 +77,13 @@ public void addEnumComment(InnerEnum innerEnum,
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (!columnRemarks) {
String columnRemarks = introspectedColumn.getRemarks();
if (!this.columnRemarks || StringUtils.isEmpty(columnRemarks)) {
return;
}
field.addJavaDocLine("/**"); //$NON-NLS-1$
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
field.addJavaDocLine(" */"); //$NON-NLS-1$
field.addJavaDocLine("/**");
field.addJavaDocLine("* " + columnRemarks);
field.addJavaDocLine("*/");
}

public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
Expand Down
52 changes: 23 additions & 29 deletions src/main/java/com/zzg/mybatis/generator/util/DbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.UITableColumnVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
Expand All @@ -13,46 +15,43 @@
*/
public class DbUtil {

public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException {
DbType dbType = DbType.valueOf(config.getDbType());
Class.forName(dbType.getDriverClass());
return DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword());
}
private static final Logger _LOG = LoggerFactory.getLogger(DbUtil.class);
private static final int DB_CONNECTION_TIMEOUTS_SENCONDS = 1;

public static List<String> getSchemas(DatabaseConfig config) throws Exception {
public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException {
DbType dbType = DbType.valueOf(config.getDbType());
Class.forName(dbType.getDriverClass());
Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword());
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getCatalogs();
List<String> schemas = new ArrayList<>();
while (rs.next()) {
schemas.add(rs.getString("TABLE_CAT"));
}
return schemas;
DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS);
String url = getConnectionUrlWithSchema(config);
_LOG.info("getConnection, connection url: {}", url);
return DriverManager.getConnection(url, config.getUsername(), config.getPassword());
}

public static List<String> getTableNames(DatabaseConfig config, String schema) throws Exception {
public static List<String> getTableNames(DatabaseConfig config) throws Exception {
DbType dbType = DbType.valueOf(config.getDbType());
Class.forName(dbType.getDriverClass());
Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword());
conn.setSchema(schema);
String url = getConnectionUrlWithSchema(config);
_LOG.info("getTableNames, connection url: {}", url);
DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS);
Connection conn = DriverManager.getConnection(url, config.getUsername(), config.getPassword());
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(schema, null, null, null);
ResultSet rs = md.getTables(null, null, null, null);
List<String> tables = new ArrayList<>();
while (rs.next()) {
tables.add(rs.getString(3));
}
return tables;
}

public static List<UITableColumnVO> getTableColumns(DatabaseConfig dbConfig, String schema, String tableName) throws Exception {
public static List<UITableColumnVO> getTableColumns(DatabaseConfig dbConfig, String tableName) throws Exception {
DbType dbType = DbType.valueOf(dbConfig.getDbType());
Class.forName(dbType.getDriverClass());
Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(dbConfig), dbConfig.getUsername(), dbConfig.getPassword());
conn.setSchema(schema);
DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS);
String url = getConnectionUrlWithSchema(dbConfig);
_LOG.info("getTableColumns, connection url: {}", url);
Connection conn = DriverManager.getConnection(url, dbConfig.getUsername(), dbConfig.getPassword());
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getColumns(schema, null, tableName, null);
ResultSet rs = md.getColumns(null, null, tableName, null);
List<UITableColumnVO> columns = new ArrayList<>();
while (rs.next()) {
UITableColumnVO columnVO = new UITableColumnVO();
Expand All @@ -65,15 +64,10 @@ public static List<UITableColumnVO> getTableColumns(DatabaseConfig dbConfig, Str
return columns;
}

public static String getConnectionUrlWithoutSchema(DatabaseConfig dbConfig) {
DbType dbType = DbType.valueOf(dbConfig.getDbType());
String connectionUrl = String.format(dbType.getConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getEncoding());
return connectionUrl;
}

public static String getConnectionUrlWithSchema(DatabaseConfig dbConfig) {
DbType dbType = DbType.valueOf(dbConfig.getDbType());
String connectionUrl = String.format(dbType.getFullConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getSchema(), dbConfig.getEncoding());
String connectionUrl = String.format(dbType.getConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getSchema(), dbConfig.getEncoding());
_LOG.info("getConnectionUrlWithSchema, connection url: {}", connectionUrl);
return connectionUrl;
}

Expand Down
Loading

0 comments on commit 60bb0dd

Please sign in to comment.