From b9e4f0da15bf274baaa5abb574abb10a7c47fa86 Mon Sep 17 00:00:00 2001 From: lsy <18332763730@163.com> Date: Thu, 30 Nov 2017 22:42:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84mybatis-generator-extend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/extend/db/JdbcConnection.java | 25 +++++- .../generator/extend/pojo/DatabaseConfig.java | 4 - .../mybatis/generator/extend/pojo/Db.java | 59 +++++++++++++ .../extend/pojo/MybatisGenerator.java | 49 +++++++++++ .../extend/pojo/MybatisGeneratorConfig.java | 4 - .../service/MybatisGeneratorExtend.java | 82 +++++++++++++++++++ .../resources/templates/generatorConfig.fm | 3 +- 7 files changed, 216 insertions(+), 10 deletions(-) delete mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/DatabaseConfig.java create mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java create mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java delete mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGeneratorConfig.java create mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/db/JdbcConnection.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/db/JdbcConnection.java index cfb5fa8..d1f4464 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/db/JdbcConnection.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/db/JdbcConnection.java @@ -1,18 +1,41 @@ package com.micro.fast.mybatis.generator.extend.db; +import com.micro.fast.mybatis.generator.extend.util.PropertiesUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + + public class JdbcConnection { private static final Logger log = LoggerFactory.getLogger(JdbcConnection.class); + private static Connection connection = null; + //加载数据库驱动获取连接 static { try { - Class.forName("com.mysql.jdbc.Driver"); + Class.forName(PropertiesUtil.getProperty("db.driverClassName")); + connection = DriverManager.getConnection( + PropertiesUtil.getProperty("db.url") + , PropertiesUtil.getProperty("db.username"), + PropertiesUtil.getProperty("db.password")); } catch (ClassNotFoundException e) { log.error("错误{} {}",e,"jdbc驱动加载失败"); e.printStackTrace(); + } catch (SQLException e) { + log.error("错误{} {}",e,"数据库连接失败"); + e.printStackTrace(); } } + /** + * 获取数据路链接 + * @return + */ + public static Connection getJdbcConection(){ + return JdbcConnection.connection; + } + } diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/DatabaseConfig.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/DatabaseConfig.java deleted file mode 100644 index 5d56092..0000000 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/DatabaseConfig.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.micro.fast.mybatis.generator.extend.pojo; - -public class DatabaseConfig { -} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java new file mode 100644 index 0000000..ab7d2c1 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java @@ -0,0 +1,59 @@ +package com.micro.fast.mybatis.generator.extend.pojo; + +import java.io.Serializable; + +/** + * mybatisGenerator所需要的数据库连接配置 + */ +public class Db { + + private String driverLocation; + + private String url; + + private String username; + + private String password; + + private String driverClassName; + + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + public String getDriverLocation() { + return driverLocation; + } + + public void setDriverLocation(String driverLocation) { + this.driverLocation = driverLocation; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java new file mode 100644 index 0000000..3289eb2 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java @@ -0,0 +1,49 @@ +package com.micro.fast.mybatis.generator.extend.pojo; + +import java.util.List; +import java.util.Map; + +/** + * mybatisGenerator的配置参数对象 + */ +public class MybatisGenerator { + private String pojoPackage; + + private String daoPackage; + + private String tablesPrefix; + + private Map tablesNameAndDoMainObjectsName; + + public Map getTablesNameAndDoMainObjectsName() { + return tablesNameAndDoMainObjectsName; + } + + public void setTablesNameAndDoMainObjectsName(Map tablesNameAndDoMainObjectsName) { + this.tablesNameAndDoMainObjectsName = tablesNameAndDoMainObjectsName; + } + + public String getPojoPackage() { + return pojoPackage; + } + + public void setPojoPackage(String pojoPackage) { + this.pojoPackage = pojoPackage; + } + + public String getDaoPackage() { + return daoPackage; + } + + public void setDaoPackage(String daoPackage) { + this.daoPackage = daoPackage; + } + + public String getTablesPrefix() { + return tablesPrefix; + } + + public void setTablesPrefix(String tablesPrefix) { + this.tablesPrefix = tablesPrefix; + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGeneratorConfig.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGeneratorConfig.java deleted file mode 100644 index e1ca710..0000000 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGeneratorConfig.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.micro.fast.mybatis.generator.extend.pojo; - -public class MybatisGeneratorConfig { -} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java new file mode 100644 index 0000000..552656f --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java @@ -0,0 +1,82 @@ +package com.micro.fast.mybatis.generator.extend.service; + +import com.micro.fast.mybatis.generator.extend.db.JdbcConnection; +import com.micro.fast.mybatis.generator.extend.pojo.Db; +import com.micro.fast.mybatis.generator.extend.pojo.MybatisGenerator; +import com.micro.fast.mybatis.generator.extend.util.PropertiesUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + + +public class MybatisGeneratorExtend { + private static final Logger log = LoggerFactory.getLogger(MybatisGeneratorExtend.class); + + private static Db db; + + private static MybatisGenerator mybatisGenerator; + + //初始化参数 + static{ + MybatisGeneratorExtend.db.setDriverLocation(PropertiesUtil.getProperty("db.driverLocation")); + MybatisGeneratorExtend.db.setUrl(PropertiesUtil.getProperty("db.url")); + MybatisGeneratorExtend.db.setUsername(PropertiesUtil.getProperty("db.username")); + MybatisGeneratorExtend.db.setPassword(PropertiesUtil.getProperty("db.password")); + MybatisGeneratorExtend.db.setDriverClassName(PropertiesUtil.getProperty("db.driverClassName")); + MybatisGeneratorExtend.mybatisGenerator.setPojoPackage( + PropertiesUtil.getProperty("mybatisGenerator.pojoPackage")); + MybatisGeneratorExtend.mybatisGenerator.setDaoPackage( + PropertiesUtil.getProperty("mybatisGenerator.daoPackage")); + MybatisGeneratorExtend.mybatisGenerator.setTablesPrefix( + PropertiesUtil.getProperty("mybatisGenerator.tablesPrefix")); + } + + public static void GenerateMybatisGeneratorNeedFilesByTemplates(){ + Connection jdbcConection = JdbcConnection.getJdbcConection(); + Statement statement = null; + ResultSet resultSet = null; + try { + statement = jdbcConection.createStatement(); + String sql = "show tables"; + resultSet = statement.executeQuery(sql); + Map tablesNameAndDoMainObjectsName = new HashMap(); + while(resultSet.next()){ + String tableName = resultSet.getString(1); + if (tableName.startsWith(mybatisGenerator.getTablesPrefix())){ + String doMainObjectName = null; + String[] strings = tableName.split("_"); + for (String string : strings) { + char c = string.charAt(0); + String s = String.valueOf(c); + String upperCase = s.toUpperCase(); + string.replaceFirst(s,upperCase); + doMainObjectName += string; + } + tablesNameAndDoMainObjectsName.put(tableName,doMainObjectName); + } + } + } catch (SQLException e) { + log.error("错误{} {}",e,"sql语句执行失败"); + e.printStackTrace(); + }finally { + try { + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + try { + resultSet.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm index c463896..7ea5e0e 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm @@ -66,7 +66,8 @@ - + <#list mybatisGenerator.tablesName as > +