diff --git a/pom.xml b/pom.xml index 0a2db26c..dc4186d9 100644 --- a/pom.xml +++ b/pom.xml @@ -11,19 +11,9 @@ - org.apache.httpcomponents - httpclient - 4.3.2 - - - org.apache.httpcomponents - httpcore - 4.3.2 - - - org.apache.httpcomponents - httpmime - 4.3.4 + org.mybatis.generator + mybatis-generator-core + 1.3.2 ch.qos.logback @@ -35,11 +25,6 @@ logback-core 1.1.2 - - org.springframework.session - spring-session-data-redis - 1.2.0.RC2 - diff --git a/src/main/java/com/zzg/mybatis/generator/MainUI.java b/src/main/java/com/zzg/mybatis/generator/MainUI.java index 9734ca22..d2b56048 100644 --- a/src/main/java/com/zzg/mybatis/generator/MainUI.java +++ b/src/main/java/com/zzg/mybatis/generator/MainUI.java @@ -2,25 +2,30 @@ import java.net.URL; +import com.zzg.mybatis.generator.controller.MainUIController; + import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; +import javafx.stage.StageStyle; public class MainUI extends Application { @Override public void start(Stage primaryStage) throws Exception { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - - URL skeletonResource = classLoader.getResource("MainUI.fxml"); - - Parent root = FXMLLoader.load(skeletonResource); - + URL url = Thread.currentThread().getContextClassLoader().getResource("MainUI.fxml"); + FXMLLoader fxmlLoader = new FXMLLoader(url); + Parent root = fxmlLoader.load(); + //primaryStage.initStyle(StageStyle.TRANSPARENT); + primaryStage.setResizable(false); primaryStage.setScene(new Scene(root)); - primaryStage.show(); + + + MainUIController controller = fxmlLoader.getController(); + controller.setPrimaryStage(primaryStage); } 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 facd590c..715fc7a7 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -1,151 +1,179 @@ package com.zzg.mybatis.generator.controller; +import java.io.File; import java.net.URL; -import java.util.Collection; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.ResourceBundle; import java.util.Set; -import java.util.stream.Collectors; - -import org.springframework.data.redis.connection.DataType; -import org.springframework.data.redis.connection.RedisNode; -import org.springframework.data.redis.connection.RedisSentinelConfiguration; -import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.data.redis.core.ZSetOperations.TypedTuple; -import org.springframework.data.redis.serializer.StringRedisSerializer; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; + +import org.mybatis.generator.api.MyBatisGenerator; +import org.mybatis.generator.api.ProgressCallback; +import org.mybatis.generator.api.ShellCallback; +import org.mybatis.generator.api.VerboseProgressCallback; +import org.mybatis.generator.config.Configuration; +import org.mybatis.generator.config.Context; +import org.mybatis.generator.config.JDBCConnectionConfiguration; +import org.mybatis.generator.config.JavaClientGeneratorConfiguration; +import org.mybatis.generator.config.JavaModelGeneratorConfiguration; +import org.mybatis.generator.config.ModelType; +import org.mybatis.generator.config.SqlMapGeneratorConfiguration; +import org.mybatis.generator.config.TableConfiguration; +import org.mybatis.generator.internal.DefaultShellCallback; + +import com.zzg.mybatis.generator.model.DatabaseDTO; + import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.ListView; +import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextField; -import javafx.scene.control.TreeItem; -import javafx.scene.control.TreeView; +import javafx.scene.text.Text; +import javafx.stage.DirectoryChooser; +import javafx.stage.FileChooser; +import javafx.stage.FileChooser.ExtensionFilter; +import javafx.stage.Stage; public class MainUIController implements Initializable { @FXML - private TextField nodesField; + private ChoiceBox dbTypeChoice; @FXML - private TextField masterField; + private TextField driverClassField; @FXML - private TextField databaseField; - + private TextField connectorFileField; @FXML - private Label connectStatusLabel; - + private TextField connectionUrlField; @FXML - private Label typeLabel; - + private TextField userNameField; @FXML - private Button connectBtn; - + private TextField passwordField; @FXML - private TreeView treeView; - + private TextField modelFolderField; + @FXML + private TextField mapperFolderField; + @FXML + private TextField daoFolderField; + @FXML + private TextField tableNameField; + @FXML + private TextField domainObjectNameField; + @FXML + private TextField packageNameField; @FXML - private ListView valueListView; + private TextField projectFolderField; - private StringRedisTemplate redis; + private Stage primaryStage; @Override public void initialize(URL location, ResourceBundle resources) { + dbTypeChoice.setOnKeyPressed(event -> { + String driverClass = dbTypeChoice.getSelectionModel().getSelectedItem().getDriverClass(); + driverClassField.setText(driverClass); + }); + driverClassField.setText("com.mysql.jdbc.Driver"); } @FXML - void connectToRedis() { - String redisNodes = nodesField.getText(); - String master = masterField.getText(); - redis = new StringRedisTemplate(); - redis.setKeySerializer(new StringRedisSerializer()); - RedisSentinelConfiguration config = new RedisSentinelConfiguration(); - config.setMaster(master); - String[] nodes = redisNodes.split(","); - for (String node : nodes) { - String[] hostport = node.split(":"); - String host = hostport[0]; - int port = Integer.parseInt(hostport[1]); - config.addSentinel(new RedisNode(host, port)); + public void chooseConnectorFile() { + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Select " + dbTypeChoice.getSelectionModel().getSelectedItem().getName() + " Connector jar file"); + fileChooser.getExtensionFilters().add(new ExtensionFilter("jar file", "*.jar")); + File selectedFile = fileChooser.showOpenDialog(primaryStage); + if (selectedFile != null) { + connectorFileField.setText(selectedFile.getAbsolutePath()); } - JedisConnectionFactory factory = new JedisConnectionFactory(config); - factory.setUsePool(true); - factory.afterPropertiesSet(); - redis.setConnectionFactory(factory); - redis.afterPropertiesSet(); - - connectStatusLabel.setText("连ζŽ₯成功"); - testKeys(); } @FXML - void testKeys() { - TreeItem rootItem = new TreeItem<>("Root"); - treeView.setRoot(rootItem); - treeView.setShowRoot(false); - Set keys = redis.keys("Cobra_*"); - if (keys != null && keys.size() > 0) { - keys.stream().forEach(key -> { - TreeItem item = new TreeItem<>(); - Label label = new Label(key); - // label.setBackground(new Background(new BackgroundFill(new Color(1, 0, 0, 1), null, null))); - label.setOnMouseClicked(event -> { - ObservableList observableList = FXCollections.observableArrayList(); - DataType type = redis.type(key); - String code = type.code(); - typeLabel.setText(code); - System.out.println(code); - Collection values = null; - if ("zset".equals(code)) { - Set> hashKeys = redis.opsForZSet().rangeWithScores(key, 0, -1); - if (hashKeys != null && hashKeys.size() > 0) { - values = hashKeys.stream().map( - tuple -> "value: " + tuple.getValue() + ", score: " + tuple.getScore().longValue()) - .collect(Collectors.toList()); - } - System.out.println(values); - } else if ("set".equals(code)) { - values = redis.opsForSet().members(key); - System.out.println(key + ": " + values); - } else if ("hash".equals(code)) { - Set hashKeys = redis.opsForHash().keys(key); - if (hashKeys != null && hashKeys.size() > 0) { - values = hashKeys.stream().map( - hashKey -> "Key: " + hashKey + ", Value: " + redis.opsForHash().get(key, hashKey)) - .collect(Collectors.toList()); - } - System.out.println(key + ": " + values); - } else if ("list".equals(code)) { - values = redis.opsForList().range(key, 0, -1); - System.out.println(values); - } - - if (values != null && values.size() > 0) { - values.stream().forEach(value -> { - observableList.add(value.toString()); - }); - } - valueListView.setItems(observableList); - }); - item.setGraphic(label); - item.setValue(""); - rootItem.getChildren().add(item); - }); + public void chooseModelFolder() { + DirectoryChooser directoryChooser = new DirectoryChooser(); + File selectedFolder = directoryChooser.showDialog(primaryStage); + if (selectedFolder != null) { + modelFolderField.setText(selectedFolder.getAbsolutePath()); } } @FXML - void clearAllKeys() { - Set keys = redis.keys("Cobra_*"); - if (keys != null && keys.size() > 0) { - keys.stream().forEach(key -> { - redis.delete(key); - }); + public void chooseMapperFolder() { + DirectoryChooser directoryChooser = new DirectoryChooser(); + File selectedFolder = directoryChooser.showDialog(primaryStage); + if (selectedFolder != null) { + mapperFolderField.setText(selectedFolder.getAbsolutePath()); } } + @FXML + public void chooseDaoFolder() { + DirectoryChooser directoryChooser = new DirectoryChooser(); + File selectedFolder = directoryChooser.showDialog(primaryStage); + if (selectedFolder != null) { + daoFolderField.setText(selectedFolder.getAbsolutePath()); + } + } + + @FXML + public void chooseProjectFolder() { + DirectoryChooser directoryChooser = new DirectoryChooser(); + File selectedFolder = directoryChooser.showDialog(primaryStage); + if (selectedFolder != null) { + projectFolderField.setText(selectedFolder.getAbsolutePath()); + } + } + + @FXML + public void generateCode() throws Exception { + Configuration config = new Configuration(); + config.addClasspathEntry(connectorFileField.getText()); + Context context = new Context(ModelType.CONDITIONAL); + config.addContext(context); + // Table config + TableConfiguration tableConfig = new TableConfiguration(context); + tableConfig.setTableName(tableNameField.getText()); + tableConfig.setDomainObjectName(domainObjectNameField.getText()); + // JDBC config + JDBCConnectionConfiguration jdbcConfig = new JDBCConnectionConfiguration(); + jdbcConfig.setDriverClass(driverClassField.getText()); + jdbcConfig.setConnectionURL(connectionUrlField.getText()); + jdbcConfig.setUserId(userNameField.getText()); + jdbcConfig.setPassword(passwordField.getText()); + // java model + JavaModelGeneratorConfiguration modelConfig = new JavaModelGeneratorConfiguration(); + modelConfig.setTargetPackage(packageNameField.getText()); + modelConfig.setTargetProject(projectFolderField.getText()); + // Mapper config + SqlMapGeneratorConfiguration mapperConfig = new SqlMapGeneratorConfiguration(); + mapperConfig.setTargetPackage(packageNameField.getText()); + mapperConfig.setTargetProject(projectFolderField.getText()); + // DAO + JavaClientGeneratorConfiguration daoConfig = new JavaClientGeneratorConfiguration(); + daoConfig.setConfigurationType("XMLMAPPER"); + daoConfig.setTargetPackage(packageNameField.getText()); + daoConfig.setTargetProject(projectFolderField.getText()); + + context.setId("myid"); + context.addTableConfiguration(tableConfig); + context.setJdbcConnectionConfiguration(jdbcConfig); + context.setJdbcConnectionConfiguration(jdbcConfig); + context.setJavaModelGeneratorConfiguration(modelConfig); + context.setSqlMapGeneratorConfiguration(mapperConfig); + context.setJavaClientGeneratorConfiguration(daoConfig); + + context.setTargetRuntime("MyBatis3"); + + List warnings = new ArrayList<>(); + Set fullyqualifiedTables = new HashSet(); + Set contexts = new HashSet(); + ProgressCallback progressCallback = new VerboseProgressCallback(); + + ShellCallback shellCallback = new DefaultShellCallback(true); // override=true + MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback , warnings); + myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables); + } + + public void setPrimaryStage(Stage primaryStage) { + this.primaryStage = primaryStage; + } } diff --git a/src/main/java/com/zzg/mybatis/generator/model/DatabaseDTO.java b/src/main/java/com/zzg/mybatis/generator/model/DatabaseDTO.java index fd5f6816..74743d05 100644 --- a/src/main/java/com/zzg/mybatis/generator/model/DatabaseDTO.java +++ b/src/main/java/com/zzg/mybatis/generator/model/DatabaseDTO.java @@ -4,6 +4,7 @@ public class DatabaseDTO { private String name; private int value; + private String driverClass; public String getName() { return name; @@ -21,6 +22,14 @@ public void setValue(int value) { this.value = value; } + public String getDriverClass() { + return driverClass; + } + + public void setDriverClass(String driverClass) { + this.driverClass = driverClass; + } + @Override public String toString() { return name; diff --git a/src/main/resources/MainUI.fxml b/src/main/resources/MainUI.fxml index e2361a67..33cbd6f3 100644 --- a/src/main/resources/MainUI.fxml +++ b/src/main/resources/MainUI.fxml @@ -1,5 +1,6 @@ + @@ -8,18 +9,11 @@ - - + - + - +