From 5a0f779ef8f7c76628ea83e357284d7520e49d22 Mon Sep 17 00:00:00 2001 From: qi_liang Date: Sun, 29 Mar 2020 12:56:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=80=9A=E8=BF=87java8=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../panda/jdbc/sql/builder/MysqlBuilder.java | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java b/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java index 0fdbe96..b995a75 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.util.*; +import java.util.stream.Collectors; /** * @author qi @@ -35,26 +36,19 @@ public class MysqlBuilder extends AbstractDbBuiler { public String automaticUpdateDb(Set classSet,List tableList) { StringBuffer sql = new StringBuffer(); //扫描实体类包的实体class - Set set = classSet; - Map map = new HashMap<>(set.size()); - - set.forEach((clazz)->{ - map.put(BeanSqlUtil.beanNameToTableName(clazz), clazz); - }); + Map map = classSet.stream().collect(Collectors.toMap(BeanSqlUtil::beanNameToTableName,clazz->clazz)); //初始化的时候,tableList可能为null - if(tableList!=null) { - tableList.forEach((table)->{ + if(tableList!=null&&tableList.size()>0) { + tableList.forEach(table->{ if (map.containsKey(table.getTableName())) { Class clazz = map.get(table.getTableName()); List fieldsList = BeanUtil.getAllField(clazz); Map clazzMap = new HashMap<>(fieldsList.size()); - Iterator fieldsIterator = fieldsList.iterator(); - while (fieldsIterator.hasNext()) { - Field f = fieldsIterator.next(); - clazzMap.put(BeanSqlUtil.caseToHump(f.getName()), f); - } - List columnList = table.getColumns(); + fieldsList.forEach(field -> { + clazzMap.put(BeanSqlUtil.caseToHump(field.getName()), field); + }); + List columnList = table.getColumns(); Iterator columnIterator = columnList.iterator(); while (columnIterator.hasNext()) { Column column = columnIterator.next(); @@ -64,13 +58,11 @@ public class MysqlBuilder extends AbstractDbBuiler { } } - columnIterator = columnList.iterator(); - while (columnIterator.hasNext()) { - Column column = columnIterator.next(); + columnList.forEach(column -> { sql.append("ALTER TABLE " + table.getTableName() + " DROP COLUMN " + column.getColumnName() + ";"); - } - - for (String key : clazzMap.keySet()) { + }); + Set keySet = clazzMap.keySet(); + keySet.forEach(key->{ Field f = (Field) clazzMap.get(key); String column = BeanSqlUtil.caseToHump(f.getName()); String columnType = dataTypeMap.get(f.getGenericType().toString()); @@ -79,7 +71,8 @@ public class MysqlBuilder extends AbstractDbBuiler { lengthStr = "(100)"; } sql.append("ALTER TABLE " + table.getTableName() + " ADD COLUMN " + column + " " + columnType + lengthStr + ";"); - } + + }); map.remove(table.getTableName()); } }); From 0f51ff79a5d4bfa9524a25eb8c30badf4811fc11 Mon Sep 17 00:00:00 2001 From: qi_liang Date: Sun, 29 Mar 2020 13:05:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=AD=94=E6=B3=95?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/panda/jdbc/sql/builder/MysqlBuilder.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java b/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java index b995a75..5d93617 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java @@ -22,6 +22,14 @@ public class MysqlBuilder extends AbstractDbBuiler { private static final Logger LOGGER = LoggerFactory.getLogger(MysqlBuilder.class); private static Map dataTypeMap=new HashMap<>(); + /** + * 数据库数据类型-char + */ + public static final String DB_TYPE_CHAR = "char"; + /** + * 数据库数据类型-int + */ + public static final String DB_TYPE_INT = "int"; static { dataTypeMap.put("class java.lang.String","varchar"); @@ -56,7 +64,6 @@ public class MysqlBuilder extends AbstractDbBuiler { clazzMap.remove(column.getColumnName()); columnIterator.remove(); } - } columnList.forEach(column -> { sql.append("ALTER TABLE " + table.getTableName() + " DROP COLUMN " + column.getColumnName() + ";"); @@ -67,7 +74,7 @@ public class MysqlBuilder extends AbstractDbBuiler { String column = BeanSqlUtil.caseToHump(f.getName()); String columnType = dataTypeMap.get(f.getGenericType().toString()); String lengthStr = ""; - if ("varchar".equals(columnType) || "int".equals(columnType)) { + if (DB_TYPE_CHAR.equals(columnType) || DB_TYPE_CHAR.equals(columnType)) { lengthStr = "(100)"; } sql.append("ALTER TABLE " + table.getTableName() + " ADD COLUMN " + column + " " + columnType + lengthStr + ";"); @@ -104,7 +111,7 @@ public class MysqlBuilder extends AbstractDbBuiler { for (Map map:mapList){ String dataTypeStr = dataTypeMap.get(map.get("fieldType")); sql.append(BeanSqlUtil.caseToHump(map.get("fieldName"))+" "+dataTypeStr); - if ("varchar".equals(dataTypeStr)||"int".equals(dataTypeStr)){ + if (DB_TYPE_CHAR.equals(dataTypeStr)||DB_TYPE_INT.equals(dataTypeStr)){ if (map.containsKey("fieldLength")) { sql.append("("+map.get("fieldLength")+")"); }