1. 程式人生 > >全選+批量刪除

全選+批量刪除

ring head edi size over clear arr page eee

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/back_page/head.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>babasport-list</title>
<script type="text/javascript">
	function checkBox(name,checked){
		$("input[name=‘"+name+"‘]").attr("checked",checked)
	}
	function optDelete(name,isDisplay) {
		//至少選擇一個
		var s=$("input[name=‘ids‘]:checked").size();
		if(s<=0){
			alert("至少選擇一個");
			return;
		}
		if(!confirm("確定刪除嗎")){
			return;
		}
		//刪除
		$("#form1").attr("action","/cn/brand/deletes.do?name="+name+"&isDisplay="+isDisplay
); $("#form1").attr("method","post").submit(); } </script> </head> <body> <div class="box-positon"> <div class="rpos">當前位置: 品牌管理 - 列表</div> <form class="ropt"> <input class="add" type="button" value="添加" onclick="javascript:window.location.href=‘/cn/brand/toAdd.do‘"/> </form> <div class="clear"></div> </div> <div class="body-box"> <form action="/cn/brand/list.do" method="post" style="padding-top:5px;"> 品牌名稱: <input type="text" name="name" value="${name}"/> <select name="isDisplay"> <option value="1" <c:if test="${isDisplay ==1 }">selected="selected"</c:if>>是</option> <option value="0" <c:if test="${isDisplay ==0 }">selected="selected"</c:if>>不是</option> </select> <input type="submit" class="query" value="查詢"/> </form> <form id="form1
"> <table cellspacing="1" cellpadding="0" border="0" width="100%" class="pn-ltable"> <thead class="pn-lthead"> <tr> <th width="20"><input type="checkbox" onclick="checkBox(‘ids‘,this.checked)"/></th> <th>品牌ID</th> <th>品牌名稱</th> <th>品牌圖片</th> <th>品牌描述</th> <th>排序</th> <th>是否可用</th> <th>操作選項</th> </tr> </thead> <tbody class="pn-ltbody"> <c:forEach items="${pagination.list}" var="entry"> <tr bgcolor="#ffffff" onmouseout="this.bgColor=‘#ffffff‘" onmouseover="this.bgColor=‘#eeeeee‘"> <td><input type="checkbox" value="${entry.id}" name="ids
"/></td> <td align="center">${entry.id}</td> <td align="center">${entry.name}</td> <td align="center"><img width="40" height="40" src="${entry.allUrl}"/></td> <td align="center">${entry.description}</td> <td align="center">${entry.sort}</td> <td align="center"><c:if test="${entry.isDisplay==1}">是</c:if><c:if test="${entry.isDisplay==0 }">不是</c:if></td> <td align="center"> <a class="pn-opt" href="/cn/brand/toEditor.do?id=${entry.id}">修改</a> | <a class="pn-opt" onclick="if(!confirm(‘您確定刪除嗎?‘)) {return false;}"
href="/cn/brand/delete.do?id=${entry.id}&name=${entry.name}&isDisplay=${entry.isDisplay}">刪除</a> </td> </tr> </c:forEach> </tbody> </table> </form> <div class="page pb15"><span class="r inb_a page_b"> <c:forEach items="${pagination.pageView}" var="page"> ${page} </c:forEach> </span></div> <div style="margin-top:15px;"><input class="del-button" type="button" value="刪除" onclick="optDelete(‘${name}‘,‘${isDisplay}‘);"/></div> </div> </body> </html>

2.dao層方法和xml

package cn.itcast.core.dao.product;

import java.util.List;

import cn.itcast.core.bean.product.Brand;
import cn.itcast.core.query.product.BrandQuery;


/**
 * 品牌
 * @author lx
 *
 */
public interface BrandDao {
	//List集合
	public List<Brand> getBrandListWithPage(Brand brand);
	//查詢集合
	public List<Brand> getBrandList(BrandQuery brandQuery);
	//查詢總記錄數
	public int getBrandCount(Brand brand);
	//添加品牌
	public void addBrand(Brand brand);
	//單個刪除
	public void deleteBrandByKey(Integer id);
	//批量刪除
	public void deleteBrandByKeys(Integer[] ids);
	//修改
	public void updateBrandByKey(Brand brand);
	//查詢單個
	public Brand getBrandByKey(Integer id);
}

xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="cn.itcast.core.dao.product.BrandDao">

	<!-- resultMap  -->
	<resultMap type="Brand" id="brand">
		<result column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="description" property="description"/>
		<result column="img_url" property="imgUrl"/>
		<result column="sort" property="sort"/>
		<result column="is_display" property="isDisplay"/>
	</resultMap>
	
	<!-- brandSelector   fields  id,name   id   #{id} == "id"  ${id} == id     -->
	<sql id="brandSelector">
		select
		<if test="fields != null">
			${fields}
		</if>
		<if test="fields == null">
			id , name ,description,img_url,sort,is_display
		</if>
		from bbs_brand
	</sql>
	<!-- 品牌條件 -->
	<sql id="brandWhere">
		<where>
			<if test="isDisplay != null">
				is_display = #{isDisplay}
			</if>
			<if test="name != null">
				<if test="nameLike == false">
					and name = #{name}
				</if>
				<if test="nameLike == true">
					and name like "%"#{name}"%"
				</if>
			</if>
		</where>
	</sql>
	
	<!-- 品牌 Order by -->
	<sql id="brandOrderBy">
		<if test="fieldOrders != null and fieldOrders.size > 0">
			order by
			<foreach collection="fieldOrders" item="fieldOrder" separator=",">
				${fieldOrder.field} ${fieldOrder.order}
			</foreach>
		</if>
	</sql>
	
	<!-- limit -->
	<sql id="brandLimit">
		<if test="startRow != null">
			limit #{startRow},#{pageSize}
		</if>
	</sql>
	
	<!-- 查詢品牌集合 -->
	<select id="getBrandList" parameterType="BrandQuery" resultMap="brand">
		<include refid="brandSelector"/>
		<include refid="brandWhere"/>
		<include refid="brandOrderBy"/>
		<include refid="brandLimit"/>
	</select>
	
	
	<!-- 查詢品牌   get*   -->
	<select id="getBrandListWithPage" parameterType="Brand" resultMap="brand">
		select id , name ,description,img_url,sort,is_display
		from bbs_brand
		<where>
			<if test="isDisplay != null">
				is_display = #{isDisplay}
			</if>
			<if test="name != null">
				and name = #{name}
			</if>
		</where>
		order by id desc
		limit #{startRow},#{pageSize}
	</select>
	
	<!-- 查詢總記錄數 -->
	<select id="getBrandCount" parameterType="Brand" resultType="Integer">
		select count(1)
		from bbs_brand
		<where>
			<if test="isDisplay != null">
				is_display = #{isDisplay}
			</if>
			<if test="name != null">
				and name = #{name}
			</if>
		</where>
	</select>
	<!-- 添加品牌 -->
	<insert id="addBrand" parameterType="Brand">
		insert into bbs_brand
		<trim prefix="(" suffix=")">
			name ,
			description,
			img_url,
			sort,
			is_display
		</trim>
		values
		<trim prefix="(" suffix=")">
			#{name},
			#{description},
			#{imgUrl},
			#{sort},
			#{isDisplay}
		</trim>
	</insert>
	<!-- 刪除 通過主鍵 -->
	<delete id="deleteBrandByKey" parameterType="Integer">
		delete from bbs_brand
		<where>
			id = #{id}
		</where>
	</delete>
	<!-- 批量刪除 -->
	<delete id="deleteBrandByKeys" parameterType="Integer">
		delete from bbs_brand
		<where>
			id in 
			<foreach collection="array" item="id" open="(" close=")" separator="," >
				#{id}
			</foreach>
		</where>
	</delete>
	<!-- 修改 -->
	<update id="updateBrandByKey" parameterType="Brand">
		update bbs_brand
		<set>
			<if test="name != null">
				name=#{name},
			</if>
			<if test="description != null">
				description=#{description},
			</if>
			<if test="imgUrl != null">
				img_url=#{imgUrl},
			</if>
			<if test="sort != null">
				sort=#{sort},
			</if>
			<if test="isDisplay != null">
				is_display=#{isDisplay}
			</if>
		</set>
		<where>
			id=#{id}
		</where>
	</update>
	<!-- 通過ID查詢一個品牌對象 -->
	<select id="getBrandByKey" parameterType="Integer" resultMap="brand">
		select id , name ,description,img_url,sort,is_display
		from bbs_brand
		<where>
			id = #{id}
		</where>
	</select>
</mapper>

controller層方法

//刪除品牌
		@RequestMapping(value="/brand/deletes.do")
		public String deletes(Integer[] ids,String name,Integer isDisplay,Model model){
			if(name!=null&&!name.equals("")){
				model.addAttribute("name", name);
			}
			model.addAttribute("id", ids);
			if(isDisplay!=null&&!isDisplay.equals("")){
				model.addAttribute("isDisplay", isDisplay);
			}
			brandService.deleteBrandByKeys(ids);
			return "redirect:/brand/list.do";
		}

全選+批量刪除