完善mybatis-generator-extend插件
This commit is contained in:
+103
@@ -0,0 +1,103 @@
|
||||
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.FreemarkerUtil;
|
||||
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;
|
||||
|
||||
/**
|
||||
* mybatis-generate-extend-maven-plugin主要逻辑
|
||||
*/
|
||||
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"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模板引擎生成mybatis-generator需要的文件
|
||||
*/
|
||||
public static void GenerateMybatisGeneratorNeedFilesByTemplates() {
|
||||
Connection jdbcConection = JdbcConnection.getJdbcConection();
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
//1.创建语句执行sql
|
||||
statement = jdbcConection.createStatement();
|
||||
String sql = "show tables";
|
||||
resultSet = statement.executeQuery(sql);
|
||||
//2.处理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 firstUpper = string.replaceFirst(s, upperCase);
|
||||
doMainObjectName += firstUpper;
|
||||
}
|
||||
char c = tableName.charAt(0);
|
||||
String s = String.valueOf(c);
|
||||
String upperCase = s.toUpperCase();
|
||||
String firstUpperDoMainObjectName = doMainObjectName.replaceFirst(s, upperCase);
|
||||
tablesNameAndDoMainObjectsName.put(tableName,firstUpperDoMainObjectName);
|
||||
}
|
||||
}
|
||||
MybatisGeneratorExtend.mybatisGenerator.setTablesNameAndDoMainObjectsName(tablesNameAndDoMainObjectsName);
|
||||
//创建模板mybatis-generator-extend模板文件
|
||||
Map<String,Object> mybatisGeneratorModel = new HashMap<>();
|
||||
mybatisGeneratorModel.put("db",MybatisGeneratorExtend.db);
|
||||
mybatisGeneratorModel.put("mybatisGenerator",MybatisGeneratorExtend.mybatisGenerator);
|
||||
boolean mybatisGeneratorFileSuccess = FreemarkerUtil.createFile("mybatisGenerator.fm", mybatisGeneratorModel, "mybatisGenerator.xml");
|
||||
boolean generatorConfigFileSuccess = FreemarkerUtil.createFile("generatorConfig.fm", mybatisGeneratorModel, "generatorConfig.xml");
|
||||
} catch (SQLException e) {
|
||||
log.error("错误{} {}", e, "sql语句执行失败");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//3.关闭流
|
||||
try {
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
-84
@@ -1,84 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* mybatis-generate-extend-maven-plugin主要逻辑
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user