目的是:将Map类型的数据,存入一张纵表中,其中三个字段有区别,busi_key,busi_value,createTime(可以保存一样的时间),利用jdbcTemplete 的batchUpdate方法执行批量操作。并对key-value中的value 通过正则表达式进行判断,如果是科学计数法的,就进行转换,存为String类型。
/**
* 设置业务扩展信息:是否存在- 存在 先删除、再保存;不存在 直接入库
*/
@Override
public void setProcInstBusinessExtData(final Long processInstId,String tenantId,long shardInd,final Map<String, Object> resource) {
if(busiExists(processInstId)) {
String deleteSql = "delete from WF_PROCESSINST_BUSINESS where processInstId = ?";
this.getJdbcTemplate().update(deleteSql,processInstId);
}
String sql =
"INSERT INTO WF_PROCESSINST_BUSINESS (processInstId,busi_key,busi_value,createTime,sharding_id,tenantid) "
+ " VALUES("+ processInstId +",?,?,?,"+ shardInd +",'"+ tenantId +"')";
this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
Iterator<Map.Entry<String, Object>> it = resource.entrySet().iterator();
Map.Entry<String, Object> entry = null;
public void setValues(PreparedStatement ps, int index) throws SQLException {
entry = it.next();
ps.setString(1, entry.getKey());
ps.setString(2, number2String(entry.getValue().toString()));
ps.setTimestamp(3, new Timestamp(new Date().getTime()));
// ps.setTimestamp(4, new Timestamp(new Date().getTime())); //更新时间 一个流程实例只保留一份数据,不更新
}
public int getBatchSize() {
return resource.size();
}
});
}
//科学计数处理 huang.song 2018-08-23
public String number2String(String value) {
String res = value;
String rex1 = "^((\\d+.?\\d+)[Ee]{1}(\\d+))$";
boolean isMatch = Pattern.matches(rex1, value);
if (isMatch) {
BigDecimal bd1 = new BigDecimal(value);
res = bd1.toPlainString();
}
return res;
}
/**
* 判断是否有记录
* @param processInstId
* @return
*/
private Boolean busiExists(long processInstId ) {
int size = this.getJdbcTemplate().queryForObject(
"select count(1) from WF_PROCESSINST_BUSINESS t where processInstId = ?", new Object[]{processInstId },Integer.class);
if (size > 0) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}