Skip to content

Commit

Permalink
support mysql8
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Aug 17, 2018
1 parent 754bb81 commit 49a33e4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ public void generate() throws Exception {
Context context = new Context(ModelType.CONDITIONAL);
configuration.addContext(context);

context.addProperty("autoDelimitKeywords", "true");
context.addProperty("autoDelimitKeywords", "true");
context.addProperty("beginningDelimiter", "`");
context.addProperty("endingDelimiter", "`");

context.addProperty("javaFileEncoding", "UTF-8");
String connectorLibPath = ConfigHelper.findConnectorLibPath(selectedDatabaseConfig.getDbType());
String dbType = selectedDatabaseConfig.getDbType();
String connectorLibPath = ConfigHelper.findConnectorLibPath(dbType);
_LOG.info("connectorLibPath: {}", connectorLibPath);
configuration.addClasspathEntry(connectorLibPath);
// Table configuration
Expand All @@ -76,31 +77,31 @@ public void generate() throws Exception {
tableConfig.setSelectByExampleStatementEnabled(false);
}

if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
} else {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
} else {
tableConfig.setCatalog(selectedDatabaseConfig.getSchema());
}
if (generatorConfig.isUseSchemaPrefix()) {
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())) {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
} else if (DbType.Oracle.name().equals(selectedDatabaseConfig.getDbType())) {
} else if (DbType.Oracle.name().equals(dbType)) {
//Oracle的schema为用户名,如果连接用户拥有dba等高级权限,若不设schema,会导致把其他用户下同名的表也生成一遍导致mapper中代码重复
tableConfig.setSchema(selectedDatabaseConfig.getUsername());
} else {
tableConfig.setCatalog(selectedDatabaseConfig.getSchema());
}
}
// 针对 postgresql 单独配置
if (DbType.valueOf(selectedDatabaseConfig.getDbType()).getDriverClass() == "org.postgresql.Driver") {
if (DbType.PostgreSQL.name().equals(dbType)) {
tableConfig.setDelimitIdentifiers(true);
}

//添加GeneratedKey主键生成
if (StringUtils.isNoneEmpty(generatorConfig.getGenerateKeys())) {
String dbType = selectedDatabaseConfig.getDbType();
if (DbType.MySQL.name().equals(dbType)) {
dbType = "JDBC";
if (StringUtils.isNotEmpty(generatorConfig.getGenerateKeys())) {
String dbType2 = dbType;
if (DbType.MySQL.name().equals(dbType2) || DbType.MySQL_8.name().equals(dbType)) {
dbType2 = "JDBC";
//dbType为JDBC,且配置中开启useGeneratedKeys时,Mybatis会使用Jdbc3KeyGenerator,
//使用该KeyGenerator的好处就是直接在一次INSERT 语句内,通过resultSet获取得到 生成的主键值,
//并很好的支持设置了读写分离代理的数据库
Expand All @@ -109,7 +110,7 @@ public void generate() throws Exception {
//当使用SelectKey时,Mybatis会使用SelectKeyGenerator,INSERT之后,多发送一次查询语句,获得主键值
//在上述读写分离被代理的情况下,会得不到正确的主键
}
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType, true, null));
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType2, true, null));
}

if (generatorConfig.getMapperName() != null) {
Expand All @@ -136,10 +137,10 @@ public void generate() throws Exception {

JDBCConnectionConfiguration jdbcConfig = new JDBCConnectionConfiguration();
// http://www.mybatis.org/generator/usage/mysql.html
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())) {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
jdbcConfig.addProperty("nullCatalogMeansCurrent", "true");
}
jdbcConfig.setDriverClass(DbType.valueOf(selectedDatabaseConfig.getDbType()).getDriverClass());
jdbcConfig.setDriverClass(DbType.valueOf(dbType).getDriverClass());
jdbcConfig.setConnectionURL(DbUtil.getConnectionUrlWithSchema(selectedDatabaseConfig));
jdbcConfig.setUserId(selectedDatabaseConfig.getUsername());
jdbcConfig.setPassword(selectedDatabaseConfig.getPassword());
Expand Down Expand Up @@ -196,8 +197,8 @@ public void generate() throws Exception {
}
// limit/offset插件
if (generatorConfig.isOffsetLimit()) {
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())
|| DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) {
if (DbType.MySQL.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.MySQLLimitPlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.MySQLLimitPlugin");
Expand All @@ -212,8 +213,8 @@ public void generate() throws Exception {
}
//forUpdate 插件
if(generatorConfig.isNeedForUpdate()) {
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())
|| DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) {
if (DbType.MySQL.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.MySQLForUpdatePlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.MySQLForUpdatePlugin");
Expand All @@ -222,17 +223,17 @@ public void generate() throws Exception {
}
//repository 插件
if(generatorConfig.isAnnotationDAO()) {
if (DbType.MySQL.name().equals(selectedDatabaseConfig.getDbType())
|| DbType.PostgreSQL.name().equals(selectedDatabaseConfig.getDbType())) {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
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())) {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("useExample", String.valueOf(generatorConfig.isUseExample()));
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public void setGeneratorConfigIntoUI(GeneratorConfig generatorConfig) {
mapperTargetPackage.setText(generatorConfig.getMappingXMLPackage());
mappingTargetProject.setText(generatorConfig.getMappingXMLTargetFolder());
encodingChoice.setValue(generatorConfig.getEncoding());
useExample.setSelected(generatorConfig.isUseExample());
}

@FXML
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/zzg/mybatis/generator/model/DbType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public enum DbType {

MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-5.1.38.jar"),
MySQL_8("com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=%s", "mysql-connector-java-8.0.11.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"),
SQL_Server("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://%s:%s;databaseName=%s", "sqljdbc4-4.0.jar"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class CommonDAOInterfacePlugin extends PluginAdapter {
private List<Method> methods = new ArrayList<>();

private ShellCallback shellCallback = null;
private boolean useExample;

public CommonDAOInterfacePlugin() {
shellCallback = new DefaultShellCallback(false);
this.useExample = "true".equals(getProperties().getProperty("useExample"));
}

private boolean isUseExample() {
return "true".equals(getProperties().getProperty("useExample"));
}

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
Expand All @@ -53,15 +55,15 @@ public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTa
mapperInterface.addJavaDocLine(" * " + "DAO公共基类,由MybatisGenerator自动生成请勿修改");
mapperInterface.addJavaDocLine(" * " + "@param <Model> The Model Class 这里是泛型不是Model类");
mapperInterface.addJavaDocLine(" * " + "@param <PK> The Primary Key Class 如果是无主键,则可以用Model来跳过,如果是多主键则是Key类");
if (useExample) {
if (isUseExample()) {
mapperInterface.addJavaDocLine(" * " + "@param <E> The Example Class");
}
mapperInterface.addJavaDocLine(" */");

FullyQualifiedJavaType daoBaseInterfaceJavaType = mapperInterface.getType();
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("Model"));
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("PK extends Serializable"));
if (useExample) {
if (isUseExample()) {
daoBaseInterfaceJavaType.addTypeArgument(new FullyQualifiedJavaType("E"));
}

Expand Down Expand Up @@ -122,7 +124,7 @@ public boolean clientGenerated(Interface interfaze,
daoSuperType.addTypeArgument(primaryKeyTypeJavaType);
interfaze.addImportedType(primaryKeyTypeJavaType);

if (useExample) {
if (isUseExample()) {
String exampleType = introspectedTable.getExampleType();
FullyQualifiedJavaType exampleTypeJavaType = new FullyQualifiedJavaType(exampleType);
daoSuperType.addTypeArgument(exampleTypeJavaType);
Expand All @@ -140,7 +142,7 @@ public boolean validate(List<String> list) {
}

private void interceptExampleParam(Method method) {
if (useExample) {
if (isUseExample()) {
method.getParameters().clear();
method.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "example"));
methods.add(method);
Expand All @@ -160,7 +162,7 @@ private void interceptModelParam(Method method) {
}

private void interceptModelAndExampleParam(Method method) {
if (useExample) {
if (isUseExample()) {
List<Parameter> parameters = method.getParameters();
if (parameters.size() == 1) {
interceptExampleParam(method);
Expand All @@ -182,7 +184,7 @@ private void interceptModelAndExampleParam(Method method) {
public boolean clientCountByExampleMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
// interface
if (useExample) {
if (isUseExample()) {
interceptExampleParam(method);
}
return false;
Expand All @@ -192,7 +194,7 @@ public boolean clientCountByExampleMethodGenerated(Method method,
@Override
public boolean clientDeleteByExampleMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptExampleParam(method);
}
return false;
Expand All @@ -216,7 +218,7 @@ public boolean clientInsertMethodGenerated(Method method, Interface interfaze,
@Override
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
}
Expand All @@ -226,7 +228,7 @@ public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
@Override
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptExampleParam(method);
method.setReturnType(new FullyQualifiedJavaType("List<Model>"));
}
Expand All @@ -244,7 +246,7 @@ public boolean clientSelectByPrimaryKeyMethodGenerated(Method method,
@Override
public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptModelAndExampleParam(method);
}
return false;
Expand All @@ -253,7 +255,7 @@ public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method,
@Override
public boolean clientUpdateByExampleWithBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptModelAndExampleParam(method);
}
return false;
Expand All @@ -262,7 +264,7 @@ public boolean clientUpdateByExampleWithBLOBsMethodGenerated(Method method,
@Override
public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method,
Interface interfaze, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptModelAndExampleParam(method);
}
return false;
Expand All @@ -277,15 +279,15 @@ public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method,

@Override
public boolean clientUpdateByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptModelAndExampleParam(method);
}
return false;
}

@Override
public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (useExample) {
if (isUseExample()) {
interceptModelAndExampleParam(method);
}
return false;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fxml/newConnection.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="MySQL" />
<String fx:value="MySQL_8" />
<String fx:value="Oracle" />
<String fx:value="PostgreSQL" />
<String fx:value="SQL_Server" />
Expand Down
Binary file not shown.

0 comments on commit 49a33e4

Please sign in to comment.