java中菜单分几级_JavaWeb三级菜单分类查询详解
廢話不多說,直接貼代碼:
dao層代碼:
mapper:
ListselectByParentId(Integer id);
mapper.xml
select
from easybuy_product_category
where parentId = #{parentId,jdbcType=INTEGER}
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(){
ListproductCategories = productCategoryMapper.selectByParentId(0);
System.out.println(productCategories);
}
}
service:
ListgetProductCategoryList();
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 ListgetProductCategoryList() {
ListproductCategoryQueryVoList = new ArrayList<>();
ListproductCategory1 = productCategoryMapper.selectByParentId(0);
for (ProductCategory p1 : productCategory1) {
productCategoryQueryVo productCategoryQueryVo = new productCategoryQueryVo();
/*一級分類中的二級分類*/
ListproductCategory2 = productCategoryMapper.selectByParentId(p1.getId());
for (ProductCategory p2 : productCategory2) {
productCategoryQueryVo productCategoryQueryVo1 = new productCategoryQueryVo();
ListproductCategories3 = 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){
ListproductCategoryVoList = productCategoryService.getProductCategoryList();
model.addAttribute("productCategoryVoList",productCategoryVoList);
return "jsp/pre/index.jsp";
}
}
JSP
數據庫設計
總結
以上是生活随笔為你收集整理的java中菜单分几级_JavaWeb三级菜单分类查询详解的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: java中 下列不合法的语句_在Java
 - 下一篇: java解析java源码_JAVA语言-