sevlet层:
/**
* 批量删除
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void deletAll(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String[] iids = req.getParameterValues("ids");
//System.out.println(iids[0]);
// 更新
int[] iids2 = new int[iids.length];
for (int i = 0; i < iids.length; i++) {
iids2[i]=Integer.parseInt(iids[i]);
}
boolean updateFlag = infoService.deletAll(iids2);
if (updateFlag) {// 修改成功
// 刷新
// resp.sendRedirect("/wky/info?action=findPageInfos¤tPage=1&pageSize=5");
ResultMsg msg = new ResultMsg(1, "成功");
String json = JSON.toJSONString(msg);
resp.getWriter().write(json);
} else {
// 修改失败
ResultMsg msg = new ResultMsg(2, "失败");
String json = JSON.toJSONString(msg);
resp.getWriter().write(json);
}
}
service层:
/**
* 批量删除
*
* @param ids
* @return
*/
public boolean deletAll(int[] iids2) {
return infoDao.deletAll(iids2);
}
dao层:
/**
* 批量删除
* @param ids
* @return
*/
public boolean deletAll(int[] iids2);
daoImpl层:
/**
* 批量删除
*/
@Override
public boolean deletAll(int[] iids2) {
// 获取conn
Connection conn = JdbcUtils.getConn();
String sql = "delete from y_info where id in(";
for(int id:iids2){
sql+=id+",";
}
//去掉最后一个逗号,增加一个)
sql = sql.substring(0, sql.length()-1);
sql+=")";
Statement state =null;
int update =0;
try {
state = conn.createStatement();
update = state.executeUpdate(sql);
if (update>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JdbcUtils.close(conn,state);
}
return false;
}
批量删除
最新推荐文章于 2021-10-14 20:11:51 发布