实战SSM_O2O商铺_25【商品类别】商品类别列表展示从Dao到View层的开发
文章目錄
- 概述
- Dao層
- ProductCategoryDao接口
- ProductCategoryDao Mapper配置文件
- 單元測試
- Service層
- ProductCategoryService 接口
- ProductCategoryServiceImpl接口實現(xiàn)類
- 單元測試
- Controller層
- 泛型類 Result
- 狀態(tài)信息ProductCategoryStateEnum
- 控制類ProductCategoryController
- ShopAdminController 添加路由信息轉發(fā)到頁面
- 單元測試
- View層
- productcategorymanage.html
- productcategorymanage.js
- productcategorymanage.css
- 運行測試
- Github地址
概述
進入owner擁有的店鋪列表后,對某個店鋪進行管理,其中類別管理模塊的效果如上。所以獲取商品類別的時候要傳入shopId.
通過前面的博客,我們對開發(fā)流程有了較為清晰的認識,這里我們將類別管理這部分的內(nèi)容從下至上來實現(xiàn)下吧。
Dao層
ProductCategoryDao接口
package com.artisan.o2o.dao;import java.util.List;import com.artisan.o2o.entity.ProductCategory;public interface ProductCategoryDao {List<ProductCategory> selectProductCategoryList(long shopId); }ProductCategoryDao 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.artisan.o2o.dao.ProductCategoryDao"><select id="selectProductCategoryList" resultType="ProductCategory">SELECTtpc.product_category_id,tpc.product_category_name,tpc.product_category_desc,tpc.priority,tpc.create_time,tpc.last_edit_time,tpc.shop_idFROMtb_product_category tpcWHEREtpc.shop_id = #{shopId}ORDER BYpriority DESC</select> </mapper>單元測試
構造測試數(shù)據(jù)
編寫單元測試
package com.artisan.o2o.dao;import static org.junit.Assert.assertEquals;import java.util.List;import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ProductCategory;public class ProductCategoryTest extends BaseTest {@AutowiredProductCategoryDao productCategoryDao;@Testpublic void testSelectProductCategoryList() {long shopId = 5L;List<ProductCategory> productCategories = productCategoryDao.selectProductCategoryList(shopId);// shopId = 5 有2條測試數(shù)據(jù),期望list中有2條assertEquals(2, productCategories.size());// SQL中按照權重排序, product1 priority 99 ,期望第一條數(shù)據(jù)是 product1assertEquals("product1", productCategories.get(0).getProductCategoryName());for (ProductCategory productCategory : productCategories) {System.out.println(productCategory.toString());}productCategories = productCategoryDao.selectProductCategoryList(6L);assertEquals(0, productCategories.size());} }檢查是否符合預期,單元測試通過
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@60856961] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 5(Long) <== Columns: product_category_id, product_category_name, product_category_desc, priority, create_time, last_edit_time, shop_id <== Row: 1, product1, product1_desc, 99, 2018-06-09 01:49:15.0, 2018-06-09 01:49:15.0, 5 <== Row: 2, product2, product2_desc, 0, 2018-06-09 01:49:40.0, 2018-06-09 01:49:40.0, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ff8d125] ProductCategory [productCategoryId=1, shopId=5, productCategoryName=product1, productCategoryDesc=product1_desc, priority=99, createTime=Sat Jun 09 01:49:15 BOT 2018, lastEditTime=Sat Jun 09 01:49:15 BOT 2018] ProductCategory [productCategoryId=2, shopId=5, productCategoryName=product2, productCategoryDesc=product2_desc, priority=0, createTime=Sat Jun 09 01:49:40 BOT 2018, lastEditTime=Sat Jun 09 01:49:40 BOT 2018] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@34c01041] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@c94fd30] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 6(Long) <== Total: 0 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@34c01041]Service層
ProductCategoryService 接口
package com.artisan.o2o.service;import java.util.List;import com.artisan.o2o.entity.ProductCategory;public interface ProductCategoryService {List<ProductCategory> queryProductCategoryList(long shopId);}ProductCategoryServiceImpl接口實現(xiàn)類
package com.artisan.o2o.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.artisan.o2o.dao.ProductCategoryDao; import com.artisan.o2o.entity.ProductCategory; import com.artisan.o2o.service.ProductCategoryService;/*** * * @ClassName: ProductCategoryServiceImpl* * @Description: 使用@Service,交由Spring托管* * @author: Mr.Yang* * @date: 2018年6月9日 上午2:46:07*/@Service public class ProductCategoryServiceImpl implements ProductCategoryService {@Autowiredprivate ProductCategoryDao productCategoryDao;@Overridepublic List<ProductCategory> queryProductCategoryList(long shopId) {return productCategoryDao.selectProductCategoryList(shopId);}}單元測試
編寫單元測試用例
package com.artisan.o2o.service;import java.util.List;import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ProductCategory;public class ProductCategoryServiceTest extends BaseTest {@Autowiredprivate ProductCategoryService productCategoryService;@Testpublic void testQueryProductCategoryList() {long shopId = 5L;List<ProductCategory> productCategories = productCategoryService.queryProductCategoryList(shopId);Assert.assertNotNull(productCategories);Assert.assertEquals(2, productCategories.size());for (ProductCategory productCategory : productCategories) {System.out.println(productCategory.toString());}}}檢查是否符合預期,單元測試通過
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@60856961] will not be managed by Spring ==> Preparing: SELECT tpc.product_category_id, tpc.product_category_name, tpc.product_category_desc, tpc.priority, tpc.create_time, tpc.last_edit_time, tpc.shop_id FROM tb_product_category tpc WHERE tpc.shop_id = ? ORDER BY priority DESC ==> Parameters: 5(Long) <== Columns: product_category_id, product_category_name, product_category_desc, priority, create_time, last_edit_time, shop_id <== Row: 1, product1, product1_desc, 99, 2018-06-09 01:49:15.0, 2018-06-09 01:49:15.0, 5 <== Row: 2, product2, product2_desc, 0, 2018-06-09 01:49:40.0, 2018-06-09 01:49:40.0, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ff8d125] ProductCategory [productCategoryId=1, shopId=5, productCategoryName=product1, productCategoryDesc=product1_desc, priority=99, createTime=Sat Jun 09 01:49:15 BOT 2018, lastEditTime=Sat Jun 09 01:49:15 BOT 2018] ProductCategory [productCategoryId=2, shopId=5, productCategoryName=product2, productCategoryDesc=product2_desc, priority=0, createTime=Sat Jun 09 01:49:40 BOT 2018, lastEditTime=Sat Jun 09 01:49:40 BOT 2018]Controller層
泛型類 Result
package com.artisan.o2o.dto;/*** * * @ClassName: Result* * @Description: 封裝json對象,所有返回結果都使用它* * @author: Mr.Yang* * @date: 2018年6月16日 上午1:55:55* * @param <T>*/ public class Result<T> {// 是否成功的標識private boolean success;// 成功時返回的數(shù)據(jù)private T data;// 錯誤碼private int errorCode;// 錯誤信息private String errMsg;/*** * @Title:Result* * @Description:空的構造函數(shù)*/public Result() {super();}/*** * @Title:Result* * @Description:數(shù)據(jù)獲取成功時使用的構造器* * @param success* @param data*/public Result(boolean success, T data) {this.success = success;this.data = data;}/**** * * @Title:Result* * @Description:數(shù)據(jù)獲取失敗時使用的構造器* * @param success* @param errorCode* @param errMsg*/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 int getErrorCode() {return errorCode;}public void setErrorCode(int errorCode) {this.errorCode = errorCode;}public String getErrMsg() {return errMsg;}public void setErrMsg(String errMsg) {this.errMsg = errMsg;}}前面博文的代碼中,我們都是使用的Map<String, Object>作為返回值,然后通過@ResponseBody轉換為JSON對象(也可以繼續(xù)使用Map<String, Object>,一樣的效果)。 這里我們換另外一種方式,將操作結果以及數(shù)據(jù)存到到List中,然后使用Result泛型類,返回Result給前臺。
狀態(tài)信息ProductCategoryStateEnum
將對ProductCategoryState的狀態(tài)信息封裝到ProductCategoryStateEnum中
package com.artisan.o2o.enums;public enum ProductCategoryStateEnum {INNER_ERROR(-1001, "操作失敗"), NULL_SHOP(-1002, "Shop信息為空");private int state ;private String stateInfo;/*** * * @Title:ProductCategoryStateEnum* * @Description:構造函數(shù)* * @param state* @param stateInfo*/private ProductCategoryStateEnum(int state, String stateInfo) {this.state = state;this.stateInfo = stateInfo;}public int getState() {return state;}public String getStateInfo() {return stateInfo;}/*** * * @Title: stateOf* * @Description: * 通過state獲取productCategoryStateEnum,從而可以調(diào)用ProductCategoryStateEnum* #getStateInfo()獲取stateInfo* * @param index* @return* * @return: ProductCategoryStateEnum*/public static ProductCategoryStateEnum stateOf(int index) {for (ProductCategoryStateEnum productCategoryStateEnum : values()) {if (productCategoryStateEnum.getState() == index) {return productCategoryStateEnum;}}return null;}}控制類ProductCategoryController
package com.artisan.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.artisan.o2o.dto.Result; import com.artisan.o2o.entity.ProductCategory; import com.artisan.o2o.entity.Shop; import com.artisan.o2o.enums.ProductCategoryStateEnum; import com.artisan.o2o.service.ProductCategoryService;@Controller @RequestMapping(value = "/shopadmin") public class ProductCategoryController {@Autowiredprivate ProductCategoryService productCategoryService;/*** * * @Title: getProductCategoryByShopId* * @Description: 根據(jù)ShopId獲取productCategory* * @param request* * @return: Result<List<ProductCategory>>*/@RequestMapping(value = "/getproductcategorybyshopId", method = RequestMethod.GET)@ResponseBodypublic Result<List<ProductCategory>> getProductCategoryByShopId(HttpServletRequest request) {List<ProductCategory> productCategoryList ;ProductCategoryStateEnum ps;// 在進入到// shop管理頁面(即調(diào)用getShopManageInfo方法時),如果shopId合法,便將該shop信息放在了session中,key為currentShop// 這里我們不依賴前端的傳入,因為不安全。 我們在后端通過session來做Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");if (currentShop != null && currentShop.getShopId() != null) {try {productCategoryList = productCategoryService.queryProductCategoryList(currentShop.getShopId());return new Result<List<ProductCategory>>(true, productCategoryList);} catch (Exception e) {e.printStackTrace();ps = ProductCategoryStateEnum.INNER_ERROR;return new Result<List<ProductCategory>>(false, ps.getState(), ps.getStateInfo());}} else {ps = ProductCategoryStateEnum.NULL_SHOP;return new Result<List<ProductCategory>>(false, ps.getState(), ps.getStateInfo());}}}ShopAdminController 添加路由信息轉發(fā)到頁面
@RequestMapping(value = "/productcategorymanage", method = RequestMethod.GET)public String productcategoryManage() {return "shop/productcategorymanage";}單元測試
啟動tomcat,訪問
http://localhost:8080/o2o/shopadmin/getproductcategorybyshopId?shopId=5或者
http://localhost:8080/o2o/shopadmin/getproductcategorybyshopId我們這里不依賴前臺的入?yún)?#xff0c;當用戶進入管理頁面時,我們將shop的信息寫入session中,從session中獲取shop的信息,當直接訪問改方法時,因為session中沒有數(shù)據(jù),所以返回
如果從shop列表頁面進入后,然后再訪問上述URL ,可以得到如下返回,JSON類型的數(shù)據(jù)
View層
根據(jù)控制層的路由規(guī)則以及viewResolver
需要在shop目錄下編寫 productcategorymanage.html
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">優(yōu)先級</div><div class="col-33">操作</div></div><!-- 通過js從后臺加載數(shù)據(jù),動態(tài)的添加內(nèi)容 --><div class="product-categroy-wrap"></div></div><!-- 預占兩個按鈕,后續(xù)完善 --><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>productcategorymanage.js
$(function () {var shopId = getQueryString("shopId");var productCategoryURL = '/o2o/shopadmin/getproductcategorybyshopId?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);}});});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; }運行測試
可以通過前臺debug和后臺debug的方式,一步步的聯(lián)調(diào),會得到如下結果。
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_25【商品类别】商品类别列表展示从Dao到View层的开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle-维护存在主键的分区表时的注
- 下一篇: Oracle-Oracle SQL Re