实战SSM_O2O商铺_22【商铺列表】Service层开发
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_22【商铺列表】Service层开发
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 概述
- ShopService接口新增接口方法
- ShopServie接口實現(xiàn)類
- 分頁工具類
- 單元測試
- Github地址
概述
ShopService接口中僅需要定義一個接口方法,在該接口方法的實現(xiàn)類中調用DAO層的兩個方法 selectShopList 和 selectShopCount ,并將數(shù)據(jù)封裝到ShopExecution中,以便控制層獲取數(shù)據(jù),在View層做展示。
ShopExecution 這里增加了一個空構造函數(shù),方便實例化并調用set方法,將shopList和shopCount 封裝到ShopExecution 中
ShopService接口新增接口方法
/*** * * @Title: getShopList* * @Description: 獲取商鋪列表. 在這一個方法中同樣的會調用查詢總數(shù)的DAO層方法,封裝到ShopExecution中* * @param shopCondition* @param pageIndex* 前端頁面 只有第幾頁 第幾頁 定義為pageIndex* @param pageSize* 展示的行數(shù)* @throws ShopOperationException* * @return: ShopExecution*/ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) throws ShopOperationException;;ShopServie接口實現(xiàn)類
@Overridepublic ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) throws ShopOperationException {// 前臺頁面插入的pageIndex(第幾頁), 而dao層是使用 rowIndex (第幾行) ,所以需要轉換一下int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);List<Shop> shopList = new ArrayList<Shop>();ShopExecution se = new ShopExecution();// 查詢帶有分頁的shopListshopList = shopDao.selectShopList(shopCondition, rowIndex, pageSize);// 查詢符合條件的shop總數(shù)int count = shopDao.selectShopCount(shopCondition);// 將shopList和 count設置到se中,返回給控制層if (shopList != null) {se.setShopList(shopList);se.setCount(count);} else {se.setState(ShopStateEnum.INNER_ERROR.getState());}return se;}分頁工具類
負責將前臺的PageIndex轉換為DAO層的rowIndex.
package com.artisan.o2o.util;/*** * * @ClassName: PageCalculator* * @Description: 將前臺使用的pageIndex 轉換為 dao層使用的 rowIndex* * @author: Mr.Yang* * @date: 2018年6月7日 上午12:24:38*/ public class PageCalculator {public static int calculateRowIndex(int pageIndex, int pageSize) {return (pageIndex > 0) ? (pageIndex - 1) * pageSize : 0;} }單元測試
tb_shop中的數(shù)據(jù)
@Testpublic void testGetShopList() {Shop shopCondition = new Shop();PersonInfo personInfo = new PersonInfo();personInfo.setUserId(1L);shopCondition.setOwner(personInfo);shopCondition.setShopName("咖啡");// 符合 shop_name like '%咖啡%' 且 owner_id =1 有3條數(shù)據(jù),// 第二個參數(shù) 和 第三個參數(shù) 從pageIndex=1 第一頁取數(shù)據(jù),取2條 pageSize=2ShopExecution se = shopService.getShopList(shopCondition, 1, 2);// 按照tb_shop中的數(shù)據(jù)篩選 符合條件的數(shù)據(jù)3條, 從第一頁開始取2條,se.getShopList().size() 應該有2條數(shù)據(jù),Assert.assertNotNull(se);Assert.assertEquals(2, se.getShopList().size());Assert.assertEquals(3, se.getCount());// 按照tb_shop中的數(shù)據(jù)篩選 符合條件的數(shù)據(jù)3條, 從第2頁開始取2條,se.getShopList().size()// 應該只有1條數(shù)據(jù),總數(shù)仍為3se = shopService.getShopList(shopCondition, 2, 2);Assert.assertNotNull(se);Assert.assertEquals(1, se.getShopList().size());Assert.assertEquals(3, se.getCount());} JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@24ba9639] will not be managed by Spring ==> Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ORDER BY s.priority DESC LIMIT ? , ? ==> Parameters: 1(Long), 0(Integer), 2(Integer) <== Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name <== Row: 24, 咖啡點, 小工匠的咖啡店, NanJing, 9876553, \upload\item\shopImage\24\2018052118095757182.jpg, 99, 2018-05-21 18:09:57.0, 2018-05-21 18:09:57.0, 0, 審核中, 1, 北京, 1, 咖啡奶茶 <== Row: 25, 咖啡店Improve, 小工匠的咖啡店Improve, NanJing-Improve, 9876553, \upload\item\shopImage\25\2018052214472089649.jpg, 99, 2018-05-22 14:46:16.0, 2018-05-22 14:46:18.0, 0, 審核中Improve, 1, 北京, 1, 咖啡奶茶 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74bf1791] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@12f9af83] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7e6ef134] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ==> Parameters: 1(Long) <== Columns: count(1) <== Row: 3 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@12f9af83] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@2631f68c] will not be managed by Spring ==> Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ORDER BY s.priority DESC LIMIT ? , ? ==> Parameters: 1(Long), 2(Integer), 2(Integer) <== Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name <== Row: 28, Modify咖啡店, Modify小工匠的咖啡店28, Modify-NanJing218, 12345628, \upload\item\shopImage\28\2018060500333541696.jpg, 78, 2018-05-28 23:13:37.0, 2018-06-06 20:56:31.0, 0, null, 2, 上海, 2, 咖啡 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bcef303] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33308786] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ==> Parameters: 1(Long) <== Columns: count(1) <== Row: 3 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bcef303]Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_22【商铺列表】Service层开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_21【商铺列表】
- 下一篇: 实战SSM_O2O商铺_23【商铺列表】