【fix sonar】generater-core文件夹

This commit is contained in:
梁琦涛
2023-03-14 11:25:54 +08:00
parent 921e9d20bd
commit c37787bd8e
19 changed files with 140 additions and 214 deletions
@@ -3,7 +3,7 @@ package com.jero.codegenerate.database;
import com.jero.codegenerate.properties.CodeConfigProperties;
public class CodegenDatasourceConfig {
public CodegenDatasourceConfig() {
private CodegenDatasourceConfig() {
}
/**
* 加载配置
@@ -20,4 +20,4 @@ public class CodegenDatasourceConfig {
CodeConfigProperties.username = username;
CodeConfigProperties.password = password;
}
}
}
@@ -22,29 +22,30 @@ import com.jero.codegenerate.generate.util.TableConvert;
@Slf4j
public class DbReadTableUtil {
private static Connection connection;
private static Statement statement;
public DbReadTableUtil() {
private DbReadTableUtil() {
}
private static final String CONNECT_DATABASE_NAME = " connect databaseName : ";
private static final String COLUMN_GET_FIELD_NAME = "columnt.getFieldName() -------------";
public static void main(String[] args) throws SQLException {
try {
List list = listColumns("demo");
Iterator iterator = list.iterator();
List<ColumnVo> list = listColumns("demo");
while(iterator.hasNext()) {
ColumnVo columnVo = (ColumnVo)iterator.next();
System.out.println(columnVo.getFieldName());
for (ColumnVo columnVo : list) {
log.info(columnVo.getFieldName());
}
} catch (Exception e) {
e.printStackTrace();
}
PrintStream printStream = System.out;
new DbReadTableUtil();
printStream.println(ArrayUtils.toString(listTableName()));
log.info(ArrayUtils.toString(listTableName()));
}
/**
* 获取所有表名的集合
@@ -54,7 +55,7 @@ public class DbReadTableUtil {
*/
public static List<String> listTableName() throws SQLException {
String sqlStr = null;
ArrayList list = new ArrayList(0);
ArrayList<String> list = new ArrayList<>(0);
try {
Class.forName(CodeConfigProperties.driverName);
@@ -62,7 +63,7 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
// 表格所属的库
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
log.info(CONNECT_DATABASE_NAME + catalog);
if (CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询所有表的sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLTABLES_SQL, TableConvert.formatStr(catalog));
@@ -94,16 +95,14 @@ public class DbReadTableUtil {
if (statement != null) {
statement.close();
statement = null;
System.gc();
}
if (connection != null) {
connection.close();
connection = null;
System.gc();
}
} catch (SQLException e) {
throw e;
e.printStackTrace();
}
}
@@ -118,7 +117,7 @@ public class DbReadTableUtil {
*/
public static List<ColumnVo> listColumns(String tableName) throws Exception {
String sqlStr = null;
ArrayList list = new ArrayList();
ArrayList<ColumnVo> list = new ArrayList<>();
int row;
try {
@@ -127,7 +126,7 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
// 表格所属的库
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
log.info(CONNECT_DATABASE_NAME + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(catalog));
@@ -174,7 +173,7 @@ public class DbReadTableUtil {
columnVo.setNullable(TableConvert.getNullable(resultSet.getString(7)));
setupColumnVo(columnVo);
columnVo.setFiledComment(StringUtils.isBlank(resultSet.getString(3)) ? columnVo.getFieldName() : resultSet.getString(3));
log.debug("columnt.getFieldName() -------------" + columnVo.getFieldName());
log.debug(COLUMN_GET_FIELD_NAME + columnVo.getFieldName());
String[] pageFilterFieldsStrings = new String[0];
if (com.jero.codegenerate.properties.CodeConfigProperties.pageFilterFields != null) {
pageFilterFieldsStrings = com.jero.codegenerate.properties.CodeConfigProperties.pageFilterFields.toLowerCase().split(",");
@@ -193,7 +192,7 @@ public class DbReadTableUtil {
}
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
log.debug("columnt.getFieldName() -------------" + columnVo1.getFieldName());
log.debug(COLUMN_GET_FIELD_NAME + columnVo1.getFieldName());
if (!com.jero.codegenerate.properties.CodeConfigProperties.dbTableId.equals(columnVo1.getFieldName()) && !CodeStringUtils.isPageFilterFieldsContainDbName(columnVo1.getFieldDbName().toLowerCase(), pageFilterFieldsStrings)) {
columnVo1.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
columnVo1.setFieldDbType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
@@ -209,33 +208,29 @@ public class DbReadTableUtil {
}
log.debug("读取表成功");
} catch (ClassNotFoundException e) {
throw e;
} catch (SQLException e) {
} catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
try {
if (statement != null) {
statement.close();
statement = null;
System.gc();
}
if (connection != null) {
connection.close();
connection = null;
System.gc();
}
} catch (SQLException e) {
throw e;
e.printStackTrace();
}
}
ArrayList result = new ArrayList();
ArrayList<ColumnVo> result = new ArrayList<>();
for(row = list.size() - 1; row >= 0; --row) {
ColumnVo columnVo = (ColumnVo)list.get(row);
ColumnVo columnVo = list.get(row);
result.add(columnVo);
}
@@ -248,9 +243,9 @@ public class DbReadTableUtil {
* @return java.util.List<com.jero.codegenerate.generate.pojo.ColumnVo>
*/
public static List<ColumnVo> listOriginalColumns(String tableName) throws Exception {
ResultSet resultSet = null;
ResultSet resultSet;
String sqlStr = null;
ArrayList list = new ArrayList();
ArrayList<ColumnVo> list = new ArrayList<>();
int row;
try {
@@ -259,7 +254,7 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
// 表格所属的库
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
log.info(CONNECT_DATABASE_NAME + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(catalog));
@@ -305,7 +300,7 @@ public class DbReadTableUtil {
columnVo.setFieldDbType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
setupColumnVo(columnVo);
columnVo.setFiledComment(StringUtils.isBlank(resultSet.getString(3)) ? columnVo.getFieldName() : resultSet.getString(3));
log.debug("columnt.getFieldName() -------------" + columnVo.getFieldName());
log.debug(COLUMN_GET_FIELD_NAME + columnVo.getFieldName());
list.add(columnVo);
while(true) {
@@ -332,33 +327,29 @@ public class DbReadTableUtil {
columnVo1.setFiledComment(StringUtils.isBlank(resultSet.getString(3)) ? columnVo1.getFieldName() : resultSet.getString(3));
list.add(columnVo1);
}
} catch (ClassNotFoundException e) {
throw e;
} catch (SQLException e) {
} catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
try {
if (statement != null) {
statement.close();
statement = null;
System.gc();
}
if (connection != null) {
connection.close();
connection = null;
System.gc();
}
} catch (SQLException e) {
throw e;
e.printStackTrace();
}
}
ArrayList result = new ArrayList();
ArrayList<ColumnVo> result = new ArrayList<>();
for(row = list.size() - 1; row >= 0; --row) {
ColumnVo columnVo = (ColumnVo)list.get(row);
ColumnVo columnVo = list.get(row);
result.add(columnVo);
}
@@ -380,7 +371,7 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
// 表格所属的库
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
log.info(CONNECT_DATABASE_NAME + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql获取表的列信息sql
sqlStr = "select column_name,data_type,column_comment,0,0 from information_schema.columns where table_name = '" + tableName + "' and table_schema = '" + catalog + "'";
@@ -500,6 +491,7 @@ public class DbReadTableUtil {
* @return java.lang.String
*/
private static String getFieldType(String value, String precision, String scale) {
String bigDecimal = "java.math.BigDecimal";
if (value.contains("char")) {
value = "java.lang.String";
} else if (value.contains("int")) {
@@ -510,14 +502,14 @@ public class DbReadTableUtil {
value = "java.lang.Double";
} else if (value.contains("number")) {
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
value = "java.math.BigDecimal";
value = bigDecimal;
} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
value = "java.lang.Long";
} else {
value = "java.lang.Integer";
}
} else if (value.contains("decimal")) {
value = "java.math.BigDecimal";
value = bigDecimal;
} else if (value.contains("date")) {
value = "java.util.Date";
} else if (value.contains("time")) {
@@ -527,7 +519,7 @@ public class DbReadTableUtil {
} else if (value.contains("clob")) {
value = "java.sql.Clob";
} else if (value.contains("numeric")) {
value = "java.math.BigDecimal";
value = bigDecimal;
} else {
value = "java.lang.Object";
}
@@ -4,7 +4,7 @@ import java.util.List;
import org.apache.commons.lang.StringUtils;
public class CodeStringUtils {
public CodeStringUtils() {
private CodeStringUtils() {
}
/**
@@ -14,7 +14,7 @@ public class CodeStringUtils {
* @return java.lang.String
*/
public static String joinComma(String[] stringArray) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
String[] array = stringArray;
int length = stringArray.length;
@@ -82,7 +82,7 @@ public class CodeStringUtils {
public static boolean isListContainString(String target, List<String> stringList) {
String[] stringArray = new String[0];
if (stringList != null) {
stringArray = (String[])((String[])stringList.toArray());
stringArray = stringList.toArray(new String[0]);
}
if (stringArray != null && stringArray.length != 0) {
@@ -1,18 +1,18 @@
package com.jero.codegenerate.database.util;
public interface DbConvertDef {
String Y = "Y";
String N = "N";
String MYSQL = "mysql";
String ORACLE = "oracle";
String SQLSERVER = "sqlserver";
String POSTGRESQL = "postgresql";
String MYSQL_ALLCOLUMNS_SQL = "select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION";
String ORACLE_ALLCOLUMNS_SQL = " select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable inner join user_col_comments commentstable on colstable.column_name = commentstable.column_name where colstable.table_name = commentstable.table_name and colstable.table_name = {0}";
String SQLSERVER_ALLCOLUMNS_SQL = "select distinct cast(a.name as varchar(50)) column_name, cast(b.name as varchar(50)) data_type, cast(e.value as NVARCHAR(200)) comment, cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision, cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale, a.max_length, (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0} order by name) c on a.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0} order by a.column_id";
String POSTGRESQL_ALLCOLUMNS_SQL = "select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname FROM\tpg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name";
String MYSQL_ALLTABLES_SQL = "select distinct table_name from information_schema.columns where table_schema = {0}";
String ORACLE_ALLTABLES_SQL = "select distinct colstable.table_name as table_name from user_tab_cols colstable order by colstable.table_name";
String SQLSERVER_ALLTABLES_SQL = "select distinct c.name as table_name from sys.objects c where c.type = 'U' ";
String POSTGRESQL_ALLTABLES_SQL = "select tablename from pg_tables where schemaname='public'";
public class DbConvertDef {
public static final String Y = "Y";
public static final String N = "N";
public static final String MYSQL = "mysql";
public static final String ORACLE = "oracle";
public static final String SQLSERVER = "sqlserver";
public static final String POSTGRESQL = "postgresql";
public static final String MYSQL_ALLCOLUMNS_SQL = "select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION";
public static final String ORACLE_ALLCOLUMNS_SQL = " select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable inner join user_col_comments commentstable on colstable.column_name = commentstable.column_name where colstable.table_name = commentstable.table_name and colstable.table_name = {0}";
public static final String SQLSERVER_ALLCOLUMNS_SQL = "select distinct cast(a.name as varchar(50)) column_name, cast(b.name as varchar(50)) data_type, cast(e.value as NVARCHAR(200)) comment, cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision, cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale, a.max_length, (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0} order by name) c on a.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0} order by a.column_id";
public static final String POSTGRESQL_ALLCOLUMNS_SQL = "select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname FROM\tpg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name";
public static final String MYSQL_ALLTABLES_SQL = "select distinct table_name from information_schema.columns where table_schema = {0}";
public static final String ORACLE_ALLTABLES_SQL = "select distinct colstable.table_name as table_name from user_tab_cols colstable order by colstable.table_name";
public static final String SQLSERVER_ALLTABLES_SQL = "select distinct c.name as table_name from sys.objects c where c.type = 'U' ";
public static final String POSTGRESQL_ALLTABLES_SQL = "select tablename from pg_tables where schemaname='public'";
}
@@ -12,7 +12,7 @@ import java.util.List;
@Slf4j
public class CreateFileConfig {
private String templatePath;
private List<File> templateRootDirs = new ArrayList();
private List<File> templateRootDirs = new ArrayList<>();
private String stylePath;
public CreateFileConfig(String templatePath) {
@@ -65,7 +65,7 @@ public class CreateFileConfig {
public List<File> listTemplateRootDirs() throws UnsupportedEncodingException {
String file = this.getClass().getResource(this.templatePath).getFile();
// 对中文路径进行处理
file = URLDecoder.decode(file.replaceAll("%20", " "), "UTF-8");
file = URLDecoder.decode(file.replace("%20", " "), "UTF-8");
log.debug("-------classpath-------" + file);
if (file.indexOf("/BOOT-INF/classes!") != -1 || file.indexOf("/BOOT-INF/lib/") != -1) {
@@ -97,4 +97,4 @@ public class CreateFileConfig {
sb.append("\"} ");
return sb.toString();
}
}
}
@@ -5,6 +5,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.jero.codegenerate.properties.CodeConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import com.jero.codegenerate.database.DbReadTableUtil;
@@ -33,7 +34,7 @@ public class CodeGenerateOne extends BaseCodeGenerate implements IGenerate {
@Override
public Map<String, Object> getCodeGenerateConfig() throws Exception {
HashMap var1 = new HashMap();
HashMap<String, Object> var1 = new HashMap<>();
var1.put("bussiPackage", com.jero.codegenerate.properties.CodeConfigProperties.packageName);
var1.put("entityPackage", this.tableVo.getEntityPackage());
var1.put("entityName", this.tableVo.getEntityName());
@@ -54,12 +55,12 @@ public class CodeGenerateOne extends BaseCodeGenerate implements IGenerate {
var1.put("tableVo", this.tableVo);
try {
if (this.columns == null || this.columns.size() == 0) {
if (this.columns == null || this.columns.isEmpty()) {
this.columns = DbReadTableUtil.listColumns(this.tableVo.getTableName());
}
var1.put("columns", this.columns);
if (this.originalColumns == null || this.originalColumns.size() == 0) {
if (this.originalColumns == null || this.originalColumns.isEmpty()) {
this.originalColumns = DbReadTableUtil.listOriginalColumns(this.tableVo.getTableName());
}
@@ -68,7 +69,7 @@ public class CodeGenerateOne extends BaseCodeGenerate implements IGenerate {
while(iterator.hasNext()) {
ColumnVo var3 = (ColumnVo)iterator.next();
if (var3.getFieldName().toLowerCase().equals(com.jero.codegenerate.properties.CodeConfigProperties.dbTableId.toLowerCase())) {
if (var3.getFieldName().equalsIgnoreCase(CodeConfigProperties.dbTableId)) {
var1.put("primaryKeyPolicy", var3.getFieldType());
}
}
@@ -86,7 +87,7 @@ public class CodeGenerateOne extends BaseCodeGenerate implements IGenerate {
public List<String> generateCodeFile(String stylePath) throws Exception {
log.debug("----jero---Code----Generation----[单表模型:" + this.tableVo.getTableName() + "]------- 生成中。。。");
String projectPath = com.jero.codegenerate.properties.CodeConfigProperties.projectPath;
Map codeGenerateConfig = this.getCodeGenerateConfig();
Map<String, Object> codeGenerateConfig = this.getCodeGenerateConfig();
String templateUrl = com.jero.codegenerate.properties.CodeConfigProperties.templateUrl;
if (dealAndGetTemplateUrl(templateUrl, "/").equals("jero/code-template")) {
templateUrl = "/" + dealAndGetTemplateUrl(templateUrl, "/") + "/one";
@@ -6,6 +6,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.jero.codegenerate.properties.CodeConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import com.jero.codegenerate.database.DbReadTableUtil;
@@ -19,16 +20,12 @@ import com.jero.codegenerate.generate.util.NonceUtils;
@Slf4j
public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate {
private static String f;
public static String A = "A";
public static String B = "B";
// 主表vo
private MainTableVo mainTableVo;
private List<ColumnVo> mainColumns;
private List<ColumnVo> originalMainColumns;
// 从表集合
private List<SubTableVo> subTables;
private static DbReadTableUtil dbReadTableUtil = new DbReadTableUtil();
public CodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
this.subTables = subTables;
@@ -49,7 +46,7 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
*/
@Override
public Map<String, Object> getCodeGenerateConfig() throws Exception {
HashMap hashMap = new HashMap();
HashMap<String,Object> hashMap = new HashMap<>();
// 包名
hashMap.put("bussiPackage", com.jero.codegenerate.properties.CodeConfigProperties.packageName);
// 实体包名
@@ -76,11 +73,11 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
hashMap.put("tableVo", this.mainTableVo);
try {
if (this.mainColumns == null || this.mainColumns.size() == 0) {
if (this.mainColumns == null || this.mainColumns.isEmpty()) {
this.mainColumns = DbReadTableUtil.listColumns(this.mainTableVo.getTableName());
}
if (this.originalMainColumns == null || this.originalMainColumns.size() == 0) {
if (this.originalMainColumns == null || this.originalMainColumns.isEmpty()) {
this.originalMainColumns = DbReadTableUtil.listOriginalColumns(this.mainTableVo.getTableName());
}
@@ -91,7 +88,7 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
while(iterator.hasNext()) {
ColumnVo columnVo = (ColumnVo)iterator.next();
// 主键类型
if (columnVo.getFieldName().toLowerCase().equals(com.jero.codegenerate.properties.CodeConfigProperties.dbTableId.toLowerCase())) {
if (columnVo.getFieldName().equalsIgnoreCase(CodeConfigProperties.dbTableId)) {
hashMap.put("primaryKeyPolicy", columnVo.getFieldType());
}
}
@@ -101,20 +98,20 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
while(iterator.hasNext()) {
// 从表(局部变量)
SubTableVo subTableVo = (SubTableVo)iterator.next();
List originalColumns;
if (subTableVo.getColums() == null || subTableVo.getColums().size() == 0) {
List<ColumnVo> originalColumns;
if (subTableVo.getColums() == null || subTableVo.getColums().isEmpty()) {
originalColumns = DbReadTableUtil.listColumns(subTableVo.getTableName());
subTableVo.setColums(originalColumns);
}
if (subTableVo.getOriginalColumns() == null || subTableVo.getOriginalColumns().size() == 0) {
if (subTableVo.getOriginalColumns() == null || subTableVo.getOriginalColumns().isEmpty()) {
originalColumns = DbReadTableUtil.listOriginalColumns(subTableVo.getTableName());
subTableVo.setOriginalColumns(originalColumns);
}
// 从表外键
String[] foreignKeys = subTableVo.getForeignKeys();
// 存储外键的集合
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
String[] foreignKeys1 = foreignKeys;
int length = foreignKeys.length;
@@ -123,7 +120,7 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
list.add(DbReadTableUtil.convertForeignKeyToCamelCase(foreignKey));
}
// put外键数组
subTableVo.setForeignKeys((String[])list.toArray(new String[0]));
subTableVo.setForeignKeys(list.toArray(new String[0]));
subTableVo.setOriginalForeignKeys(foreignKeys);
}
// put从表集合
@@ -141,7 +138,7 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
@Override
public List<String> generateCodeFile(String stylePath) throws Exception {
String projectPath = com.jero.codegenerate.properties.CodeConfigProperties.projectPath;
Map configMap = this.getCodeGenerateConfig();
Map<String, Object> configMap = this.getCodeGenerateConfig();
String templateUrl = com.jero.codegenerate.properties.CodeConfigProperties.templateUrl;
if (dealAndGetTemplateUrl(templateUrl, "/").equals("jero/code-template")) {
templateUrl = "/" + dealAndGetTemplateUrl(templateUrl, "/") + "/onetomany";
@@ -168,4 +165,4 @@ public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate
this.generateCodeFile(stylePath);
return this.messageList;
}
}
}
@@ -11,9 +11,11 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.jero.codegenerate.generate.config.CreateFileConfig;
import com.jero.codegenerate.generate.util.FileHelper;
@@ -24,10 +26,8 @@ import lombok.extern.slf4j.Slf4j;
public class BaseCodeGenerate {
protected static String characterSet = "UTF-8";
protected List<String> messageList = new ArrayList();
protected List<String> messageList = new ArrayList<>();
public BaseCodeGenerate() {
}
/**
* 生成项目文件
* @date 2021/4/2 14:32
@@ -41,7 +41,7 @@ public class BaseCodeGenerate {
for(int i = 0; i < createFileConfig.listTemplateRootDirs().size(); ++i) {
// 获取一个模板根目录
File templateRootDir = (File)createFileConfig.listTemplateRootDirs().get(i);
File templateRootDir = createFileConfig.listTemplateRootDirs().get(i);
this.createOutPutFile(projectPath, templateRootDir, configMap, createFileConfig);
}
@@ -63,13 +63,13 @@ public class BaseCodeGenerate {
} else {
log.info(" load template from templateRootDir = '" + templateRootDir.getAbsolutePath() + "',stylePath ='" + createFileConfig.getStylePath() + "', out GenerateRootDir:" + com.jero.codegenerate.properties.CodeConfigProperties.projectPath);
// 获取模板目录下的模板
List templateFileList = FileHelper.listFileAndSort(templateRootDir);
List<File> templateFileList = FileHelper.listFileAndSort(templateRootDir);
log.debug("----srcFiles----size-----------" + templateFileList.size());
log.debug("----srcFiles----list------------" + templateFileList.toString());
for(int i = 0; i < templateFileList.size(); ++i) {
// 获取一个模板文件
File srcFile = (File)templateFileList.get(i);
File srcFile = templateFileList.get(i);
this.createOutPutFile(projectPath, templateRootDir, configMap, srcFile, createFileConfig);
}
@@ -97,6 +97,9 @@ public class BaseCodeGenerate {
}
//模板文件目录
String outputFilepath = getTemplatePath(configMap, templateFilePath, createFileConfig);
if(Objects.isNull(outputFilepath)){
return;
}
log.debug("-------outputFilepath--" + outputFilepath);
String packageDir;
// 输出文件路径以 java 开头
@@ -182,18 +185,16 @@ public class BaseCodeGenerate {
* @return void
*/
protected void splitFileAndGenerate(File file, String str) {
InputStreamReader isr = null;
BufferedReader bf = null;
ArrayList list = new ArrayList();
ArrayList<OutputStreamWriter> list = new ArrayList<>();
boolean flag = false;
int i;
label341: {
label342: {
try {
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), characterSet);
BufferedReader bf = new BufferedReader(isr);){
flag = true;
isr = new InputStreamReader(new FileInputStream(file), characterSet);
bf = new BufferedReader(isr);
boolean b = false;
OutputStreamWriter osw = null;
@@ -214,16 +215,13 @@ public class BaseCodeGenerate {
this.messageList.add("生成成功:" + substring);
b = true;
} else if (b) {
osw.append(s + "\r\n");
osw.append(s).append("\r\n");
}
}
for(int j = 0; j < list.size(); ++j) {
((Writer)list.get(j)).close();
}
bf.close();
isr.close();
log.debug("[generate]\t delete file:" + file.getAbsolutePath());
// 删除文件
delFile(file);
@@ -240,15 +238,7 @@ public class BaseCodeGenerate {
} finally {
if (flag) {
try {
if (bf != null) {
bf.close();
}
if (isr != null) {
isr.close();
}
if (list.size() > 0) {
if (!list.isEmpty()) {
for(int var12 = 0; var12 < list.size(); ++var12) {
if (list.get(var12) != null) {
((Writer)list.get(var12)).close();
@@ -263,15 +253,8 @@ public class BaseCodeGenerate {
}
try {
if (bf != null) {
bf.close();
}
if (isr != null) {
isr.close();
}
if (list.size() > 0) {
if (!list.isEmpty()) {
for(i = 0; i < list.size(); ++i) {
if (list.get(i) != null) {
((Writer)list.get(i)).close();
@@ -286,15 +269,8 @@ public class BaseCodeGenerate {
}
try {
if (bf != null) {
bf.close();
}
if (isr != null) {
isr.close();
}
if (list.size() > 0) {
if (!list.isEmpty()) {
for(i = 0; i < list.size(); ++i) {
if (list.get(i) != null) {
((Writer)list.get(i)).close();
@@ -309,15 +285,7 @@ public class BaseCodeGenerate {
}
try {
if (bf != null) {
bf.close();
}
if (isr != null) {
isr.close();
}
if (list.size() > 0) {
if (!list.isEmpty()) {
for(i = 0; i < list.size(); ++i) {
if (list.get(i) != null) {
((Writer)list.get(i)).close();
@@ -339,7 +307,6 @@ public class BaseCodeGenerate {
*/
protected static String getTemplatePath(Map<String, Object> configMap, String templateFilePath, CreateFileConfig createFileConfig) throws Exception {
String templateFilePath1 = templateFilePath;
boolean flag = true;
int i;
// ascii 64 = @
if ((i = templateFilePath.indexOf(64)) != -1) {
@@ -348,7 +315,7 @@ public class BaseCodeGenerate {
String substring = templateFilePath.substring(i + 1);
Object o = configMap.get(substring);
if (o == null) {
System.err.println("[not-generate] WARN: test expression is null by key:[" + substring + "] on template:[" + templateFilePath + "]");
log.error("[not-generate] WARN: test expression is null by key:[" + substring + "] on template:[" + templateFilePath + "]");
return null;
}
@@ -361,7 +328,7 @@ public class BaseCodeGenerate {
Configuration configuration = FreemarkerHelper.getConfiguration(createFileConfig.listTemplateRootDirs(), characterSet, "/");
templateFilePath1 = FreemarkerHelper.getFillTemplateString(templateFilePath1, configMap, configuration);
String stylePath = createFileConfig.getStylePath();
if (stylePath != null && stylePath != "") {
if (stylePath != null && !"".equals(stylePath)) {
templateFilePath1 = templateFilePath1.substring(stylePath.length() + 1);
}
// 后缀名
@@ -376,11 +343,11 @@ public class BaseCodeGenerate {
* @param file
* @return boolean
*/
protected static boolean delFile(File file) {
protected static boolean delFile(File file) throws IOException {
boolean flag = false;
for(int i = 0; !flag && i++ < 10; flag = file.delete()) {
System.gc();
for(int i = 0; !flag && i++ < 10;flag = Files.deleteIfExists(file.toPath())) {
log.info("");
}
return flag;
@@ -20,8 +20,6 @@ public class CgFormColumnExtendVo {
protected String defaultVal;
protected String uploadnum;
public CgFormColumnExtendVo() {
}
public String getUploadnum() {
return this.uploadnum;
@@ -170,4 +168,4 @@ public class CgFormColumnExtendVo {
public String toString() {
return "{}";
}
}
}
@@ -16,9 +16,6 @@ public class ColumnVo extends CgFormColumnExtendVo {
private String classType_row = "";
private String optionType = "";
public ColumnVo() {
}
public String getFieldDbType() {
return this.fieldDbType;
}
@@ -92,7 +89,7 @@ public class ColumnVo extends CgFormColumnExtendVo {
}
public String getClassType_row() {
return this.classType != null && this.classType.indexOf("easyui-") >= 0 ? this.classType.replaceAll("easyui-", "") : this.classType_row;
return this.classType != null && this.classType.contains("easyui-") ? this.classType.replace("easyui-", "") : this.classType_row;
}
public void setClassType_row(String classType_row) {
@@ -118,4 +115,4 @@ public class ColumnVo extends CgFormColumnExtendVo {
public String toString() {
return "{\"fieldDbName\":\"" + this.fieldDbName + "\",\"fieldName\":\"" + this.fieldName + "\",\"filedComment\":\"" + this.filedComment + "\",\"fieldType\":\"" + this.fieldType + "\",\"fieldDbType\":\"" + this.fieldDbType + "\",\"classType\":\"" + this.classType + "\",\"classType_row\":\"" + this.classType_row + "\",\"optionType\":\"" + this.optionType + "\",\"charmaxLength\":\"" + this.charmaxLength + "\",\"precision\":\"" + this.precision + "\",\"scale\":\"" + this.scale + "\",\"nullable\":\"" + this.nullable + "\",\"fieldLength\":\"" + this.fieldLength + "\",\"fieldHref\":\"" + this.fieldHref + "\",\"fieldValidType\":\"" + this.fieldValidType + "\",\"fieldDefault\":\"" + this.fieldDefault + "\",\"fieldShowType\":\"" + this.fieldShowType + "\",\"fieldOrderNum\":\"" + this.fieldOrderNum + "\",\"isKey\":\"" + this.isKey + "\",\"isShow\":\"" + this.isShow + "\",\"isShowList\":\"" + this.isShowList + "\",\"isQuery\":\"" + this.isQuery + "\",\"uploadnum\":\"" + this.uploadnum + "\",\"defaultVal\":\"" + this.defaultVal + "\",\"sort\":\"" + this.sort + "\",\"readonly\":\"" + this.readonly + "\",\"queryMode\":\"" + this.queryMode + "\",\"dictField\":\"" + this.dictField + "\",\"dictTable\":\"" + this.dictTable + "\",\"dictText\":\"" + this.dictText + "\"}";
}
}
}
@@ -14,8 +14,6 @@ public class TableVo {
private Integer fieldRequiredNum;
private Map<?, ?> extendParams;
public TableVo() {
}
public String getEntityPackage() {
return this.entityPackage;
@@ -17,8 +17,6 @@ public class MainTableVo {
public Integer fieldRequiredNum;
private Map<?, ?> extendParams;
public MainTableVo() {
}
public Map<?, ?> getExtendParams() {
return this.extendParams;
@@ -120,4 +118,4 @@ public class MainTableVo {
public String toString() {
return "{\"entityPackage\":\"" + this.entityPackage + "\",\"tableName\":\"" + this.tableName + "\",\"entityName\":\"" + this.entityName + "\",\"ftlDescription\":\"" + this.ftlDescription + "\",\"primaryKeyPolicy\":\"" + this.primaryKeyPolicy + "\",\"sequenceCode\":\"" + this.sequenceCode + "\",\"ftl_mode\":\"" + this.ftl_mode + "\",\"subTables\":" + this.subTables + ",\"fieldRowNum\":\"" + this.fieldRowNum + "\",\"searchFieldNum\":\"" + this.searchFieldNum + "\",\"fieldRequiredNum\":\"" + this.fieldRequiredNum + "\"}";
}
}
}
@@ -21,8 +21,6 @@ public class SubTableVo {
private List<ColumnVo> originalColumns;
private Map<?, ?> extendParams;
public SubTableVo() {
}
public Map<?, ?> getExtendParams() {
return this.extendParams;
@@ -139,4 +137,4 @@ public class SubTableVo {
public String toString() {
return "{\"entityPackage\":\"" + this.entityPackage + "\",\"tableName\":\"" + this.tableName + "\",\"entityName\":\"" + this.entityName + "\",\"primaryKeyPolicy\":\"" + this.primaryKeyPolicy + "\",\"sequenceCode\":\"" + this.sequenceCode + "\",\"ftlDescription\":\"" + this.ftlDescription + "\",\"originalForeignKeys\":\"" + Arrays.toString(this.originalForeignKeys) + "\",\"foreignKeys\":\"" + Arrays.toString(this.foreignKeys) + "\",\"colums\":" + this.colums + ",\"originalColumns\":" + this.originalColumns + "}";
}
}
}
@@ -12,11 +12,9 @@ import org.apache.commons.lang.StringUtils;
@Slf4j
public class FileHelper {
public static List<String> dirNameList = new ArrayList();
public static List<String> fileSuffixList = new ArrayList();
public static List<String> dirNameList = new ArrayList<>();
public static List<String> fileSuffixList = new ArrayList<>();
public FileHelper() {
}
static {
dirNameList.add(".svn");
@@ -37,14 +35,9 @@ public class FileHelper {
* @return java.util.List<java.io.File>
*/
public static List<File> listFileAndSort(File dir) throws IOException {
ArrayList list = new ArrayList();
dfsAndAddFile(dir, (List)list);
Collections.sort(list, new Comparator<File>() {
@Override
public int compare(File var1, File var2) {
return var1.getAbsolutePath().compareTo(var2.getAbsolutePath());
}
});
ArrayList<File> list = new ArrayList<>();
dfsAndAddFile(dir, list);
Collections.sort(list, (var1, var2) -> var1.getAbsolutePath().compareTo(var2.getAbsolutePath()));
return list;
}
/**
@@ -88,7 +81,7 @@ public class FileHelper {
* @return boolean
*/
public static boolean isFile(File file) {
return file.isDirectory() ? false : isSuffixNotBlank(file.getName());
return !file.isDirectory() && isSuffixNotBlank(file.getName());
}
/**
* 后缀名是否为空白
@@ -163,7 +156,7 @@ public class FileHelper {
*/
private static boolean isSuffixContain(File file) {
for(int i = 0; i < fileSuffixList.size(); ++i) {
if (file.getName().endsWith((String) fileSuffixList.get(i))) {
if (file.getName().endsWith(fileSuffixList.get(i))) {
return true;
}
}
@@ -22,8 +22,6 @@ import java.util.StringTokenizer;
@Slf4j
public class FreemarkerHelper {
public FreemarkerHelper() {
}
/**
* 获取配置类
* @date 2021/4/2 15:47
@@ -39,7 +37,7 @@ public class FreemarkerHelper {
FileTemplateLoader[] fileTemplateLoaders = new FileTemplateLoader[templateRootDirs.size()];
for(int i = 0; i < templateRootDirs.size(); ++i) {
File file = (File)templateRootDirs.get(i);
File file = templateRootDirs.get(i);
log.debug(" FileTemplateLoader " + file.getAbsolutePath());
fileTemplateLoaders[i] = new FileTemplateLoader(file);
}
@@ -54,7 +52,7 @@ public class FreemarkerHelper {
public static List<String> a(String var0, String var1) {
String[] var2 = b(var0, "\\/");
ArrayList var3 = new ArrayList();
ArrayList<String> var3 = new ArrayList<>();
var3.add(var1);
var3.add(File.separator + var1);
String var4 = "";
@@ -72,14 +70,14 @@ public class FreemarkerHelper {
return new String[0];
} else {
StringTokenizer var2 = new StringTokenizer(var0, var1);
ArrayList var3 = new ArrayList();
ArrayList<String> var3 = new ArrayList<>();
while(var2.hasMoreElements()) {
Object var4 = var2.nextElement();
var3.add(var4.toString());
}
return (String[])var3.toArray(new String[var3.size()]);
return var3.toArray(new String[var3.size()]);
}
}
/**
@@ -5,18 +5,18 @@ import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
/**
* Nonce Number Once 只使用一次的数字相关工具类
*/
@Slf4j
public class NonceUtils {
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private static final String[] NUMBER_STRINGS = new String[]{"0", "00", "0000", "00000000"};
private static Date date;
private static int count = 0;
public NonceUtils() {
}
/**
* 生成指定长度随机字符串
* @date 2021/4/6 9:01
@@ -72,6 +72,7 @@ public class NonceUtils {
* @return java.lang.String
*/
public static String getNow() {
SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = new Date();
return SIMPLE_DATE_FORMAT.format(date);
}
@@ -133,6 +134,6 @@ public class NonceUtils {
}
public static void main(String[] args) throws IOException {
System.out.println(getSecureRandomLong() + getCurrentTimeMillis());
log.info(getSecureRandomLong() + getCurrentTimeMillis() + "");
}
}
@@ -11,8 +11,7 @@ import java.util.List;
import java.util.Locale;
public class SimpleFormat {
public SimpleFormat() {
}
/**
* 下划线字符串转换为驼峰字符串
* @date 2021/4/6 9:55
@@ -158,20 +157,20 @@ public class SimpleFormat {
* @return java.lang.String
*/
public String getInStrs(List<String> stringList) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
Iterator iterator = stringList.iterator();
while(iterator.hasNext()) {
String s = (String)iterator.next();
sb.append("'" + s + "',");
sb.append("'").append(s).append("',");
}
String string = sb.toString();
if ("".equals(string) && !string.endsWith(",")) {
if ("".equals(string)) {
return null;
} else {
string = string.substring(0, string.length() - 1);
return string;
}
}
}
}
@@ -3,8 +3,7 @@ package com.jero.codegenerate.generate.util;
import org.apache.commons.lang.StringUtils;
public class TableConvert {
public TableConvert() {
}
/**
* 校验字段为 '是' 或 '否'
* @date 2021/4/6 10:46
@@ -36,30 +36,20 @@ public class CodeConfigProperties {
public static String pageFilterFields;
public static String fieldRowNum;
private static ResourceBundle getDatasourcePropties(String url) {
PropertyResourceBundle propertyResourceBundle = null;
BufferedInputStream bufferedInputStream = null;
String configPath = System.getProperty("user.dir") + File.separator + "config" + File.separator + url + ".properties";
public static final String MY_SQL = "mysql";
public static final String SQL_SERVER = "sqlserver";
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(configPath));
private static ResourceBundle getDatasourcePropties(String url) {
String configPath = System.getProperty("user.dir") + File.separator + "config" + File.separator + url + ".properties";
PropertyResourceBundle propertyResourceBundle = null;
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(configPath))){
propertyResourceBundle = new PropertyResourceBundle(bufferedInputStream);
bufferedInputStream.close();
if (propertyResourceBundle != null) {
log.info(" JAR方式部署,通过config目录读取配置:" + configPath);
}
} catch (IOException e) {
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
e.printStackTrace();
}
return propertyResourceBundle;
}
@@ -85,7 +75,7 @@ public class CodeConfigProperties {
public static final boolean getDbFiledConvert() {
String dbFiledConvert = configDatasourcePropties.getString("db_filed_convert");
return !dbFiledConvert.toString().equals("false");
return !dbFiledConvert.equals("false");
}
private static String getBussiPackage() {
@@ -146,7 +136,7 @@ public class CodeConfigProperties {
configDatasourcePropties = ResourceBundle.getBundle(CodeConfigProperties.CONFIG_FILE_URL);
}
databaseType = "mysql";
databaseType = MY_SQL;
driverName = "com.mysql.jdbc.Driver";
databaseUrl = "jdbc:mysql://localhost:3306/jero-boot?useUnicode=true&characterEncoding=UTF-8";
username = "root";
@@ -173,11 +163,11 @@ public class CodeConfigProperties {
dbFiledConvert = getDbFiledConvert();
pageFilterFields = getPageFilterFields();
pageSearchFieldNum = getPageSearchFieldNum();
if (databaseUrl.indexOf("mysql") < 0 && databaseUrl.indexOf("MYSQL") < 0) {
if (databaseUrl.indexOf("oracle") < 0 && databaseUrl.indexOf("ORACLE") < 0) {
if (databaseUrl.indexOf("postgresql") < 0 && databaseUrl.indexOf("POSTGRESQL") < 0) {
if (databaseUrl.indexOf("sqlserver") >= 0 || databaseUrl.indexOf("sqlserver") >= 0) {
databaseType = "sqlserver";
if (!databaseUrl.contains(MY_SQL) && !databaseUrl.contains("MYSQL")) {
if (!databaseUrl.contains("oracle") && !databaseUrl.contains("ORACLE")) {
if (!databaseUrl.contains("postgresql") && !databaseUrl.contains("POSTGRESQL")) {
if (databaseUrl.contains(SQL_SERVER) || databaseUrl.contains("sql server")) {
databaseType = SQL_SERVER;
}
} else {
databaseType = "postgresql";
@@ -186,7 +176,7 @@ public class CodeConfigProperties {
databaseType = "oracle";
}
} else {
databaseType = "mysql";
databaseType = MY_SQL;
}
sourceRootPackage = sourceRootPackage.replace(".", "/");