Skip to content

Commit

Permalink
customize classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed May 27, 2017
1 parent 4743dd6 commit 53b2e50
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 144 deletions.
15 changes: 0 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,6 @@
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.5</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1209</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down
17 changes: 1 addition & 16 deletions src/main/java/com/zzg/mybatis/generator/MainUI.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
package com.zzg.mybatis.generator;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zzg.mybatis.generator.controller.MainUIController;

import com.zzg.mybatis.generator.model.GeneratorConfig;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.view.AlertUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.net.URL;

/**
* 这是本软件的主入口,要运行本软件请直接运行本类就可以了,不用传入任何参数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.internal.ObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -20,6 +21,8 @@
import java.util.List;
import java.util.Set;

import static org.mybatis.generator.internal.util.ClassloaderUtility.getCustomClassloader;

/**
* The bridge between GUI and the mybatis generator. All the operation to mybatis generator should proceed through this
* class
Expand All @@ -39,16 +42,18 @@ public class MybatisGeneratorBridge {
private List<IgnoredColumn> ignoredColumns;

private List<ColumnOverride> columnOverrides;
/**
* The Context will share between all controller to be a the mybatis generator configuration collector
*/
private static Configuration configuration = new Configuration();
private static Context context = new Context(ModelType.CONDITIONAL);

public MybatisGeneratorBridge() {
init();
}
static {
configuration.addContext(context);
context.addProperty("javaFileEncoding", "UTF-8");
}

private void init() {
Configuration config = new Configuration();
Context context = new Context(ModelType.CONDITIONAL);
context.addProperty("javaFileEncoding", "UTF-8");
config.addContext(context);
public MybatisGeneratorBridge() {
}

public void setGeneratorConfig(GeneratorConfig generatorConfig) {
Expand All @@ -60,13 +65,7 @@ public void setDatabaseConfig(DatabaseConfig databaseConfig) {
}

public void generate() throws Exception {
Configuration config = new Configuration();
String connectorLibPath = ConfigHelper.findConnectorLibPath(selectedDatabaseConfig.getDbType());
_LOG.info("connectorLibPath: {}", connectorLibPath);
config.addClasspathEntry(connectorLibPath);
Context context = new Context(ModelType.CONDITIONAL);
config.addContext(context);
// Table config
// Table configuration
TableConfiguration tableConfig = new TableConfiguration(context);
tableConfig.setTableName(generatorConfig.getTableName());
tableConfig.setDomainObjectName(generatorConfig.getDomainObjectName());
Expand Down Expand Up @@ -101,7 +100,7 @@ public void generate() throws Exception {
JavaModelGeneratorConfiguration modelConfig = new JavaModelGeneratorConfiguration();
modelConfig.setTargetPackage(generatorConfig.getModelPackage());
modelConfig.setTargetProject(generatorConfig.getProjectFolder() + "/" + generatorConfig.getModelPackageTargetFolder());
// Mapper config
// Mapper configuration
SqlMapGeneratorConfiguration mapperConfig = new SqlMapGeneratorConfiguration();
mapperConfig.setTargetPackage(generatorConfig.getMappingXMLPackage());
mapperConfig.setTargetProject(generatorConfig.getProjectFolder() + "/" + generatorConfig.getMappingXMLTargetFolder());
Expand Down Expand Up @@ -148,12 +147,15 @@ public void generate() throws Exception {
Set<String> fullyqualifiedTables = new HashSet<>();
Set<String> contexts = new HashSet<>();
ShellCallback shellCallback = new DefaultShellCallback(true); // override=true
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, shellCallback, warnings);
myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
}

public static Context getContext() {
return context;
}

public void setProgressCallback(ProgressCallback progressCallback) {
public void setProgressCallback(ProgressCallback progressCallback) {
this.progressCallback = progressCallback;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.zzg.mybatis.generator.controller;

import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import com.zzg.mybatis.generator.bridge.MybatisGeneratorBridge;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.GeneratorConfig;
Expand Down Expand Up @@ -30,6 +29,7 @@

import java.io.File;
import java.net.URL;
import java.sql.SQLRecoverableException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -158,7 +158,7 @@ public void initialize(URL location, ResourceBundle resources) {
children.add(newTreeItem);
}
}
} catch (CommunicationsException e) {
} catch (SQLRecoverableException e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert("连接超时");
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ void testConnection() {
return;
}
try {
String url = DbUtil.getConnectionUrlWithSchema(config);
System.out.println(url);
DbUtil.getConnection(config);
AlertUtil.showInfoAlert("连接成功");
} catch (Exception e) {
Expand Down
145 changes: 82 additions & 63 deletions src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,114 @@
package com.zzg.mybatis.generator.model;

import java.util.Objects;

/**
* Created by Owen on 5/13/16.
*/
public class DatabaseConfig {

private String dbType;
/**
* The name of the config
*/
private String name;
private String dbType;
/**
* The name of the config
*/
private String name;

private String host;

private String host;
private String port;

private String port;
private String schema;

private String schema;
private String username;

private String username;
private String password;

private String password;
private String encoding;

private String encoding;
public String getName() {
return name;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}
public String getHost() {
return host;
}

public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}

public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}

public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}

public void setPort(String port) {
this.port = port;
}
public String getSchema() {
return schema;
}

public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}

public void setSchema(String schema) {
this.schema = schema;
}
public String getUsername() {
return username;
}

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public void setPassword(String password) {
this.password = password;
}
public String getEncoding() {
return encoding;
}

public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}
public String getDbType() {
return dbType;
}

public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}

public void setDbType(String dbType) {
this.dbType = dbType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DatabaseConfig that = (DatabaseConfig) o;
return Objects.equals(dbType, that.dbType) && Objects.equals(name, that.name) && Objects.equals(host, that
.host) && Objects.equals(port, that.port) && Objects.equals(schema, that.schema) && Objects.equals
(username, that.username) && Objects.equals(password, that.password) && Objects.equals(encoding, that
.encoding);
}

@Override
public String toString() {
return name;
}
@Override
public int hashCode() {
return Objects.hash(dbType, name, host, port, schema, username, password, encoding);
}

@Override
public String toString() {
return "DatabaseConfig{" + "dbType='" + dbType + '\'' + ", name='" + name + '\'' + ", host='" + host + '\'' +
", port='" + port + '\'' + ", schema='" + schema + '\'' + ", username='" + username + '\'' + ", " +
"password='" + password + '\'' + ", encoding='" + encoding + '\'' + '}';
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,25 @@ public static String findConnectorLibPath(String dbType) {
}
}

public static List<String> getAllJDBCDriverJarPaths() {
List<String> jarFilePathList = new ArrayList<>();
URL resource = Thread.currentThread().getContextClassLoader().getResource("logback.xml");
try {
String path = resource.toURI().getRawPath();
File file = new File(path.substring(0, path.lastIndexOf("/")) + "/lib");
File[] jarFiles = file.listFiles();
if (jarFiles != null && jarFiles.length > 0) {
for (File jarFile : jarFiles) {
if (jarFile.isFile() && jarFile.getAbsolutePath().endsWith(".jar")) {
jarFilePathList.add(jarFile.getAbsolutePath());
}
}
}
} catch (Exception e) {
throw new RuntimeException("找不到驱动文件,请联系开发者");
}
return jarFilePathList;
}


}
Loading

0 comments on commit 53b2e50

Please sign in to comment.