Skip to content

Commit

Permalink
完善驱动查找方式,解决jar包运行找不到驱动的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Feb 9, 2017
1 parent d2ecab4 commit fc109d4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ mybatis-generator-gui是基于[mybatis generator](http://www.mybatis.org/generat
cd mybatis-generator-gui
mvn jfx:jar
### 启动本软件
* 方法一: 下载的zip包

解压zip包,如果安装好了JRE或者JDK 8,直接cd至软件目录运行

java -jar mybatis-generator-gui.jar

* 方法二: 自助构建
* 方法一: 自助构建


mvn install:install-file -Dfile=./src/main/resources/connector/ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -DgeneratePom=true
cd target/jfx/app/
java -jar mybatis-generator-gui.jar


* 方法三: IDE中运行
* 方法二: IDE中运行

Eclipse or IntelliJ IDEA中启动, 找到MainUI类并运行就可以了
Eclipse or IntelliJ IDEA中启动, 找到```com.zzg.mybatis.generator.MainUI```类并运行就可以了

#### Oracle用户请注意
由于Oracle的驱动在maven官方的repository中没有,Oracle的用户需要手动安装一下驱动,cd到项目目录下,执行:

mvn install:install-file -Dfile=./src/main/resources/connector/ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -DgeneratePom=true



### 文档
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.6</version>
<version>0.7.2</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -26,6 +28,8 @@
*/
public class MybatisGeneratorBridge {

private static final Logger _LOG = LoggerFactory.getLogger(MybatisGeneratorBridge.class);

private GeneratorConfig generatorConfig;

private DatabaseConfig selectedDatabaseConfig;
Expand Down Expand Up @@ -57,6 +61,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);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/zzg/mybatis/generator/model/DbType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
public enum DbType {

MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s", "connector/mysql-connector-java-5.1.38.jar"),
Oracle("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s", "connector/ojdbc14.jar"),
PostgreSQL("org.postgresql.Driver", "jdbc:postgresql://%s:%s/%s", "connector/postgresql-9.4.1209.jar");
MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-5.1.38.jar"),
Oracle("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s", "ojdbc14.jar"),
PostgreSQL("org.postgresql.Driver", "jdbc:postgresql://%s:%s/%s", "postgresql-9.4.1209.jar");

private final String driverClass;
private final String connectionUrlPattern;
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.GeneratorConfig;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -200,17 +201,18 @@ public static int deleteGeneratorConfig(String name) throws Exception {
public static String findConnectorLibPath(String dbType) {
DbType type = DbType.valueOf(dbType);
String connectorJarFileName = type.getConnectorJarFile();
URL resource = Thread.currentThread().getContextClassLoader().getResource(connectorJarFileName);
URL resource = Thread.currentThread().getContextClassLoader().getResource("logback.xml");
_LOG.info("jar resource: {}", resource);
if (resource != null) {
try {
return resource.toURI().getRawPath();
} catch (URISyntaxException e) {
// ignore
try {
File file = new File(resource.toURI().getRawPath() + "/../lib/" + type.getConnectorJarFile());
return file.getCanonicalPath();
} catch (Exception e) {
throw new RuntimeException("找不到驱动文件,请联系开发者");
}
} else {
throw new RuntimeException("connector can't find");
throw new RuntimeException("lib can't find");
}
return null;
}


Expand Down
File renamed without changes.

0 comments on commit fc109d4

Please sign in to comment.