文章目录
-
-
- 1. Dao层的实现
-
- 1. ProductCategoryDao接口
- 2. ProductCategoryDao.xml实现类
- 3. ProductCategoryDaoTest测试类
- 2. Service层的实现
-
- 1. ProductCategoryService接口
- 2. ProductCategoryServiceImpl实现类
- 3. Controller层的实现
-
- 3.1 DTO之Result类
- 3.2 ProductCategoryStateEnum类
- 3.3 Controller层的实现
- 4. View层的实现
-
- 4.1 productcategorymanage.html
- 4.2 productcategorymanage.js
- 4.3 productcategorymanage.css
- 4.4 ShopAdminController添加路由
-
1. Dao层的实现
1. ProductCategoryDao接口
import com.imooc.o2o.entity.ProductCategory;
import java.util.List;
public interface ProductCategoryDao {
/**
* 根据店铺id查询商品类别信息
* @param shopId
* @return
*/
List<ProductCategory> queryProductCategoryList(long shopId);
}
2. ProductCategoryDao.xml实现类
<?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.imooc.o2o.dao.ProductCategoryDao">
<select id="queryProductCategoryList"
resultType="com.imooc.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
</select>
</mapper>
3. ProductCategoryDaoTest测试类
在数据库中添加测试信息:
import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.ProductCategory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class ProductCategoryDaoTest extends BaseTest {
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
public void testQueryByShopId() throws Exception{
long shopId=142;
List<ProductCategory> productCategoryList
= productCategoryDao.queryProductCategoryList(shopId);
System.out.println("该店铺自定义类别数为:"+productCategoryList.size());
}
}
2. Service层的实现
1. ProductCategoryService接口
import com.imooc.o2o.dao.ProductCategory;
import java.util.List;
public interface ProductCategoryService {
/**
* 查询指定某个店铺下的所有商品类别信息
* @param shopId
* @return
*/
List<ProductCategory> getProductCategoryList(Long shopId);
2. ProductCategoryServiceImpl实现类
import com.imooc.o2o.dao.ProductCategory;
import com.imooc.o2o.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
@Autowired
private ProductCategoryDao productCategoryDao;
@Override
public List<ProductCategory> getProductCategoryList(Long shopId) {
return productCategoryDao.queryProductCategoryList(shopId);
}
}
3. Controller层的实现
3.1 DTO之Result类
/**
* 封装json对象,所有的返回结果都使用它
*/
public class Result<T> {
private boolean success;//是否成功的标志
private T data;//成功时返回的数据
private int errorCode;
private String errMsg;//错误信息
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 ProductCategoryStateEnum类
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 productCategoryStateEnum : values()) {
if (productCategoryStateEnum.getState() == index) {
return productCategoryStateEnum;
}
}
return null;
}
}
3.3 Controller层的实现
import com.imooc.o2o.dto.Result;
import com.imooc.o2o.entity.ProductCategory;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.enums.ProductCategoryStateEnum;
import com.imooc.o2o.service.ProductCategoryService;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping(value = "/shopadmin")
public class ProductCategoryManagementController {
@Autowired
private ProductCategoryService productCategoryService;
@ResponseBody
@RequestMapping(value = "/getproductcategorylist",method = RequestMethod.GET)
private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
Shop shop = new Shop();
shop.setShopId(142L);
request.getSession().setAttribute("currentShop",shop);
Shop currentShop = (Shop)request.getSession().getAttribute("currentShop");
List<ProductCategory> list = null;
if(currentShop!=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());
}
}
}
4. View层的实现
4.1 productcategorymanage.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="/o2o/favicon.ico">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<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">
<!-- 自定义的CSS -->
<link rel="stylesheet"
href="../resources/css/shop/productcategorymanage.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>
<!-- 通过js从后台加载数据,动态的添加内容 -->
<div class="product-categroy-wrap"></div>
</div>
<!-- 预占两个按钮,后续完善 -->
<div class="content-block">
<div class="row">
<div class="col-50">
<a href="#" class="button button-big button-fill button-success"
id="new">新增</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/common/common.js'
charset='utf-8'></script>
<script type='text/javascript'
src='../resources/js/shop/productcategorymanage.js' charset='utf-8'></script>
</body>
</html>
4.2 productcategorymanage.js
$(function () {
var shopId = getQueryString("shopId");
var productCategoryURL = '/o2o/shopadmin/getproductcategorylist?shopId=' + shopId;
$.getJSON(productCategoryURL,function(data){
if (data.success) {
var dataList = data.data;
$('.product-categroy-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>';
});
$('.product-categroy-wrap').append(tempHtml);
}
});
});
4.3 productcategorymanage.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.4 ShopAdminController添加路由
@RequestMapping(value="/productcategorymanage")
public String productCategoryManage(){
return "shop/productcategorymanage";
}