校园商铺-7商品类别模块-2商品类别列表从后到前
1.Dao层
准备数据
insert into tb_product_category(`product_category_name`,`priority`,`shop_id`)
values('店铺商品类别1',0,1),('店铺商品类别2',20,1),('店铺商品类别3',2,1);
1.1 建立Dao文件
package com.csj2018.o2o.dao;
import java.util.List;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryDao {
List<ProductCategory> queryProductCategoryList(long shopId);
}
1.2.建立mapper文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.csj2018.o2o.dao.ProductCategoryDao">
<select id="queryProductCategoryList" resultType="com.csj2018.o2o.entity.ProductCategory" parameterType="Long">
SELECT
product_category_id,
product_category_name,
priority,
create_time,
shop_id
from tb_product_category
where
shop_id = #{shopId}
order by priority desc
</select>
</mapper>
1.3.对Dao进行单元测试
package com.csj2018.o2o.dao;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
public class ProductCategoryDaoTest extends BaseTest {
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
public void testQueryByShopId() throws Exception {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
System.out.println("该店铺自定义商品类别数为:"+productCategoryList.size());
}
}
2.Service层
2.1建立Service接口
package com.csj2018.o2o.service;
import java.util.List;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryService {
/**
* 查询指定某个店铺下的所有商品类别信息
* @param shopId
* @return
*/
List<ProductCategory> getProductCategoryList(long shopId);
}
2.2 建立Service实现类
package com.csj2018.o2o.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.dao.ProductCategoryDao;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.service.ProductCategoryService;
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Autowired
private ProductCategoryDao productCategoryDao;
@Override
public List<ProductCategory> getProductCategoryList(long shopId) {
// TODO Auto-generated method stub
return productCategoryDao.queryProductCategoryList(shopId);
}
}
2.3 对Service层进行单元测试
package com.csj2018.o2o.service;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
public class ProductCategoryServiceTest extends BaseTest{
@Autowired
ProductCategoryService productCategoryService;
@Test
public void testGetbyShopid() {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryService.getProductCategoryList(shopId);
for(ProductCategory pc:productCategoryList) {
System.out.printf("%s 权重:%s",pc.getProductCategoryName(),pc.getPriority());
}
}
}
3.controller层
3.1 返回对象Result泛型
package com.csj2018.o2o.dto;
public class Result<T> {
private boolean success;//是否成功标志
private T data;//成功时返回的数据
private String errMsg;//错误信息
private int errorCode;
public Result() {}
//成功时的构造器
public Result(boolean success,T data) {
this.success = success;
this.data = data;
}
//错误时的构造器
public Result(boolean success,int errorCode,String errMsg) {
this.success = success;
this.errorCode = errorCode;
this.errMsg = errMsg;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}
3.2枚举类
package com.csj2018.o2o.enums;
public enum ProductCategoryStateEnum {
SUCCESS(1,"创建成功"),INNER_ERROR(-1001,"操作失败"),EMPTY_LIST(-1002,"添加数少于1");
private int state;
private String stateInfo;
private ProductCategoryStateEnum(int state,String stateInfo) {
this.state = state;
this.stateInfo = stateInfo;
}
public int getState() {
return state;
}
public String getStateInfo() {
return stateInfo;
}
public static ProductCategoryStateEnum stateOf(int index) {
for(ProductCategoryStateEnum state:values()) {
if(state.getState() == index) {
return state;
}
}
return null;
}
}
3.3 建立controller层
package com.csj2018.o2o.web.shopadmin;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.csj2018.o2o.dto.Result;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.enums.ProductCategoryStateEnum;
import com.csj2018.o2o.service.ProductCategoryService;
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
@Autowired
private ProductCategoryService productCategoryService;
@RequestMapping(value="/getproductcategorylist",method=RequestMethod.GET)
@ResponseBody
private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
//测试用,可删除
Shop shop = new Shop();
shop.setShopId(1L);
request.getSession().setAttribute("currentShop", shop);
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
List<ProductCategory> list = null;
if(currentShop.getShopId() != null && currentShop.getShopId()>0){
list = productCategoryService.getProductCategoryList(currentShop.getShopId());
return new Result<List<ProductCategory>>(true,list);
}else {
ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
return new Result<List<ProductCategory>>(false,ps.getState(),ps.getStateInfo());
}
}
}
测试controller,浏览器访问http://127.0.0.1:18080/o2o/shopadmin/getproductcategorylist,返回结果如下
4.前端开发
注意删除controller的测试数据
Shop shop = new Shop();
shop.setShopId(1L);
request.getSession().setAttribute("currentShop", shop);
4.1编写html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>商品类别</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="/favicon.ico">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet"
href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
<link rel="stylesheet"
href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
<link rel="stylesheet" href="../resources/css/shop/productcategorymanagement.css">
</head>
<body>
<header class="bar bar-nav">
<h1 class="title">商品类别</h1>
</header>
<div class="content">
<div class="content-block">
<div class="row row-product-category">
<div class="col-33">类别</div>
<div class="col-33">优先级</div>
<div class="col-33">操作</div>
</div>
<div class="category-wrap"></div>
</div>
<div class="content-block">
<div class="row">
<div class="col-50">
<a href="#" id="new" class = "button button-big button-fill button-success">新增</a>
</div>
<div class="col-50">
<a href="#" class="button button-big button-fill" id="submit">提交</a>
</div>
</div>
</div>
</div>
<script type='text/javascript'
src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript'
src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script type='text/javascript'
src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
<script type='text/javascript'
src='../resources/js/shop/productcategorymanagement.js' charset="utf-8"></script>
</body>
</html>
4.2依赖的js和css
webapp/resources/js/shop/productcategorymanagement.js
/**
*
*/
$(function(){
var listUrl = "/o2o/shopadmin/getproductcategorylist";
var addUrl = "/o2o/shopamdin/addproductcategory";
var deleteUrl = "/o2o/shopamdin/removecategory";
getList();
function getList(){
$.getJSON(listUrl,function(data){
if(data.success){
var dataList = data.data;
$('.category-wrap').html('');
var tempHtml = '';
dataList.map(function(item,index){
tempHtml += ''
+'<div class="row row-product-category now">'
+'<div class="col-33 product-category-name">' + item.productCategoryName + '</div>'
+'<div class="col-33">' + item.priority +'</div>'
+'<div class="col-33"><a href="#" class="button delete" data-id="' + item.productCategoryId +'">删除</a></div>'
+'</div>';
});
$('.category-wrap').append(tempHtml);
}
});
}
})
webapp/resources/css/shop/productcategorymanagement.css
.row-product-category{
border:1px solid #999;
padding:.5rem;
border-bottom:none;
}
.row-product-category:last-child{
border-bottom:1px solid #999;
}
.category-input{
border:none;
background-color:#eee;
}
.product-category-name{
white-space:nowrap;
overflow-x:scroll;
}
4.3建立路由productCategoryManage
package com.csj2018.o2o.web.shopadmin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="shopadmin",method=RequestMethod.GET)
public class ShopAdminCtroller {
@RequestMapping(value="/shopoperation")
public String shopOperation() {
return "shop/shopoperation";
}
@RequestMapping(value="/shoplist")
public String shopList() {
return "shop/shoplist";
}
@RequestMapping(value="/shopmanagement")
public String shopManagement() {
return "shop/shopmanagement";
}
//新增
@RequestMapping(value="/productcategorymanagement")
private String productCategoryManage() {
return "shop/productcategorymanagement";
}
}
从商铺页面访问商品分类
问题
1.商品类别模块为什么使用shopId,而不是Shop实体类?
因为我们获取productCategory时,并不需要获取除了shopId之外的信息,因此不用Shop实体类。
public class ProductCategory {
private Long productCategoryId;
private Long shopId;
private String productCategoryName;
private Integer priority;
private Date createTime;
}