ajax 提交数组,mybatis 数组接受

时间:2022-07-01 17:28:49
function jinxingzhong() {
// 配送单编号:psd2017090103
var divHtml = "";
$.ajax({
type: "POST",
url: "/app/jinxingzhong",
traditional: true,
data: {
loginUserId: "549d321508db446e9bcaa477835fe5f1",
deliveryCode: "psd2017090102",// 配送单号
states: [1, 2] //1:已接单待提货 2:已提货待安装(这个参数是固定的)
},
dataType: 'json',
success: function (data) {
console.log(data);
console.log(typeof(data));
divHtml += "配送单编号:" + data[0].delivery_code + "<br>";
divHtml += "地址:" + data[0].provinceName + data[0].cityName + data[0].countryName + data[0].customer_addr + "<br>";
divHtml += "地址id(省,市,区):" + data[0].province + "/" + data[0].city + "/" + data[0].country + "/" + data[0].customer_addr + "<br><br>";
divHtml += "收货人:" + data[0].customer_id + data[0].customer_name + "<br>";
divHtml += "电话:" + data[0].customer_tel + "<br>";
divHtml += "state (state 1 = 已接单待提货):" + data[0].state + "<br>";
divHtml += "state (state 2 = 已提货待安装):" + data[0].state + "<br>";
console.log(divHtml);
$('#jinxingzhong').html(divHtml);
}, error: function () {
alert("错了");
}
});
}
注意:这里ajax参数要加上,traditional:true,不然后台controller,接受不到参数的
private final Logger logger = Logger.getLogger(this.getClass());

    @RequestMapping("/jinxingzhong")
@ResponseBody
public String jinxingzhong(String loginUserId, String deliveryCode, Integer[] states) { List<App> jinxingzhongList = appService.jinxingzhong(loginUserId, deliveryCode, states); logger.info(JSON.toJSONStringWithDateFormat(jinxingzhongList, "yyyy-MM-dd HH:mm:ss"));
String jinxingzhongJsonStr = JSON.toJSONString(jinxingzhongList); // 将 list 转换成 json对象的字符串 logger.info(JSON.toJSONStringWithDateFormat(jinxingzhongJsonStr, "yyyy-MM-dd HH:mm:ss"));
return jinxingzhongJsonStr;
}

  

public interface AppMapper {
/**
* 获取接单信息
*
* @param loginUserId
* @param states
* @return
*/
List<App> getJieDan(@Param("loginUserId") String loginUserId, @Param("states") Integer[] states, @Param("deliveryCode") String deliveryCode);

  注意:@param 是绑定参数;

ajax 提交数组,mybatis 数组接受

这里注意,collection ,里面的值,网上说,如果是list 就用list,数组就用 array ,我试过,这两个都不行,用绑定的参数对象才可以,item里的值是相当于

对数组的集合循环的变量,任意的;