1.鏈接mysql的文件加上批處理的配置 [批量新增和更新可以參照多個對象批量刪除,這個只有批量刪除的例子,但批量新增和更新只有mapper.xml文件不一樣,只需要單獨的更新和新增方法就可以,jsp頁面多個對象傳值都是一樣的實現方式]
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/work_project?allowMultiQueries=true&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
2.獲取jsp文件里多個對象的值,異步ajax提交。(兩種方式實現批量刪除,一個是只有id的數組傳值,另一個是多個對象傳值[批量更新和新增也可參照這個])
2.1 jsp頁面動態拼接函數(我這個例子是把對象的多個屬性值都放到一個input里了,批量更新和新增可以把單個的屬性放到每一行td的input標簽里)
var html ="";
$.each(pageData,function (i,n){
html += '<tr>';
html += ' <td>'+(i+1)+'</td>';
html += ' <td><input type="checkbox" id="'+n.id+'" name="'+n.username+'" email="'+n.email+'" loginacct="'+n.loginacct+'"></td>';//這里是批量刪除的關鍵
html += ' <td>'+n.loginacct +'</td>';
html += ' <td>'+n.username +'</td>';
html += ' <td>'+n.email +'</td>';
html += ' <td>';
html += ' <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>';
html += ' <button type="button" class="btn btn-primary btn-xs" onclick="window.location.href=\'${CWF_PATH}/user/toUpdate.htm?id='+n.id+'\'"><i class=" glyphicon glyphicon-pencil"></i></button>';
html += ' <button type="button" class="btn btn-danger btn-xs" onclick="deleteUser('+n.id+',\''+n.username+'\')"><i class=" glyphicon glyphicon-remove"></i></button>';
html += ' </td>';
html += '</tr>';
});
$("#tbodyConcat").html(html);
2.1.2 ajax異步提交,方式1(只有id的數組傳值)。

function deleteBatch(){
var selectedCheckBox = $("#tbodyConcat input:checked");//根據選中的checkbox取得所有要刪除的數據的集合
if(selectedCheckBox.length<=0){
layer.msg("請選擇要刪除的數據!", {time:1000, icon:6, shift:6});
return false;
}
var idStr = "";//數組方式,定義傳值的變量(根據ajax的data屬性設置的,具體看上邊的圖)
$.each(selectedCheckBox,function(i,n){
//數組方式
if(0!=i){
idStr += "&";
}
idStr += "ids="+n.id; //后台必須以ids接收(這里如果是abc,后台接收變量也要是abc)
});
layer.confirm("確認要刪除這些用戶嗎",{icon:3,title:"提示"},function(confirmIndex){
layer.close(confirmIndex);
$.ajax({
type:"POST",
contentType : 'application/json;charset=utf-8', //設置請求頭信息
dataType:"json",
data:idStr,//數組方式
url:"${CWF_PATH}/user/deleteBatch.do",
beforeSend:function(){
return true;
},success:function(data){
if(data.success){
window.location.href="${CWF_PATH}/user/toIndex.htm";
}else{
layer.msg(data.message, {time:1000, icon:6, shift:6});
}
},errot:function(){
layer.msg("刪除數據失敗!", {time:1000, icon:6, shift:6});
}
});
},function(confirmIndex){
layer.close(confirmIndex);
}
);
}
java后台deleteBatch方法(controller文件,這里service和dao不展示,只展示mapper.xml文件的方法)
//注意這里的ids必須和jsp頁面[idStr += "ids="+n.id;]的ids一致,
//而不是ajax的[data:idStr,]的idStr
@ResponseBody
@RequestMapping("/deleteBatch")
public Object deleteBatch(Integer []ids){
AjaxResult result = new AjaxResult();
try {
int count = userService.deleteBatch(ids);
if(count == ids.length){
result.setSuccess(true);
result.setMessage("刪除成功");
}
} catch (Exception e) {
result.setSuccess(false);
result.setMessage("刪除失敗");
e.printStackTrace();
}
return result;
}
mapper.xml文件(int deleteBatchUser(@Param("ids") Integer[] id); // collection="ids")
<!-- collection="array" 如果參數是數組,采用默認"array"名你獲取數組參數.
collection="list" 如果參數是集合,采用默認"list"名你獲取集合參數.
collection="具體名可以根據dao接口的@Param設置" 例如:int deleteBatchUser(@Param("ids") Integer[] id); // collection="ids"-->
<delete id="deleteBatch">
delete from t_user
where id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
2.2多個對象傳值方式
2.2.1 jsp頁面動態拼接函數(我這個例子是把對象的多個屬性值都放到一個input里了,批量更新和新增可以把單個的屬性放到每一行td的input標簽里)
var html ="";
$.each(pageData,function (i,n){
html += '<tr>';
html += ' <td>'+(i+1)+'</td>';
html += ' <td><input type="checkbox" id="'+n.id+'" name="'+n.username+'" email="'+n.email+'" loginacct="'+n.loginacct+'"></td>';//這里是批量刪除的關鍵
html += ' <td>'+n.loginacct +'</td>';
html += ' <td>'+n.username +'</td>';
html += ' <td>'+n.email +'</td>';
html += ' <td>';
html += ' <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>';
html += ' <button type="button" class="btn btn-primary btn-xs" onclick="window.location.href=\'${CWF_PATH}/user/toUpdate.htm?id='+n.id+'\'"><i class=" glyphicon glyphicon-pencil"></i></button>';
html += ' <button type="button" class="btn btn-danger btn-xs" onclick="deleteUser('+n.id+',\''+n.username+'\')"><i class=" glyphicon glyphicon-remove"></i></button>';
html += ' </td>';
html += '</tr>';
});
$("#tbodyConcat").html(html);
2.2.2 ajax異步提交,方式2(多個對象的數組傳值,批量更新和新增可以參照這個)。
function deleteBatch(){
var selectedCheckBox = $("#tbodyConcat input:checked");//根據選中的checkbox取得要刪除(更新和新增)的集合
if(selectedCheckBox.length<=0){
layer.msg("請選擇要刪除的數據!", {time:1000, icon:6, shift:6});
return false;
}
var jsonData = new Array();//定義要刪除(更新和新增)數據的數組,將頁面的多個對象數據存放到這個數組
//循環集合,根據attr取得input里的各個屬性值。(如果是新增和修改,需要根據取得的當前對象去獲取其他input的text屬性的值:可以用jQuery的parent加fing方式去獲取,具體可以去百度)
$.each(selectedCheckBox,function(i,n){
var that = this;
//把取得的多個對象的值push到數組里,注意這里的key要和User實體類的屬性一致
jsonData.push({"id":$(that).attr("id"),
"loginacct":$(that).attr("loginacct"),
"username":$(that).attr("name"),
"email":$(that).attr("email")});
});
layer.confirm("確認要刪除這些用戶嗎",{icon:3,title:"提示"},function(confirmIndex){
layer.close(confirmIndex);
$.ajax({
type:"POST",
contentType : 'application/json;charset=utf-8', //設置請求頭信息
dataType:"json",
data:JSON.stringify(jsonData),//將Json對象序列化成Json字符串,JSON.stringify()原生態方法
url:"${CWF_PATH}/user/deleteBatch.do",
beforeSend:function(){
return true;
},success:function(data){
if(data.success){
window.location.href="${CWF_PATH}/user/toIndex.htm";
}else{
layer.msg(data.message, {time:1000, icon:6, shift:6});
}
},errot:function(){
layer.msg("刪除數據失敗!", {time:1000, icon:6, shift:6});
}
});
},function(confirmIndex){
layer.close(confirmIndex);
}
);
}
java后台deleteBatch方法(controller文件,這里service和dao不展示,只展示mapper.xml文件的方法)
注意接收參數的地方要加 @RequestBody
//前台數組傳輸
@ResponseBody
@RequestMapping("/deleteBatch")
public Object deleteBatch(@RequestBody List<User> users){
AjaxResult result = new AjaxResult();
try {
int count = userService.deleteBatchByList(users);
if(count == users.size()){
result.setSuccess(true);
result.setMessage("批量刪除成功");
}
} catch (Exception e) {
result.setSuccess(false);
result.setMessage("批量刪除失敗");
e.printStackTrace();
}
return result;
}
mapper.xml文件的方法(批量更新和新增的話只需要把insert和update的方法加上就可以了,也是用foreach的方式,不會的去百度)
mapper接口 int deleteBatchByList(List<User> users);
<delete id="deleteBatchByList">
delete from t_user
where id in
<foreach collection="list" item="user" separator="," open="(" close=")">
#{user.id}
</foreach>
</delete>
=========新增Mapper.xm文件方法例子(寫法不同,配置文件不同)================================
****注意如果不都寫在foreach里,數據庫連接文件可以不加allowMultiQueries=true&rewriteBatchedStatements=true
insert into t_user_role(userid,roleid) values
<foreach collection="list" item="entity" index="index"
separator=",">
(#{entity.id},#{entity.roleId})
</foreach>
****注意如果寫在都foreach里, 數據庫連接文件必須加allowMultiQueries=true&rewriteBatchedStatements=true
<foreach collection="list" item="user" separator=";">
insert into t_user_role(userid,roleid) values(#{user.id},#{user.roleId})
</foreach>
上邊的改進版(先判斷在更新)
<foreach collection="list" item="user" separator=";">
insert into t_user_role
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="user.id != null">
userid,
</if>
<if test="user.roleId != null">
roleid,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="user.id != null">
#{user.id,jdbcType=INTEGER},
</if>
<if test="user.roleId != null">
#{user.roleId,jdbcType=INTEGER}
</if>
</trim>
</foreach>