通过java8修改代码

master
星期八 5 years ago
parent 0b17850346
commit 5a0f779ef8
  1. 35
      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<Class> classSet,List<Table> tableList) {
StringBuffer sql = new StringBuffer();
//扫描实体类包的实体class
Set<Class> set = classSet;
Map<String, Class> map = new HashMap<>(set.size());
set.forEach((clazz)->{
map.put(BeanSqlUtil.beanNameToTableName(clazz), clazz);
});
Map<String, Class> 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<Field> fieldsList = BeanUtil.getAllField(clazz);
Map<String, Object> clazzMap = new HashMap<>(fieldsList.size());
Iterator<Field> fieldsIterator = fieldsList.iterator();
while (fieldsIterator.hasNext()) {
Field f = fieldsIterator.next();
clazzMap.put(BeanSqlUtil.caseToHump(f.getName()), f);
}
List<Column> columnList = table.getColumns();
fieldsList.forEach(field -> {
clazzMap.put(BeanSqlUtil.caseToHump(field.getName()), field);
});
List<Column> columnList = table.getColumns();
Iterator<Column> 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<String> 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());
}
});

Loading…
Cancel
Save