商品分类-新增分类-级联查询
新增功能,首先实现级联查询
1、实现步骤
1.1、 Service 服务层
package com.ego.service;
import com.ego.pojo.GoodsCategory;
import com.ego.vo.GoodsCategoryVo;
import java.util.List;
/**
* 商品分类service
*/
public interface GoodsCategoryServicei {
/**
* 根据parentId查询商品分类列表
*
* @param parentId
* @return
*/
List<GoodsCategory> selectCategoryList(short parentId);
/**
* 保存商品分类
* @param goodsCategory
* @return
*/
int categorySave(GoodsCategory goodsCategory);
/**
* 查询商品分类列表页面
* @return
*/
List<GoodsCategoryVo> selectCatoryListForView();
}
GoodsCategoryServiceImpl
package com.ego.service.impl;
import com.ego.mapper.GoodsCategoryMapper;
import com.ego.pojo.GoodsCategory;
import com.ego.pojo.GoodsCategoryExample;
import com.ego.service.GoodsCategoryServicei;
import com.ego.vo.GoodsCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 商品分类service
*/
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryServicei {
@Autowired
private GoodsCategoryMapper goodsCategoryMapper;
/**
* 根据parentId查询商品分类列表
*
* @param parentId
* @return
*/
@Override
public List<GoodsCategory> selectCategoryList(short parentId) {
//创建example对象
GoodsCategoryExample example = new GoodsCategoryExample();
//创建查询对象并设置查询查询参数
example.createCriteria().andParentIdEqualTo(parentId);
//查询
return goodsCategoryMapper.selectByExample(example);
}
/**
* 保存商品分类
*
* @param goodsCategory
* @return
*/
@Override
public int categorySave(GoodsCategory goodsCategory) {
return goodsCategoryMapper.insertSelective(goodsCategory);
}
/**
* 查询商品分类列表页面
*
* @return
*/
@Override
public List<GoodsCategoryVo> selectCatoryListForView() {
//创建example对象
GoodsCategoryExample example = new GoodsCategoryExample();
//创建查询对象并设置参数
example.createCriteria().andParentIdEqualTo((short) 0).andLevelEqualTo((byte) 1);
//查询一级分类
List<GoodsCategory> gcList01 = goodsCategoryMapper.selectByExample(example);
//处理一级分类查询下级分类
List<GoodsCategoryVo> gcvList01 = new ArrayList<>();
for (GoodsCategory gc01 : gcList01) {
//将对象转化成vo对象
GoodsCategoryVo gcv01 = new GoodsCategoryVo();
//将原对象属性复制到新对象中
BeanUtils.copyProperties(gc01, gcv01);
//清空查询参数
example.clear();
//创建二级分类对象并设置参数
example.createCriteria().andParentIdEqualTo(gc01.getId()).andLevelEqualTo((byte) 2);
//查询二级分类
List<GoodsCategory> gcList02 = goodsCategoryMapper.selectByExample(example);
//处理二级分类查询下级分类
List<GoodsCategoryVo> gcvList02 = new ArrayList<>();
for (GoodsCategory gc02 : gcList02) {
//将对象转化为vo对象
GoodsCategoryVo gcv02 = new GoodsCategoryVo();
//将原对象属性复制到新对象中
BeanUtils.copyProperties(gc02, gcv02);
//清空查询参数
example.clear();
//创建三级分类查询并设置参数
example.createCriteria().andParentIdEqualTo(gc02.getId()).andLevelEqualTo((byte) 3);
//查询三级分类
List<GoodsCategory> gcList03 = goodsCategoryMapper.selectByExample(example);
//处理三级分类添加到二级分类vo对象中
List<GoodsCategoryVo> gcvList03 = new ArrayList<>();
for (GoodsCategory gc03 : gcList03) {
// 将对象转换成vo对象
GoodsCategoryVo gcv03 = new GoodsCategoryVo();
//将原对象属性复制到新对象中
BeanUtils.copyProperties(gc03, gcv03);
//将对象添加至三级分类vo对象中
gcvList03.add(gcv03);
}
//添加到二级分类vo对象中
gcv02.setChildern(gcvList03);
//将对象添加至二级分类vo对象中
gcvList02.add(gcv02);
}
//添加至一级分类vo对象中
gcv01.setChildern(gcvList02);
//将对象添加至一级分类vo对象中
gcvList01.add(gcv01);
}
return gcvList01;
}
}
1.2、 Controller 控制层
ProductController.java
package com.ego.controller;
import com.ego.pojo.GoodsCategory;
import com.ego.result.BaseResult;
import com.ego.service.GoodsCategoryServicei;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* Created by Administrator on 2019/9/12.
*/
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private GoodsCategoryServicei goodsCategoryService;
/**
* 跳转商品分类页面
*
* @return
*/
@RequestMapping("/category/list")
public String categoryList(Model model) {
//查询商品分类信息
model.addAttribute("gcvList",goodsCategoryService.selectCatoryListForView());
return "product/category/category-list";
}
/**
* 跳转商品分类新增页面并查询所有*分类
*
* @return
*/
@RequestMapping("/category/add")
public String categoryAdd(Model model) {
//查询商品分类的*菜单
model.addAttribute("gcvList", goodsCategoryService.selectCategoryList((short) 0));
return "product/category/category-add";
}
/**
* 根据父id查询商品分类子类列表
*
* @param parentId
* @return
*/
@RequestMapping(value = "/category/{parentId}", method = RequestMethod.GET)
@ResponseBody
public List<GoodsCategory> categoryListByParentId(@PathVariable short parentId) {
//查询商品分类
return goodsCategoryService.selectCategoryList(parentId);
}
/**
* 保存商品分类
*
* @param goodsCategory
* @return
*/
@RequestMapping("/category/save")
@ResponseBody
public BaseResult categorySave(GoodsCategory goodsCategory) {
int result = goodsCategoryService.categorySave(goodsCategory);
return result > 0 ? BaseResult.success() : BaseResult.error();
}
}
1.3、 jsp 页面
category-add.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath }"></c:set>
<!DOCTYPE html>
<html>
<head>
<!-- 静态导入 -->
<%@ include file="../../head.jsp" %>
<script type="text/javascript">
function delfunc(obj) {
layer.confirm('确认删除?', {
btn: ['确定', '取消'] //按钮
}, function () {
$.ajax({
type: 'post',
url: $(obj).attr('data-url'),
data: {act: 'del', del_id: $(obj).attr('data-id')},
dataType: 'json',
success: function (data) {
if (data == 1) {
layer.msg('操作成功', {icon: 1});
$(obj).parent().parent().remove();
} else {
layer.msg(data, {icon: 2, time: 2000});
}
layer.closeAll();
}
})
}, function (index) {
layer.close(index);
return false;// 取消
}
);
}
//全选
function selectAll(name, obj) {
$('input[name*=' + name + ']').prop('checked', $(obj).checked);
}
function get_help(obj) {
layer.open({
type: 2,
title: '帮助手册',
shadeClose: true,
shade: 0.3,
area: ['90%', '90%'],
content: $(obj).attr('data-url'),
});
}
function delAll(obj, name) {
var a = [];
$('input[name*=' + name + ']').each(function (i, o) {
if ($(o).is(':checked')) {
a.push($(o).val());
}
})
if (a.length == 0) {
layer.alert('请选择删除项', {icon: 2});
return;
}
layer.confirm('确认删除?', {btn: ['确定', '取消']}, function () {
$.ajax({
type: 'get',
url: $(obj).attr('data-url'),
data: {act: 'del', del_id: a},
dataType: 'json',
success: function (data) {
if (data == 1) {
layer.msg('操作成功', {icon: 1});
$('input[name*=' + name + ']').each(function (i, o) {
if ($(o).is(':checked')) {
$(o).parent().parent().remove();
}
})
} else {
layer.msg(data, {icon: 2, time: 2000});
}
layer.closeAll();
}
})
}, function (index) {
layer.close(index);
return false;// 取消
}
);
}
</script>
<meta name="__hash__" content="3089b9badfca8307d7d520487d125ae4_6385f66dff50b4c04db3ec79b8a9d245"/>
</head>
<body style="background-color:#ecf0f5;">
<div class="wrapper">
<div class="breadcrumbs" id="breadcrumbs">
<ol class="breadcrumb">
<li><a href="javascript:void();"><i class="fa fa-home"></i> 后台首页</a></li>
<li><a href="javascript:void();">商品管理</a></li>
<li><a href="javascript:void();">添加修改分类</a></li>
</ol>
</div>
<section class="content">
<div class="row">
<div class="col-sm-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">增加分类</h3>
<div class="pull-right">
<a href="javascript:history.go(-1)" data-toggle="tooltip" title="" class="btn btn-default"
data-original-title="返回"><i class="fa fa-reply"></i></a>
<a href="javascript:;" class="btn btn-default"
data-url="http://www.ego.cn/Doc/Index/article/id/1006/developer/user.html"
οnclick="get_help(this)"><i class="fa fa-question-circle"></i> 帮助</a>
</div>
</div>
<!-- /.box-header -->
<form action="" method="post" class="form-horizontal"
id="category_form">
<div class="box-body">
<div class="form-group">
<label class="col-sm-2 control-label">分类名称</label>
<div class="col-sm-6">
<input type="text" placeholder="名称" class="form-control large" name="name" value="">
<span class="help-inline" style="color:#F00; display:none;" id="err_name"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">手机分类名称</label>
<div class="col-sm-6">
<input type="text" placeholder="手机分类名称" class="form-control large"
name="mobileName" value="">
<span class="help-inline" style="color:#F00; display:none;"
id="err_mobile_name"></span>
</div>
</div>
<div class="form-group">
<input type="hidden" name="parentId" id="parentId" value="0"/>
<input type="hidden" name="level" id="level" value="1"/>
<label0 class="control-label col-sm-2">上级分类</label0>
<div class="col-sm-3">
<select name="parent_id_1" id="parent_id_1"
οnchange="getCategory(this.value,'parent_id_2','0');"
class="small form-control">
<option value="0">*分类</option>
<c:forEach items="${gcvList}" var="gc">
<option value="${gc.id}">${gc.name}</option>
</c:forEach>
</select>
</div>
<div class="col-sm-3">
<select name="parent_id_2" id="parent_id_2" class="small form-control"
οnchange="setParentId(this.value, '3');">
<option value="0">请选择商品分类</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">导航显示</label>
<div class="col-sm-10">
<label>
<input checked="checked" type="radio" name="isShow" value="1"> 是
<input type="radio" name="is_show" value="0"> 否
</label>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">分类分组:</label>
<div class="col-sm-1">
<select name="catGroup" id="cat_group" class="form-control">
<option value="0">0</option>
<option value='1'>1</option>
"
<option value='2'>2</option>
"
<option value='3'>3</option>
"
<option value='4'>4</option>
"
<option value='5'>5</option>
"
<option value='6'>6</option>
"
<option value='7'>7</option>
"
<option value='8'>8</option>
"
<option value='9'>9</option>
"
<option value='10'>10</option>
"
<option value='11'>11</option>
"
<option value='12'>12</option>
"
<option value='13'>13</option>
"
<option value='14'>14</option>
"
<option value='15'>15</option>
"
<option value='16'>16</option>
"
<option value='17'>17</option>
"
<option value='18'>18</option>
"
<option value='19'>19</option>
"
<option value='20'>20</option>
"
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">分类展示图片</label>
<div class="col-sm-10">
<input οnclick="GetUploadify(1,'image','category');" type="button" value="上传图片"/>
<input type="text" value="" name="image" id="image" class="form-control large"
readonly="readonly" style="width:500px;display:initial;"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">显示排序</label>
<div class="col-sm-1">
<input type="text" placeholder="50" class="form-control large" name="sortOrder"
value=""/>
<span class="help-inline" style="color:#F00; display:none;"
id="err_sort_order"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">分佣比例</label>
<div class="col-sm-1">
<input type="text" placeholder="50" class="form-control large"
name="commissionRate" id="commission_rate" value="0"
οnpaste="this.value=this.value.replace(/[^\d.]/g,'')"
οnkeyup="this.value=this.value.replace(/[^\d.]/g,'')"/>
</div>
<div class="col-sm-1" style="margin-top: 6px;margin-left: -20px;">
<span>%</span>
</div>
</div>
</div>
<div class="box-footer">
<input type="hidden" name="id" value="">
<button type="reset" class="btn btn-primary pull-left"><i class="icon-ok"></i>重填</button>
<button type="button"
οnclick="ajax_submit_form();"
class="btn btn-primary pull-right"><i class="icon-ok"></i>提交
</button>
</div>
<input type="hidden" name="__hash__"
value="3089b9badfca8307d7d520487d125ae4_6385f66dff50b4c04db3ec79b8a9d245"/></form>
</div>
</div>
</div>
</section>
</div>
<script>
/** 以下是编辑时默认选中某个商品分类*/
$(document).ready(function () {
});
function getCategory(parentId, nextNode, level) {
// 用户重新选择*分类时,重置下级分类为:请选择商品分类,且清空下级分类信息
var htmlStr = "<option value='0'>请选择商品分类</option>";
if (0 == parentId) {
$("#" + nextNode).html(htmlStr);
// 修改parentId和level
$("#parentId").val(parentId);
$("#level").val(1);
return;
}
// 修改parentId和level
$("#parentId").val(parentId);
$("#level").val(2);
$.ajax({
url: "${ctx}/product/category/" + parentId,
type: "GET",
data: {"parentId": parentId},
dataType: "JSON",
success: function (result) {
if (result.length > 0) {
for (i = 0; i < result.length; i++) {
htmlStr += "<option value='" + result[i].id + "'>" + result[i].name + "</option>";
}
$("#" + nextNode).html(htmlStr);
} else {
layer.alert("获取子分类失败!");
}
},
error: function () {
// jQuery的alert插件
layer.alert("获取子分类失败!");
}
});
}
// 设置parentId和level
function setParentId(parentId, level) {
if (0 == parentId) {
// 修改parentId和level
$("#parentId").val($("#parent_id_1").val());
$("#level").val(2);
return;
}
$("#parentId").val(parentId);
$("#level").val(level);
}
function ajax_submit_form() {
$.ajax({
url: "${ctx}/product/category/save",
type: "POST",
data: $("#category_form").serialize(),
dataType: "JSON",
success: function (result) {
if (200 == result.code) {
layer.confirm('保存成功!', {btn: ['继续新增', '返回列表']},
function () {
window.location.href = "${ctx}/product/category/add";
}, function () {
window.location.href = "${ctx}/product/category/list";
});
} else {
layer.alert("获取子分类失败!");
}
},
error: function () {
// jQuery的alert插件
layer.alert("获取子分类失败!");
}
});
}
</script>
</body>
</html>