mybatis批量插入,control中传入参数为
2019-01-29 本文已影响5人
墨色尘埃
关于mybatis批量问题,见之前文章
mybatis批量插入、批量更新和批量删除
mybatis批量新增,插入的数据顺序乱了
①如果前端传数组过来
[
{
"sendNumber": "1062323454524",
"recvNumber": "13802023030",
"sendContent": "短信内容",
"sendTime": "2019-01-24 13:35:23"
},
{
"sendNumber": "1062323454524",
"recvNumber": "13802023030",
"sendContent": "短信内容",
"sendTime": "2019-01-24 13:35:23"
}
]
则control用List
接
/**
* 新增短信信息接口
*/
@RequestMapping(value = "/recvSMS", method = RequestMethod.POST)
public ResponseObj<Boolean> recvSMS(@RequestBody List<SmsRecv> list) {
if (list != null && list.size() > 0) {
int i = smsRecvMapper.insertByBatch(list);
if (i == list.size())
return new ResponseObj<>(true, RetCode.SUCCESS);
return new ResponseObj<>(false, RetCode.FAIL);
} else {
return new ResponseObj<>(false, RetCode.FAIL);
}
}
②如果前端传这种方式过来
{
"data": [
{
"sendNumber": "1062323454524",
"recvNumber": "13802023030",
"sendContent": "短信内容",
"sendTime": "2019-01-24 13:35:23"
},
{
"sendNumber": "1062323454524",
"recvNumber": "13802023030",
"sendContent": "短信内容",
"sendTime": "2019-01-24 13:35:23"
}
]
}
则control用Map
接
/**
* 新增短信信息接口
*/
@RequestMapping(value = "/recvSMS", method = RequestMethod.POST)
public ResponseObj<Boolean> recvSMS(@RequestBody Map formData) {
List<SmsRecv> smsRecvList = new ArrayList<>();
List<Map<String, String>> list = (List<Map<String, String>>) formData.get("data");
if (list != null && list.size() > 0) {
for (Map<String, String> map : list) {
SmsRecv smsRecv = new SmsRecv();
smsRecv.setSmsId(Long.valueOf(CommonUtil.getSysRef()));
smsRecv.setSendNumber(map.get("sendNumber"));
smsRecv.setRecvNumber(map.get("recvNumber"));
smsRecv.setSendContent(map.get("sendContent"));
String sendTime = map.get("sendTime");
smsRecv.setSendTime(DateUtil.stringTooDate(sendTime));
smsRecv.setOperateTime(DateUtil.getDateTime());
smsRecvList.add(smsRecv);
}
int i = smsRecvMapper.insertByBatch(smsRecvList);
if (i == smsRecvList.size())
return new ResponseObj<>(true, RetCode.SUCCESS);
return new ResponseObj<>(false, RetCode.FAIL);
} else {
return new ResponseObj<>(false, RetCode.FAIL);
}
}