废话不多说,直接贴代码:
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>
数据库设计