JavaWeb三级菜单分类查询详解

JavaWeb三级菜单分类查询详解_第1张图片

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

dao层代码:

mapper:

List selectByParentId(Integer id);

mapper.xml

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 productCategories = productCategoryMapper.selectByParentId(0);
        System.out.println(productCategories);
    }
}

service:

List 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 getProductCategoryList() {

        List productCategoryQueryVoList = new ArrayList<>();

        List productCategory1 = productCategoryMapper.selectByParentId(0);
        for (ProductCategory p1 : productCategory1) {
            productCategoryQueryVo productCategoryQueryVo = new productCategoryQueryVo();
            /*一级分类中的二级分类*/
            List productCategory2 = productCategoryMapper.selectByParentId(p1.getId());
            for (ProductCategory p2 : productCategory2) {
                productCategoryQueryVo productCategoryQueryVo1 = new productCategoryQueryVo();
                List 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 productCategoryVoList = productCategoryService.getProductCategoryList();
        model.addAttribute("productCategoryVoList",productCategoryVoList);
        return "jsp/pre/index.jsp";
    }
}

JSP

数据库设计

JavaWeb三级菜单分类查询详解_第2张图片

你可能感兴趣的:(SSM框架整合)