完善mybatis-generator-extend
This commit is contained in:
+24
-1
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
package com.micro.fast.mybatis.generator.extend.pojo;
|
||||
|
||||
public class DatabaseConfig {
|
||||
}
|
||||
+59
@@ -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;
|
||||
}
|
||||
}
|
||||
+49
@@ -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<String,String> tablesNameAndDoMainObjectsName;
|
||||
|
||||
public Map<String, String> getTablesNameAndDoMainObjectsName() {
|
||||
return tablesNameAndDoMainObjectsName;
|
||||
}
|
||||
|
||||
public void setTablesNameAndDoMainObjectsName(Map<String, String> 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;
|
||||
}
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
package com.micro.fast.mybatis.generator.extend.pojo;
|
||||
|
||||
public class MybatisGeneratorConfig {
|
||||
}
|
||||
+82
@@ -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<String,String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -66,7 +66,8 @@
|
||||
<!-- enableSubPackages:是否让schema作为包的后缀 -->
|
||||
<property name="enableSubPackages" value="false" />
|
||||
</javaClientGenerator>
|
||||
|
||||
<#list mybatisGenerator.tablesName as >
|
||||
</#list>
|
||||
<table tableName="upms_log" domainObjectName="UpmsLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
|
||||
<table tableName="upms_organization" domainObjectName="UpmsOrganization" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
|
||||
<table tableName="upms_permission" domainObjectName="UpmsPermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
|
||||
|
||||
Reference in New Issue
Block a user