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