Skip to content

Commit

Permalink
修改dbutils的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhuo committed Sep 4, 2019
1 parent 5c8e9db commit b8e5eb4
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 224 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/chen/JeneralDB/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class App {

// 创建数据库映射对象
public static void main(String[] args) throws Exception {
DBFactory.getInstance().createEntityFromDataBase();
new DBFactory().createEntityFromDataBase();
}
}
72 changes: 38 additions & 34 deletions src/main/java/com/chen/JeneralDB/DBFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,21 @@ public class DBFactory {
private boolean decimalPack = false;

private String database;

private DBUtil dbUtil;

private DBFactory() {
public DBFactory() {
try {
this.database = getProperty("db_name");
} catch (IOException e) {
e.printStackTrace();
}
}


public static synchronized DBFactory getInstance() {
if (null == dbFactory) {
dbFactory = new DBFactory();
}

return dbFactory;
public void setDbUtil(DBUtil dbUtil) {
this.dbUtil = dbUtil;
}


/**
* 根据数据库生成实体类
*/
Expand All @@ -72,7 +68,7 @@ public void createEntityFromDataBase()
*/
public void createEntitysByTableNames(List<String> tableNames)
throws Exception {
createEntitysByTableNames(tableNames, null, null);
createEntitysByTableNames(tableNames, null, null, null);
}


Expand All @@ -82,7 +78,7 @@ public void createEntitysByTableNames(List<String> tableNames)
* @param tableNames 表名
* @param directoryPath 文件夹名
*/
public void createEntitysByTableNames(List<String> tableNames, String directoryPath, String packageName)
public void createEntitysByTableNames(List<String> tableNames, String directoryPath, String packageName, String classNamePostfix)
throws Exception {
if (null == directoryPath)
directoryPath = getProperties().getProperty("packageOutPath");
Expand All @@ -96,15 +92,15 @@ public void createEntitysByTableNames(List<String> tableNames, String directoryP
directory.mkdirs();

for (String tableName : tableNames) {
parseToJava(tableName, directory.getAbsolutePath(), packageName);
parseToJava(tableName, directory.getAbsolutePath(), packageName, classNamePostfix);
}
}


/**
* 生成Java文件的主体类
*/
private void parseToJava(String allTableName, String directory, String packageName)
private void parseToJava(String allTableName, String directory, String packageName, String postfix)
throws Exception {
if (null == allTableName || allTableName.isEmpty() || null == directory) {
throw new NullPointerException();
Expand All @@ -113,16 +109,18 @@ private void parseToJava(String allTableName, String directory, String packageNa
StringBuilder sqlBuilder = new StringBuilder("select * from ");
sqlBuilder.append(allTableName);

ResultSetMetaData resultSet = DBUtil.getInstance().queryResultSetMetaData(sqlBuilder.toString());
ResultSetMetaData resultSet = dbUtil.queryResultSetMetaData(sqlBuilder.toString());

int size = resultSet.getColumnCount();
String[] columnNames = new String[size];
String[] columnType = new String[size];
Integer[] columntSize = new Integer[size];

utilPack = false; sqlPack = false; decimalPack = false;
for (int i = 0; i < size; i++) {
columnNames[i] = resultSet.getColumnName(i + 1);
columnType[i] = resultSet.getColumnTypeName(i + 1);
columntSize[i] = resultSet.getColumnDisplaySize(i + 1);

if ("datetime,timestamp,date,year".contains(columnType[i].toLowerCase()))
utilPack = true;
Expand All @@ -134,18 +132,18 @@ private void parseToJava(String allTableName, String directory, String packageNa
decimalPack = true;
}

String content = parse(allTableName, packageName, columnNames, columnType);
String content = parse(allTableName, packageName, postfix,columnNames, columnType, columntSize);

StringBuffer javaFilePath = new StringBuffer(directory);
javaFilePath.append(File.separator);
javaFilePath.append(formatClassName(allTableName));
javaFilePath.append(formatClassName(allTableName, postfix));
javaFilePath.append(".java");

writeToJavaFile(content, javaFilePath.toString());
}


private String parse(String allTableName, String packageName, String[] columnNames, String[] columnType)
private String parse(String allTableName, String packageName, String postfix, String[] columnNames, String[] columnType, Integer[] columnSize)
throws IOException {
StringBuffer buffer = new StringBuffer();

Expand Down Expand Up @@ -173,14 +171,14 @@ private String parse(String allTableName, String packageName, String[] columnNam
buffer.append("\r\n");

buffer.append("@Table(\"").append(allTableName).append("\")\r\n");
buffer.append("public class " + formatClassName(allTableName) + " {\r\n");
buffer.append("public class " + formatClassName(allTableName, postfix) + " {\r\n");
buffer.append("\r\n");

processAllAttrs(buffer, allTableName, columnNames, columnType);
processAllAttrs(buffer, allTableName, columnNames, columnType, columnSize);

processAllMethod(buffer, columnNames, columnType);
processAllMethod(buffer, columnNames, columnType, columnSize);

processToString(buffer, columnNames, columnType);
processToString(buffer, columnNames, columnType, columnSize);

buffer.append("}\r\n");

Expand All @@ -192,7 +190,7 @@ private String parse(String allTableName, String packageName, String[] columnNam
* 生成toString()方法
*
* */
private void processToString(StringBuffer buffer, String[] columnNames, String[] columnType) {
private void processToString(StringBuffer buffer, String[] columnNames, String[] columnType, Integer[] columnSize) {
buffer.append("\tpublic String toString() {\n");
buffer.append("\t\tStringBuffer string = new StringBuffer();\n");

Expand All @@ -218,14 +216,14 @@ private void processToString(StringBuffer buffer, String[] columnNames, String[]
* 生成所有get set 方法
*
* */
private void processAllMethod(StringBuffer buffer, String[] colnames, String[] colTypes) {
private void processAllMethod(StringBuffer buffer, String[] colnames, String[] colTypes, Integer[] columnSize) {
for (int i = 0; i < colnames.length; i++) {
buffer.append("\tpublic void set" + initCap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " +
buffer.append("\tpublic void set" + initCap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i], columnSize[i]) + " " +
colnames[i] + ") {\r\n");
buffer.append("\t\tthis." + colnames[i] + " = " + colnames[i] + ";\r\n");
buffer.append("\t}\r\n");
buffer.append("\r\n");
buffer.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initCap(colnames[i]) + "() {\r\n");
buffer.append("\tpublic " + sqlType2JavaType(colTypes[i], columnSize[i]) + " get" + initCap(colnames[i]) + "() {\r\n");
buffer.append("\t\treturn " + colnames[i] + ";\r\n");
buffer.append("\t}\r\n");
buffer.append("\r\n");
Expand All @@ -237,7 +235,7 @@ private void processAllMethod(StringBuffer buffer, String[] colnames, String[] c
* 生成所有属性
*
* */
private void processAllAttrs(StringBuffer buffer, String tableName, String[] columnNames, String[] columnTypes) {
private void processAllAttrs(StringBuffer buffer, String tableName, String[] columnNames, String[] columnTypes, Integer[] columnSize) {
for (int i = 0; i < columnNames.length; i++) {
String indexType = getIndexOfcolumn(columnNames[i], tableName);
if (null != indexType) {
Expand All @@ -255,7 +253,7 @@ private void processAllAttrs(StringBuffer buffer, String tableName, String[] col

buffer.append(")\r\n");
buffer.append("\tprivate ");
buffer.append(sqlType2JavaType(columnTypes[i]));
buffer.append(sqlType2JavaType(columnTypes[i], columnSize[i]));
buffer.append(" ");
buffer.append(columnNames[i]);
buffer.append(";\r\n");
Expand All @@ -267,7 +265,7 @@ private void processAllAttrs(StringBuffer buffer, String tableName, String[] col
private String getIndexOfcolumn(String columnName, String tableName) {
String sql = "show index from " + tableName + " where Column_name = ?;";
try {
DataTable dt = DBUtil.getInstance().queryDataTable(sql, columnName);
DataTable dt = dbUtil.queryDataTable(sql, columnName);
return !dt.isEmpty() ? dt.getObjectByColumnNameInRow("Key_name", 0).toString() : null;
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -277,15 +275,19 @@ private String getIndexOfcolumn(String columnName, String tableName) {
}


private String sqlType2JavaType(String sqlType) {
private String sqlType2JavaType(String sqlType, Integer integer) {
String ret = null;

switch (sqlType.toLowerCase()) {
case "bit":
ret = "Boolean";
break;
case "tinyint":
ret = "byte";
if (integer == 1) {
ret = "Boolean";
} else {
ret = "byte";
}
break;
case "smallint":
ret = "short";
Expand Down Expand Up @@ -341,7 +343,7 @@ private String sqlType2JavaType(String sqlType) {
* 根据表名格式化生成类名
*
* */
private String formatClassName(String allTableName) {
private String formatClassName(String allTableName, String postfix) {
if (null == allTableName || allTableName.isEmpty()) {
return allTableName;
}
Expand All @@ -360,6 +362,9 @@ private String formatClassName(String allTableName) {
allTableName = allTableName.replaceAll("[_]", "");
}

if (null != postfix) {
allTableName = allTableName + postfix;
}
return allTableName;
}

Expand Down Expand Up @@ -389,7 +394,7 @@ private String initCap(String allTableName) {
public String[] getAllTableNamesOfDataBase()
throws Exception {
String sql = String.format(allTableNameSql, getProperty("db_name"), getProperty("db_type"));
Object[] allTableNames = DBUtil.getInstance().queryDataTable(sql).getObjectsByColumnName("TABLE_NAME");
Object[] allTableNames = dbUtil.queryDataTable(sql).getObjectsByColumnName("TABLE_NAME");
String[] result = new String[allTableNames.length];

for (int i = 0; i < allTableNames.length; i++) {
Expand All @@ -407,7 +412,7 @@ public String[] getAllTableNamesOfDataBase()
private DataTable getAllPKOfTable(String tableName)
throws Exception {
String sql = "show index from " + tableName + " where key_name = 'PRIMARY';";
return DBUtil.getInstance().queryDataTable(sql);
return dbUtil.queryDataTable(sql);
}


Expand Down Expand Up @@ -474,6 +479,5 @@ private void writeToJavaFile(String content, String directory)
printWriter.write(content);
printWriter.flush();
printWriter.close();
DBUtil.print("成功创建实体类" + javaFile.getPath());
}
}
Loading

0 comments on commit b8e5eb4

Please sign in to comment.