删除商品功能和批量删除商品功能
//删除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";
}