介绍此文前,先分享一个在线代码格式化工具(http://tool.oschina.net/codeformat/json),可以将json数据格式化。
需要插入到库中的字段格式(CLOB字段),又称大数据字段,详细了解去百度查下。
如下是我希望插入到库中的json格式数据:
{"files":[{"filename": "1.jpg","url": "12368176238716.jpg","sort": 0},
{"filename": "2.doc","url": "71987439845793.doc","sort": 1},
{"filename": "3.xls","url": "098867546557657.xls","sort": 2}]}
如何实现呢?
当然是拼字符串了,解释很苍白,直接贴代码了:
/**
* 新增
*
* @param request
* @return
*/
@RequestMapping("/addNotice")
@ResponseBody
public Object addNotice(HttpServletRequest request) {
Map
String paramsStr = "{\"files\":[";
String[] fnames = request.getParameterValues("fname");
String[] urls = request.getParameterValues("furl");
if(fnames!=null){
for (int i = 0; i < fnames.length; i++) {
paramsStr += "{\"filename\": \"" + fnames[i] + "\",\"url\": \""
+ urls[i] + "\",\"sort\": " + i + "}";
if(i < fnames.length-1){
paramsStr +=",";
}
}
paramsStr += "]}";
/*paramsStr拼接格式按如下样式:
* {"files":[{"filename": "1.jpg","url": "12368176238716.jpg","sort": 0},
{"filename": "2.doc","url": "71987439845793.doc","sort": 1},
{"filename": "3.xls","url": "098867546557657.xls","sort": 2}
]}*/
param.put("params", paramsStr);
}
groupnoticeService.pushNotice(param);
return 1;
}
sql语句里面关于插入clob字段的写法,请见下面红色字体所示:
insert into td_pushmessage t
(t.msgid,t.time,t.unit,t.title,t.content,t.gid,t.msgtype,t.creator,
t.CREATEDEPT,t.PARAMS,t.BEGINTIME,t.DEADLINE,t.CREATETIME)
values
(
#{msg_id,jdbcType=VARCHAR},
#{time,jdbcType=VARCHAR},
#{unit,jdbcType=VARCHAR},
#{title,jdbcType=VARCHAR},
#{content,jdbcType=VARCHAR},
#{gid,jdbcType=VARCHAR},
#{msgtype,jdbcType=VARCHAR},
#{creator,jdbcType=VARCHAR},
#{createdept,jdbcType=VARCHAR},
#{params,jdbcType=CLOB},
to_date(#{clocked,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss'),
sysdate,
to_date(#{deadline,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss'),
sysdate)
效果如下: