1. 程式人生 > >SSM刪除、修改商品功能

SSM刪除、修改商品功能

刪除商品功能和批量刪除商品功能

	//刪除sql語句
	<delete id="deleteBrandByKey" parameterType="Integer">
		delete from bbs_brand
		<where>
			id = #{id}
		</where>
	</delete>
	
	//批量刪除sql語句
	<delete id="deleteBrandByKeys" parameterType="Integer">
		delete from bbs_brand
		<where>
			id in
			<foreach collection="array" open="(" separator="," close=")" item="id">
				#{id}
			</foreach>
		</where>
	</delete>

	//批量刪除前臺程式碼
	<script type="text/javascript">
		function checkBox(name,checked){
			$("input[name=ids]").attr("checked",checked);
		}
	
		function optDelete(name,isDisplay){
			var length = $("input[name=ids]:checked");
			if(length <= 0){
				alert("請至少選擇一個");
				return ;
			}
			if(!confirm("您確定刪除嗎?")){
				return ;
			}
			$("#jvForm").attr("action","/brand/deletes.do?name="+name+"&isDisplay="+isDisplay);
			$("#jvForm").attr("method","post").submit();
		}
	</script>
	<!-- 傳遞引數 -->
		<div style="margin-top: 15px;">
			<input class="del-button" type="button" value="刪除"
				onclick="optDelete('${name}','${isDisplay}');" />
		</div>

	//controller層程式碼
	//通過id刪除商品
	@RequestMapping("/brand/delete.do")
	public String delete(Integer id,Integer isDisplay,String name,ModelMap modelMap){
		brandService.deleteBrandByKey(id);
		
		if(StringUtils.isNotBlank(name)){
			modelMap.addAttribute("name",name);
		}
		if(isDisplay !=null){
			modelMap.addAttribute("isDisplay",isDisplay);
		}
		
		return "redirect:/brand/list.do";
	}
	//通過ids刪除商品
	@RequestMapping("/brand/deletes.do")
	public String deletes(Integer[] ids,Integer isDisplay,String name,ModelMap modelMap){
		brandService.deleteBrandByKeys(ids);
		
		if(StringUtils.isNotBlank(name)){
			modelMap.addAttribute("name",name);
		}
		if(isDisplay !=null){
			modelMap.addAttribute("isDisplay",isDisplay);
		}
		
		return "redirect:/brand/list.do";
	}

實現修改商品功能

	//sql語句
	<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>

	//上傳圖片到伺服器
	<script type="text/javascript">
		function uploadPic() {
			var options = {
				url : "/upload/uploadPic.do",
				dataType : "json",
				type : "post",
				success : function(data) {
					$("#allImgUrl").attr("src", data.url);
					$("#path").val(data.path);
				}
			}
			$("#jvForm").ajaxSubmit(options);
		}
	</script>
	
	//後臺程式碼
	
	// 跳到修改頁面
	@RequestMapping("/brand/toEdit.do")
	public String toEdit(Integer id, ModelMap modelMap) {
		Brand brand = brandService.getBrand(id);
		modelMap.addAttribute("brand", brand);
		return "brand/edit";
	}

	// 實現修改功能
	@RequestMapping("/brand/edit.do")
	public String edit(Integer id,Brand brand) {
		brand.setId(id);
		brandService.updateBrandByKey(brand);
		return "redirect:/brand/list.do";
	}