实战SSM_O2O商铺_18【商铺编辑】Service层开发
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_18【商铺编辑】Service层开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 結構
- Service層接口及其實現類
- 單元測試
- Github地址
概述
既然是商鋪編輯,肯定要根據入參shopId獲取shop信息,然后用戶在客戶端修改店鋪信息后,提交到后臺更新商鋪信息。
所以同樣的對于Service層來講 有2個方法 (DAO層也有對應的兩個方法,只不過updateShop我們復用了)
-
Shop getShopById(long shopId);
-
ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException;
結構
Service層接口及其實現類
com.artisan.o2o.service.ShopService 接口新增兩個接口方法如下
/*** * * @Title: getShopById* * @Description: 根據shopId查詢商鋪* * @param shopId* @return* * @return: Shop*/Shop getShopById(long shopId);/*** * * @Title: modifyShop* * @Description: 編輯商鋪信息* * @param shop* @param shopFileInputStream* @param fileName* @return* * @return: ShopExecution*/ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException;com.artisan.o2o.service.impl.ShopServiceImpl.java實現類
@Overridepublic Shop getShopById(long shopId) {return shopDao.selectShopById(shopId);}@Override@Transactionalpublic ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException {if (shop == null || shop.getShopId() == null) {return new ShopExecution(ShopStateEnum.NULL_SHOP_INFO);}else{try {// 1. 判斷是否需要處理圖片if (shopFileInputStream != null && fileName != null && !"".equals(fileName)) {// 1.1 刪除掉舊的圖片// 查詢入參shop對應數據庫表中的shopImg路徑Shop tempShop = shopDao.selectShopById(shop.getShopId());if (tempShop != null) {// 刪除就的縮略圖ImageUtil.deleteStorePath(tempShop.getShopImg());}// 1.2 用新的圖片生成縮略圖addShopImg(shop, shopFileInputStream, fileName);}// 2. 更新店鋪信息// 2.1 更新一些必要屬性shop.setLastEditTime(new Date());// 2.2 更新店鋪int effectedNum = shopDao.updateShop(shop);if (effectedNum <= 0) {throw new ShopOperationException(ShopStateEnum.INNER_ERROR.getStateInfo());}return new ShopExecution(ShopStateEnum.SUCCESS, shop);} catch (Exception e) {e.printStackTrace();throw new ShopOperationException("modify shop error:" + e.getMessage());}}}因為用戶有可能更新圖片,其中為了刪除舊的文件或者目錄,com.artisan.o2o.util.ImageUtil.java 新增了工具類
/*** * * @Title: deleteStorePath* * @Description: 判斷storePath是否為目錄,為目錄的話刪掉目錄下的所有文件,否則刪掉文件* * @param storePath* * @return: void*/public static void deleteStorePath(String storePath) {File fileOrMenu = new File(FileUtil.getImgBasePath() + storePath);if (fileOrMenu != null) {if (fileOrMenu.isDirectory()) {File[] files = fileOrMenu.listFiles();for (int i = 0; i < files.length; i++) {files[i].delete();}}fileOrMenu.delete();}}單元測試
@Testpublic void testModifyShop() {Shop shop = new Shop();Area area = new Area();ShopCategory shopCategory = new ShopCategory();shop.setShopId(28L);area.setAreaId(2);shopCategory.setShopCategoryId(2L);shop.setArea(area);shop.setShopCategory(shopCategory);shop.setShopName("Modify咖啡店");shop.setShopDesc("Modify小工匠的咖啡店");shop.setShopAddr("Modify-NanJing");shop.setPhone("123456");shop.setPriority(78);File shopFile = new File("D:/o2o/artisan.jpg");ShopExecution se = null;InputStream ins = null;try {ins = new FileInputStream(shopFile);se = shopService.modifyShop(shop, ins, shopFile.getName());} catch (FileNotFoundException e) {e.printStackTrace();}Assert.assertEquals(ShopStateEnum.SUCCESS.getState(), se.getState());}運行符合預期。
控制臺日志
Creating a new SqlSession Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@3fed2870] will 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.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id AND s.shop_id = ? ==> Parameters: 28(Long) <== 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小工匠的咖啡店, Modify-NanJing, 123456, \upload\item\shopImage\28\2018060301211217157.jpg, 78, 2018-05-28 23:13:37.0, 2018-06-03 01:21:14.0, 0, null, 1, 北京, 1, 咖啡奶茶 <== Total: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] from current transaction ==> Preparing: update tb_shop SET shop_name=?, shop_desc=?, shop_addr=?, phone=?, shop_img=?, priority=?, last_edit_time=?, area_id=?, shop_category_id=? where shop_id = ? ==> Parameters: Modify咖啡店(String), Modify小工匠的咖啡店(String), Modify-NanJing(String), 123456(String), \upload\item\shopImage\28\2018060301223045572.jpg(String), 78(Integer), 2018-06-03 01:22:31.968(Timestamp), 2(Integer), 2(Long), 28(Long) <== Updates: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]庫表字段正常
舊的圖片被刪除,新的圖片OK
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_18【商铺编辑】Service层开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_17【商铺编辑】
- 下一篇: 实战SSM_O2O商铺_19【商铺编辑】