工具类:
/**
* 动态修改表结构
*/
@IocBean
@Slf4j
public class MyExtDao extends NutDao {
public void create(Dao dao, final String tableName,
final Map<String, Object> map, boolean hasColumnComment, boolean hasTableComment, String tableComment, boolean dropIfExists) {
MapEntityMaker mapEntityMaker = new MapEntityMaker();
mapEntityMaker.init(getDataSource(), dao.getJdbcExpert(), null);
dao.create(mapEntityMaker.make(tableName, map, hasColumnComment, hasTableComment, tableComment), dropIfExists);
}
public void migration(Dao dao,
final String tableName,
final Map<String, Object> map,
final boolean hasColumnComment,
final boolean add,
final boolean del) {
MapEntityMaker mapEntityMaker = new MapEntityMaker();
mapEntityMaker.init(getDataSource(), dao.getJdbcExpert(), null);
Entity<?> en = mapEntityMaker.make(tableName, map, hasColumnComment, false, null);
final JdbcExpert expert = dao.getJdbcExpert();
dao = Daos.ext(dao, tableName);
final List<Sql> sqls = new ArrayList<Sql>();
dao.run(new ConnCallback() {
@Override
public void invoke(Connection conn) throws Exception {
expert.setupEntityField(conn, en);
Statement stat = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
try {
// 获取数据库元信息
stat = conn.createStatement();
rs = stat.executeQuery("select * from " + en.getTableName() + " where 1 != 1");
meta = rs.getMetaData();
Set<String> columnNames = new HashSet<String>();
int columnCount = meta.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(meta.getColumnName(i).toLowerCase());
}
for (MappingField mf : en.getMappingFields()) {
if (mf.isReadonly()) {
continue;
}
String colName = mf.getColumnName();
if (columnNames.contains(colName.toLowerCase())) {
columnNames.remove(colName.toLowerCase());
continue;
}
if (add) {
log.info("add column[{}] to table[{}]",
mf.getColumnName(),
en.getTableName());
sqls.add(expert.createAddColumnSql(en, mf));
}
}
if (del) {
for (String colName : columnNames) {
log.info("del column[{}] from table[{}]", colName, en.getTableName());
Sql sql = Sqls.create("ALTER table $table DROP column $name");
sql.vars().set("table", en.getTableName());
sql.vars().set("name", colName);
sqls.add(sql);
}
}
} catch (SQLException e) {
if (log.isDebugEnabled()) {
log.debug("migration Table '{}' fail!", en.getTableName(), e);
}
}
// Close ResultSet and Statement
finally {
Daos.safeClose(stat, rs);
}
}
});
for (Sql sql : sqls) {
dao.execute(sql);
}
}
}
调用:
public void buildTable(String tableCode, String tableComment, String tableJson) {
String tableName = OfficeConstant.TABLE_PREFIX + tableCode;
if (Strings.isBlank(tableJson)) {
log.info("表结构未定义 {}", tableName);
return;
}
List<NutMap> fieldList = Json.fromJsonAsList(NutMap.class, Json.fromJson(String.class, tableJson));
if (!this.dao().exists(tableName)) {
// 表不存在,则创建
myExtDao.create(this.dao(), tableName, getTableMap(tableName, fieldList),
true, true, tableComment, false);
} else {
// 修改表结构
myExtDao.migration(this.dao(), tableName, getTableMap(tableName, fieldList), true,true, true);
}
}
ps:欢迎优化代码