JavaWeb三级菜单分类查询详解

废话不多说,直接贴代码:

dao层代码:

mapper:

List<ProductCategory> selectByParentId(Integer id);

mapper.xml

<select id="selectByParentId" parameterType="int" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from easybuy_product_category
  where parentId = #{parentId,jdbcType=INTEGER}
</select>

Test:

import cn.hd.entity.ProductCategory;
import cn.hd.mapper.ProductCategoryMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_dao.xml")
public class productCategoryTest {

    @Resource(name = "productCategoryMapper")
    private ProductCategoryMapper productCategoryMapper;

    @Test
    public void fun(){
        List<ProductCategory> productCategories = productCategoryMapper.selectByParentId(0);
        System.out.println(productCategories);
    }
}

service:

List<productCategoryQueryVo> getProductCategoryList();

serviceImpl:

package cn.hd.service.impl;


import cn.hd.entity.ProductCategory;
import cn.hd.mapper.ProductCategoryMapper;
import cn.hd.query_vo.productCategoryQueryVo;
import cn.hd.service.ProductCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("productCategoryServiceImpl")
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource(name = "productCategoryMapper")
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<productCategoryQueryVo> getProductCategoryList() {

        List<productCategoryQueryVo> productCategoryQueryVoList = new ArrayList<>();

        List<ProductCategory> productCategory1 = productCategoryMapper.selectByParentId(0);
        for (ProductCategory p1 : productCategory1) {
            productCategoryQueryVo productCategoryQueryVo = new productCategoryQueryVo();
            /*一级分类中的二级分类*/
            List<ProductCategory> productCategory2 = productCategoryMapper.selectByParentId(p1.getId());
            for (ProductCategory p2 : productCategory2) {
                productCategoryQueryVo productCategoryQueryVo1 = new productCategoryQueryVo();
                List<ProductCategory> productCategories3 = productCategoryMapper.selectByParentId(p2.getId());

                for (ProductCategory p3:productCategories3) {
                    productCategoryQueryVo productCategoryQueryVo2 = new productCategoryQueryVo();
                    productCategoryQueryVo2.setProductCategories(p3);

                    productCategoryQueryVo1.getProductCategoryVoList().add(productCategoryQueryVo2);
                }

                productCategoryQueryVo1.setProductCategories(p2);
                productCategoryQueryVo.getProductCategoryVoList().add(productCategoryQueryVo1);
            }
            /*封装一级分类 本身*/
            productCategoryQueryVo.setProductCategories(p1);
            productCategoryQueryVoList.add(productCategoryQueryVo);
        }
        return productCategoryQueryVoList;
    }
}

Test:

import cn.hd.service.ProductCategoryService;
        import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

        import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_service.xml")
public class productCategoryTest {
    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;

    @Test
    public void fun(){
        productCategoryService.getProductCategoryList();
    }
}

Controller:

package cn.hd.controller;

import cn.hd.query_vo.productCategoryQueryVo;
import cn.hd.service.ProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


@Controller
public class ProductCategoryController {

    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;

    @RequestMapping("/index")
    public String index(Model model){
        List<productCategoryQueryVo> productCategoryVoList = productCategoryService.getProductCategoryList();
        model.addAttribute("productCategoryVoList",productCategoryVoList);
        return "jsp/pre/index.jsp";
    }
}

JSP

<div class="leftNav">
                <ul>
                    <c:forEach items="${productCategoryVoList}" var="temp" >
                        <li>
                            <div class="fj">
                        <span class="n_img"><span></span>
                            <img src="${ctx}/statics/images/${temp.productCategories.iconClass}"/></span>
                                <span class="fl">${temp.productCategories.name}</span>
                            </div>
                            <div class="zj">
                                <div class="zj_l">
                                    <c:forEach items="${temp.productCategoryVoList}" var="vo">
                                        <div class="zj_l_c">
                                            <h2>
                                                <a href="${ctx}/Product?action=queryProductList&category=${vo.productCategories.id}&level=2">${vo.productCategories.name}</a>
                                            </h2>
                                            <c:forEach items="${vo.productCategoryVoList}" var="vo2">
                                                <a href="${ctx}/Product?action=queryProductList&category=${vo2.productCategories.id}&level=3">${vo2.productCategories.name}</a> |
                                            </c:forEach>
                                        </div>
                                    </c:forEach>
                                </div>
                            </div>
                        </li>
                    </c:forEach>
                </ul>
            </div>

数据库设计

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
三级联动下拉菜单是指在一个下拉菜单中选择某一项后,会根据该项的值,在另一下拉菜单中展示相应的选项,以此类推,构成一个层级结构的下拉菜单。在基于AJAX的JavaWeb开发中,可以通过异步请求获取后台数据,实现三级联动下拉菜单的动态更新。 以下是一个基于AJAX的三级联动下拉菜单实现步骤: 1. 创建三个下拉菜单,分别表示三个层级的选项。 2. 在页面加载时,通过AJAX请求获取第一级选项的数据,并将其填充到第一个下拉菜单中。 3. 给第一个下拉菜单添加change事件监听器,在选项改变时,获取选中的值,并通过AJAX请求获取该值对应的第二级选项数据,并将其填充到第二个下拉菜单中。 4. 给第二个下拉菜单添加change事件监听器,在选项改变时,获取选中的值,并通过AJAX请求获取该值对应的第三级选项数据,并将其填充到第三个下拉菜单中。 5. 最终选中的三个层级的值可以通过JavaScript代码拼接而成,或者提交给后台进行处理。 下面是一个简单的示例代码: ```html <!-- 页面中的三个下拉菜单 --> <select id="level1"> <option value="">请选择</option> </select> <select id="level2"> <option value="">请选择</option> </select> <select id="level3"> <option value="">请选择</option> </select> <script type="text/javascript"> // 页面加载时获取第一级选项数据 $(function() { $.ajax({ type: "GET", url: "getLevel1Data.action", success: function(data) { // 将数据填充到第一级下拉菜单中 $("#level1").html(data); } }); }); // 第一级下拉菜单改变时获取第二级选项数据 $("#level1").change(function() { var level1Value = $(this).val(); if (level1Value != "") { $.ajax({ type: "GET", url: "getLevel2Data.action", data: {level1Value: level1Value}, success: function(data) { // 将数据填充到第二级下拉菜单中 $("#level2").html(data); } }); } else { $("#level2").html("<option value=''>请选择</option>"); $("#level3").html("<option value=''>请选择</option>"); } }); // 第二级下拉菜单改变时获取第三级选项数据 $("#level2").change(function() { var level2Value = $(this).val(); if (level2Value != "") { $.ajax({ type: "GET", url: "getLevel3Data.action", data: {level2Value: level2Value}, success: function(data) { // 将数据填充到第三级下拉菜单中 $("#level3").html(data); } }); } else { $("#level3").html("<option value=''>请选择</option>"); } }); </script> ``` 在上述代码中,使用了jQuery库来简化AJAX请求的操作,具体实现方式可以根据实际需求进行修改。另外,需要在后台编写相应的处理方法来返回各级选项的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值