日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实战SSM_O2O商铺_36【商品】商品列表之Dao+Service+Controller层的实现

發布時間:2025/3/21 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战SSM_O2O商铺_36【商品】商品列表之Dao+Service+Controller层的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • Dao層
    • ProductDao.java
    • ProductDao.xml
    • 單元測試
  • Service層
    • ProductService.java
    • ProductServiceImpl.java
    • 單元測試
  • Controller層
    • ProductController.java
  • 單元測試
  • Github地址

概述

接著繼續來完成商品列表的功能吧,需要支持分頁的功能。


Dao層

ProductDao.java

增加如下兩個接口

/*** * * @Title: selectProductList* * @Description: 支持分頁功能的查詢product* * 需要支持根據商品名稱(支持模糊查詢)、商品狀態、shopId、商品類別的查詢及組合查詢* * @param productCondition* @param rowIndex* 從第幾行開始取* @param pageSize* 返回多少行數據(頁面上的數據量)* * 比如 rowIndex為1,pageSize為5 即為 從第一行開始取,取5行數據* * @return: List<Product>*/List<Product> selectProductList(@Param("productCondition") Product productCondition, @Param("rowIndex") int rowIndex, @Param("pageSize") int pageSize);/*** * * @Title: selectCountProduct* * @Description: 按照條件查詢 符合前臺傳入的條件的商品的總數* * @param productCondition* @return* * @return: int*/int selectCountProduct(@Param("productCondition") Product productCondition);

ProductDao.xml

增加如下SQL映射

<sql id="selectProductByCondition"><!-- 根據shopId 查詢--><if test="productCondition.shop != null and productCondition.shop.shopId != null ">and shop_id = #{productCondition.shop.shopId}</if><!-- 根據product_category_id 查詢--><if test="productCondition.productCategory != null and productCondition.productCategory.productCategoryId != null">and product_category_id = #{productCondition.productCategory.productCategoryId}</if><!-- 根據enable_status 查詢--><if test="productCondition.enableStatus != null">and enable_status = #{productCondition.enableStatus}</if><!-- 根據product_name 模糊查詢--><if test="productCondition.productName != null">and product_name like '%${productCondition.productName}%'</if> </sql><select id="selectProductList" resultType="com.artisan.o2o.entity.Product">SELECTproduct_id,product_name,product_desc,img_addr,normal_price,promotion_price,priority,create_time,last_edit_time,enable_status,product_category_id,shop_idFROMtb_product <where><include refid="selectProductByCondition"></include></where>ORDER BY priority desc LIMIT #{rowIndex} ,#{pageSize}</select><select id="selectCountProduct" resultType="Integer">SELECTcount(1)FROMtb_product<where><include refid="selectProductByCondition"></include></where></select>

單元測試

@Testpublic void testC_SelectProductListAndCount() {int rowIndex = 1;int pageSize = 2;List<Product> productList = new ArrayList<Product>();int effectNum = 0;Shop shop = new Shop();shop.setShopId(5L);Product productCondition = new Product();productCondition.setShop(shop);productList = productDao.selectProductList(productCondition, rowIndex, pageSize);Assert.assertEquals(2, productList.size());effectNum = productDao.selectCountProduct(productCondition);Assert.assertEquals(7, effectNum);System.out.println("==========================================");Shop shop2 = new Shop();shop2.setShopId(5L);ProductCategory productCategory = new ProductCategory();productCategory.setProductCategoryId(36L);Product productCondition2 = new Product();productCondition2.setShop(shop2);productCondition2.setProductCategory(productCategory);productCondition2.setProductName("test");productList = productDao.selectProductList(productCondition2, rowIndex, pageSize);Assert.assertEquals(2, productList.size());effectNum = productDao.selectCountProduct(productCondition2);Assert.assertEquals(5, effectNum);}

結合tb_product中的記錄,驗證是否符合預期,這里單元測試通過。

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@29a0cdb] will not be managed by Spring ==> Preparing: SELECT product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id FROM tb_product WHERE shop_id = ? ORDER BY priority desc LIMIT ? ,? ==> Parameters: 5(Long), 1(Integer), 2(Integer) <== Columns: product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id <== Row: 2, modifyProduct, modifyProduct desc, /mmm/ddd, 350, 300, 66, 2018-06-29 17:46:46.0, 2018-06-30 16:02:01.0, 1, 36, 5 <== Row: 3, test_product, product desc, /aaa/bbb, 10, 8, 66, 2018-06-24 18:45:36.0, 2018-06-24 18:45:36.0, 1, 36, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@492691d7] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7b94089b] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@47f9738] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_product WHERE shop_id = ? ==> Parameters: 5(Long) <== Columns: count(1) <== Row: 7 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7b94089b] ========================================== Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@388526fb] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6436a7db] will not be managed by Spring ==> Preparing: SELECT product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ORDER BY priority desc LIMIT ? ,? ==> Parameters: 5(Long), 36(Long), 1(Integer), 2(Integer) <== Columns: product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id <== Row: 4, test_product, product desc, \upload\item\shopImage\5\2018062515593428322.jpg, 10, 8, 66, 2018-06-25 15:58:16.0, 2018-06-25 15:58:17.0, 1, 36, 5 <== Row: 5, test_product, product desc, \upload\item\shopImage\5\2018062516124013361.jpg, 10, 8, 66, 2018-06-25 16:12:40.0, 2018-06-25 16:12:40.0, 1, 36, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@388526fb] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40238dd0] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@79179359] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ==> Parameters: 5(Long), 36(Long) <== Columns: count(1) <== Row: 5 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40238dd0]

Service層

Service接口層只需要提供一個方法即可,內部調用Dao層的兩個方法,將返回結果封裝到DTO中。

ProductService.java

/*** * * @Title: queryProductionList* * @Description: 查詢* * @param productCondition* @param pageIndex* 前端頁面 只有第幾頁 第幾頁 定義為pageIndex* @param pageSize* 一頁中展示的行數* @throws ProductOperationException* * @return: ProductExecution*/ProductExecution queryProductionList(Product productCondition, int pageIndex, int pageSize) throws ProductOperationException;

ProductServiceImpl.java

@Overridepublic ProductExecution queryProductionList(Product productCondition, int pageIndex, int pageSize) throws ProductOperationException {List<Product> productList = null;int count = 0;try {// 將pageIndex 轉換為Dao層識別的rowIndexint rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);// 調用Dao層獲取productList和總量productList = productDao.selectProductList(productCondition, rowIndex, pageSize);count = productDao.selectCountProduct(productCondition);} catch (Exception e) {e.printStackTrace();new ProductExecution(ProductStateEnum.INNER_ERROR);}return new ProductExecution(ProductStateEnum.SUCCESS, productList, count);}

單元測試

@Testpublic void testQueryProductListAndCount() {// 庫表中符合如下篩選條件的記錄為5條// select * from tb_product a where a.product_category_id = 36 and// a.shop_id = 5 and a.product_name like '%test%';// 從第1頁開始取,每頁取3條int pageIndex = 1;int pageSize = 3;Shop shop2 = new Shop();shop2.setShopId(5L);ProductCategory productCategory = new ProductCategory();productCategory.setProductCategoryId(36L);Product productCondition = new Product();productCondition.setShop(shop2);productCondition.setProductCategory(productCategory);productCondition.setProductName("test");ProductExecution productExecution = productService.queryProductionList(productCondition, pageIndex, pageSize);// 操作成功的狀態為1Assert.assertEquals(1, productExecution.getState());Assert.assertEquals(3, productExecution.getProductList().size());Assert.assertEquals(5, productExecution.getCount());// 從第2頁開始取,每頁依然取3條pageIndex = 2;productExecution = productService.queryProductionList(productCondition, pageIndex, pageSize);Assert.assertEquals(1, productExecution.getState());Assert.assertEquals(2, productExecution.getProductList().size());Assert.assertEquals(5, productExecution.getCount());}

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@177bea38] will not be managed by Spring ==> Preparing: SELECT product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ORDER BY priority desc LIMIT ? ,? ==> Parameters: 5(Long), 36(Long), 0(Integer), 3(Integer) <== Columns: product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id <== Row: 3, test_product, product desc, /aaa/bbb, 10, 8, 66, 2018-06-24 18:45:36.0, 2018-06-24 18:45:36.0, 1, 36, 5 <== Row: 4, test_product, product desc, \upload\item\shopImage\5\2018062515593428322.jpg, 10, 8, 66, 2018-06-25 15:58:16.0, 2018-06-25 15:58:17.0, 1, 36, 5 <== Row: 5, test_product, product desc, \upload\item\shopImage\5\2018062516124013361.jpg, 10, 8, 66, 2018-06-25 16:12:40.0, 2018-06-25 16:12:40.0, 1, 36, 5 <== Total: 3 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4716be8b] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@460ebd80] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@74f5ce22] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ==> Parameters: 5(Long), 36(Long) <== Columns: count(1) <== Row: 5 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@460ebd80] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@dbd8e44] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6a55299e] will not be managed by Spring ==> Preparing: SELECT product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ORDER BY priority desc LIMIT ? ,? ==> Parameters: 5(Long), 36(Long), 3(Integer), 3(Integer) <== Columns: product_id, product_name, product_desc, img_addr, normal_price, promotion_price, priority, create_time, last_edit_time, enable_status, product_category_id, shop_id <== Row: 6, test_product, product desc, \upload\item\shopImage\5\2018062516132272045.jpg, 10, 8, 66, 2018-06-25 16:13:22.0, 2018-06-25 16:13:22.0, 1, 36, 5 <== Row: 8, test_product, product desc, /aaa/bbb, 10, 8, 66, 2018-06-30 16:01:59.0, 2018-06-30 16:01:59.0, 1, 36, 5 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@dbd8e44] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5a18cd76] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@403f0a22] will not be managed by Spring ==> Preparing: SELECT count(1) FROM tb_product WHERE shop_id = ? and product_category_id = ? and product_name like '%test%' ==> Parameters: 5(Long), 36(Long) <== Columns: count(1) <== Row: 5 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5a18cd76]

Controller層

ProductController.java

增加如下路由方法

@RequestMapping(value = "/getproductlist", method = RequestMethod.GET)@ResponseBodyprivate Map<String, Object> queryProductList(HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();// 獲取前端傳遞過來的頁碼int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");// 獲取前端傳過來的每頁要求返回的商品數量int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");// 從session中獲取shop信息,主要是獲取shopId 不依賴前臺的參數,盡可能保證安全Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");// 空值判斷if ((pageIndex > -1) && (pageSize > -1) && currentShop != null && currentShop.getShopId() != null) {// 獲取前臺可能傳遞過來的需要檢索的條件,包括是否需要從某個商品類別以及根據商品名稱模糊查詢某個店鋪下的商品long productCategoryId = HttpServletRequestUtil.getLong(request, "productCategoryId");String productName = HttpServletRequestUtil.getString(request, "productName");// 拼裝查詢條件,根據前端傳入的條件進行組合Product productCondition = compactProductCondition4Search(currentShop.getShopId(), productCategoryId, productName);// 調用服務ProductExecution pe = productService.queryProductionList(productCondition, pageIndex, pageSize);// 將結果返回給前臺modelMap.put("productList", pe.getProductList());modelMap.put("count", pe.getCount());modelMap.put("success", true);} else {modelMap.put("success", false);modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");}return modelMap;}/*** * * @Title: compactProductCondition4Search* * @Description: 組裝查詢條件* * @param shopId* @param productCategoryId* @param productName* * @return: Product*/private Product compactProductCondition4Search(Long shopId, long productCategoryId, String productName) {Product productCondition = new Product();Shop shop = new Shop();shop.setShopId(shopId);productCondition.setShop(shop);if (productCategoryId != -1L) {ProductCategory productCategory = new ProductCategory();productCategory.setProductCategoryId(productCategoryId);productCondition.setProductCategory(productCategory);}if (productName != null) {productCondition.setProductName(productName);}return productCondition;}

單元測試

啟動tomcat的服務,第一次可以加入斷點,使用debug的方式啟動逐步調測該方法。

先獲取shoplist,然后進入shop管理頁面,使后端將該shop的信息寫入到session中。 因為這個方法的shop信息是從session中獲取的。

最后訪問

http://localhost:8080/o2o/shopadmin/getproductlist?pageIndex=1&&pageSize=8

根據數據庫中的記錄,合理設置pageIndex和pageSize,多次驗證獲取的數據是否符合預期。

{"success": true,"count": 7,"productList": [{"productId": 7,"productName": "offical_product1","productDesc": "product offical desc1","imgAddr": "\\upload\\item\\shopImage\\5\\2018070123313434331.png","normalPrice": "1001","promotionPrice": "801","priority": 661,"createTime": 1530286468000,"lastEditTime": 1530502295000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 2,"productName": "modifyProduct","productDesc": "modifyProduct desc","imgAddr": "/mmm/ddd","normalPrice": "350","promotionPrice": "300","priority": 66,"createTime": 1530308806000,"lastEditTime": 1530388921000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 3,"productName": "test_product","productDesc": "product desc","imgAddr": "/aaa/bbb","normalPrice": "10","promotionPrice": "8","priority": 66,"createTime": 1529880336000,"lastEditTime": 1529880336000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 4,"productName": "test_product","productDesc": "product desc","imgAddr": "\\upload\\item\\shopImage\\5\\2018062515593428322.jpg","normalPrice": "10","promotionPrice": "8","priority": 66,"createTime": 1529956696000,"lastEditTime": 1529956697000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 5,"productName": "test_product","productDesc": "product desc","imgAddr": "\\upload\\item\\shopImage\\5\\2018062516124013361.jpg","normalPrice": "10","promotionPrice": "8","priority": 66,"createTime": 1529957560000,"lastEditTime": 1529957560000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 6,"productName": "test_product","productDesc": "product desc","imgAddr": "\\upload\\item\\shopImage\\5\\2018062516132272045.jpg","normalPrice": "10","promotionPrice": "8","priority": 66,"createTime": 1529957602000,"lastEditTime": 1529957602000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null},{"productId": 8,"productName": "test_product","productDesc": "product desc","imgAddr": "/aaa/bbb","normalPrice": "10","promotionPrice": "8","priority": 66,"createTime": 1530388919000,"lastEditTime": 1530388919000,"enableStatus": 1,"productImgList": null,"productCategory": null,"shop": null}] }

Github地址

代碼地址: https://github.com/yangshangwei/o2o

總結

以上是生活随笔為你收集整理的实战SSM_O2O商铺_36【商品】商品列表之Dao+Service+Controller层的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美精品xx | 日韩每日更新 | 日韩在线观看一区二区 | 奇米色在线 | 黄网页在线观看 | 亚洲黄网在线观看 | 国产情侣第一页 | 噜噜色av| 人人妻人人澡人人爽精品欧美一区 | 丝袜诱惑一区 | 成人福利视频导航 | 欧美又大又硬又粗bbbbb | 黑人爱爱视频 | 色视频网站 | 日本护士╳╳╳hd少妇 | 四虎国产精品永久在线国在线 | 天天撸天天操 | 日韩色资源 | 日本在线看片 | 国产亚洲精品精品精品 | 国产精品xxx在线观看 | 天天干天天操天天舔 | 中文字幕一区二区三区四区不卡 | 亚洲av无码一区二区乱子伦 | 91精品国产乱码久久久久久久久 | a免费毛片 | 国产宾馆实践打屁股91 | 国内久久精品 | 1024日韩| 欧美黑人疯狂性受xxxxx野外 | 亚洲呦呦 | 蜜芽在线视频 | 40到50岁中老年妇女毛片 | 男人吃奶视频 | 日本在线第一页 | 韩国色网 | www.日本在线视频 | 亚洲综合色一区二区 | av在线影视 | 91黄色在线观看 | 成人网av| 黄色大片一级 | 免费吸乳羞羞网站视频 | 在线欧美一区二区 | a∨视频| 国产欧美日韩精品区一区二污污污 | 在线观看亚洲欧美 | 天天看片天天干 | 欧美精品一线 | 亚洲视频网站在线 | 人人爽夜夜爽 | 免费日本黄色 | 女同hd系列中文字幕 | www超碰在线 | 免费毛片看片 | 国产91高清| 91视频污在线观看 | 69久久夜色精品国产69 | 99久久毛片 | 欧美男同又粗又长又大 | h视频在线看 | 香港三日本三级少妇66 | 亚洲国产成人精品91久久久 | 秋霞国产一区 | 中文字幕欧美日韩 | 奇米影视第四色7777 | av 日韩 人妻 黑人 综合 无码 | 国产99在线观看 | 久久综合鬼色 | 色妞欧美 | 亚洲在线看片 | 免费a级 | 国产日韩成人内射视频 | 欧美黑人精品一区二区不卡 | 免费拍拍拍网站 | 欧美a级黄色 | 天天爽天天射 | 久久久久久久久久免费视频 | jzzjzz日本丰满成熟少妇 | 国产精品福利在线播放 | 99精品视频免费观看 | 天天色av | 精品无码三级在线观看视频 | 一区二区麻豆 | 国产一区二区三区网站 | 成人看| www.av在线 | 日本少妇xxxxxx | 国产老肥熟 | 青青操视频在线 | 性欧美欧美巨大69 | 国产精品久久久久久久免费看 | 日韩男人的天堂 | 日韩五码 | 激情欧美一区二区 | 亚洲欧美a | 一级日韩毛片 | 欧美男人操女人 | 精品国产乱码久久久久久影片 |