|
发表于 2016-11-7 02:14:43
|
显示全部楼层
本帖最后由 wex81997167 于 2016-11-7 02:18 编辑
保存部分能否也做下优化,在保存成功的时候如果是mysql数据库返回下自增id和受影响函数,尝试了下需要重新生成jar包的节奏啊,能不能不需要重新生成baas-api.jar也能达到效果!这是我们修改的代码
-------效果截图(返回一个id自增的集合,如果是批量插入也可以获取所有插入的最新id值):
-------
CURD.java
public static JSONObject save(JSONObject params, ActionContext context) throws SQLException, NamingException, ParseException {
// 获取参数
String db = params.getString("db");
JSONArray tables = params.getJSONArray("tables");
JSONObject permissions = params.containsKey("permissions")?params.getJSONObject("permissions"):null;
JSONObject result = new JSONObject();
Connection conn = context.getConnection(db);
try {
conn.setAutoCommit(false);
int i=0;
if (tables != null && tables.size()>0) {
for(Object jsonTable : tables){
Table table = Transform.jsonToTable((JSONObject)jsonTable);
String tableName = table.getTableName();
if(canSave(permissions, tableName)){
result.put("ids",DataUtils.saveData(conn, table, getSaveColumnsByPermissions(permissions, tableName)));
}
}
}
return result;
} finally {
conn.close();
}
}
-------
DataUtils.java
/**
* 保存Table数据,并指定列范围
* @param conn
* @param table
* @param tableName 数据库表名
* @param columns 列范围
* @throws SQLException
*/
public static JSONObject saveData(Connection conn, Table table, Collection<String> columns){
JSONObject result = new JSONObject();
ResultSet rs=null;
long id = 0;//自增ID
if (columns == null) {
columns = new ArrayList<String>();
columns.addAll(table.getColumnNames());
}
String idColumn = table.getIDColumn();
PreparedStatement newStat = null;
String newSQL = createNewSQL(table, columns);
try {
newStat = conn.prepareStatement(newSQL,Statement.RETURN_GENERATED_KEYS);
for (Row row : table.getRows(RowState.NEW)) {
int i = 1,j=0;
for (String column : columns) {
newStat.setObject(i, row.getValue(column));
i++;
}
if(logger.isDebugEnabled())
logger.debug("saveData:"+newStat.toString());
newStat.execute();
rs = newStat.getGeneratedKeys();
while(rs.next()){
id=rs.getLong(1);
}
result.put("id"+j++,id);
}
} catch (SQLException e) {
String msg = "SQL执行失败,"+newStat.toString();
throw new com.justep.baas.data.sql.SQLException(msg,e);
}
PreparedStatement editStat = null;
String updateSQL = createUpdateSQL(table, columns);
try {
editStat = conn.prepareStatement(updateSQL);
for (Row row : table.getRows(RowState.EDIT)) {
int i = 1;
for (String column : columns) {
editStat.setObject(i, row.getValue(column));
i++;
}
editStat.setObject(columns.size() + 1, row.isChanged(idColumn) ? row.getOldValue(table.getIDColumn()) : row.getValue(idColumn));
if(logger.isDebugEnabled())
logger.debug("saveData:"+editStat.toString());
editStat.execute();
}
} catch (SQLException e) {
String msg = "SQL执行失败,"+editStat.toString();
throw new com.justep.baas.data.sql.SQLException(msg,e);
}
PreparedStatement deleteStat = null;
String deleteSQL = createDeleteSQL(table);
try {
deleteStat = conn.prepareStatement(deleteSQL);
for (Row row : table.getRows(RowState.DELETE)) {
deleteStat.setObject(1, row.isChanged(idColumn) ? row.getOldValue(table.getIDColumn()) : row.getValue(idColumn));
if(logger.isDebugEnabled())
logger.debug("saveData:"+deleteStat.toString());
deleteStat.execute();
}
} catch (SQLException e) {
String msg = "SQL执行失败,"+deleteStat.toString();
throw new com.justep.baas.data.sql.SQLException(msg,e);
}
return result;
}
|
|