实战SSM_O2O商铺_33【商品】商品编辑之Service层的实现

网友投稿 475 2022-05-28

文章目录

概述

Service接口

Service接口实现类

单元测试

Github地址

概述

在完成了 Dao层的部分之后,顺其自然的我们来到了Service层,需要调用Dao层提供的操作数据库的方法。

主要步骤如下:

1. 如用户上传了缩略图,则将原有的缩略图删除(磁盘上删除),并更新tb_product表的img_addr字段,否则不做任何处理。

2. 如果用户上传了新的商品详情图片,则将原有的属于该productId下的全部的商品详情图删除(磁盘上删除),同时删除productId对应的tb_product_img中的全部数据。

3. 更新tb_product的信息

Service接口

新增两个接口如下:

/** * * * @Title: queryProductById * * @Description: 根据productId查询product * * @param productId * * @return: Product */ Product queryProductById(long productId); /** * * * @Title: modifyProduct * * @Description: TODO * * @param product * 产品信息 * @param imageHolder * 产品缩略图的封装信息 * @param prodImgDetailList * 产品详情图片的封装信息 * @throws ProductOperationException * * @return: ProductExecution */ ProductExecution modifyProduct(Product product, ImageHolder imageHolder, List prodImgDetailList) throws ProductOperationException;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Service接口实现类

/** * 注意事务控制@Transactional */ @Override @Transactional public ProductExecution modifyProduct(Product product, ImageHolder imageHolder, List prodImgDetailList) throws ProductOperationException { if (product != null && product.getShop() != null && product.getShop().getShopId() != null && product.getProductCategory().getProductCategoryId() != null) { // 设置默认的属性 product.setLastEditTime(new Date()); // Step1. 处理缩略图 if (imageHolder != null) { Product tempProduct = productDao.selectProductById(product.getProductId()); // 1.1 删除旧的缩略图 if (tempProduct.getImgAddr() != null) { ImageUtil.deleteStorePath(tempProduct.getImgAddr()); } // 1.2 添加新的缩略图 addProductImg(product, imageHolder); } // Step2. 处理商品详情 // 如果添加商品成功,继续处理商品详情图片,并写入tb_product_img if (prodImgDetailList != null && prodImgDetailList.size() > 0) { // 2.1 删除库表中productId对应的tb_product_img的信息 deleteProductImgs(product.getProductId()); // 2.2 处理商品详情图片,并写入tb_product_img addProductDetailImgs(product, prodImgDetailList); } try { // Step3.更新tb_product int effectNum = productDao.updateProduct(product); if (effectNum <= 0) { throw new ProductOperationException("商品更新失败"); } return new ProductExecution(ProductStateEnum.SUCCESS, product); } catch (Exception e) { throw new ProductOperationException("商品更新失败:" + e.getMessage()); } } else { return new ProductExecution(ProductStateEnum.NULL_PARAMETER); } } private void deleteProductImgs(Long productId) { // 获取该商铺下对应的productImg信息 List productImgList = productImgDao.selectProductImgList(productId); // 遍历删除该目录下的全部文件 for (ProductImg productImg : productImgList) { ImageUtil.deleteStorePath(productImg.getImgAddr()); } // 删除tb_product_img中该productId对应的记录 productImgDao.deleteProductImgById(productId); } @Override public Product queryProductById(long productId) { return productDao.selectProductById(productId); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

实战SSM_O2O商铺_33【商品】商品编辑之Service层的实现

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

单元测试

@Test public void testModifyProduct() throws Exception { // 注意表中的外键关系,确保这些数据在对应的表中的存在 ProductCategory productCategory = new ProductCategory(); productCategory.setProductCategoryId(36L); // 注意表中的外键关系,确保这些数据在对应的表中的存在 Shop shop = new Shop(); shop.setShopId(5L); // 构造Product Product product = new Product(); product.setProductName("offical_product"); product.setProductDesc("product offical desc"); product.setNormalPrice("100"); product.setPromotionPrice("80"); product.setPriority(66); product.setLastEditTime(new Date()); product.setProductCategory(productCategory); product.setShop(shop); product.setProductId(7L); // 构造 商品图片 File productFile = new File("D:/o2o/1.jpg"); InputStream ins = new FileInputStream(productFile); ImageHolder imageHolder = new ImageHolder(ins, productFile.getName()); // 构造商品详情图片 List prodImgDetailList = new ArrayList(); File productDetailFile1 = new File("D:/o2o/artisan.jpg"); InputStream ins1 = new FileInputStream(productDetailFile1); ImageHolder imageHolder1 = new ImageHolder(ins1, productDetailFile1.getName()); File productDetailFile2 = new File("D:/o2o/TIM.jpg"); InputStream ins2 = new FileInputStream(productDetailFile2); ImageHolder imageHolder2 = new ImageHolder(ins2, productDetailFile2.getName()); prodImgDetailList.add(imageHolder1); prodImgDetailList.add(imageHolder2); // 调用服务 ProductExecution pe = productService.modifyProduct(product, imageHolder, prodImgDetailList); Assert.assertEquals(ProductStateEnum.SUCCESS.getState(), pe.getState()); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

SQL日志如下:

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6f63b475] will be managed by Spring ==> Preparing: SELECT p.product_id, p.product_name, p.product_desc, p.img_addr, p.normal_price, p.promotion_price, p.priority, p.create_time, p.last_edit_time, p.enable_status, p.product_category_id, p.shop_id, pm.product_img_id, pm.img_addr, pm.img_desc, pm.priority, pm.create_time FROM tb_product p LEFT JOIN tb_product_img pm ON p.product_id =pm.product_id WHERE p.product_id = ? ORDER BY pm.priority DESC ==> Parameters: 7(Long) <== 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, product_img_id, img_addr, img_desc, priority, create_time <== Row: 7, 香飘飘, test添加商品, \upload\item\shopImage\5\2018062911342810920.png, 5, 3.5, 99, 2018-06-29 11:34:28.0, 2018-06-29 11:34:28.0, 1, 36, 5, 11, \upload\item\shopImage\5\20180629113433657450.jpg, null, null, 2018-06-29 11:34:37.0 <== Row: 7, 香飘飘, test添加商品, \upload\item\shopImage\5\2018062911342810920.png, 5, 3.5, 99, 2018-06-29 11:34:28.0, 2018-06-29 11:34:28.0, 1, 36, 5, 13, \upload\item\shopImage\5\20180629113434424572.jpg, null, null, 2018-06-29 11:34:37.0 <== Row: 7, 香飘飘, test添加商品, \upload\item\shopImage\5\2018062911342810920.png, 5, 3.5, 99, 2018-06-29 11:34:28.0, 2018-06-29 11:34:28.0, 1, 36, 5, 12, \upload\item\shopImage\5\20180629113433541021.jpg, null, null, 2018-06-29 11:34:37.0 <== Total: 3 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] from current transaction ==> Preparing: SELECT product_img_id, img_addr, img_desc, priority, create_time, product_id FROM tb_product_img WHERE product_id=? ORDER BY product_img_id ==> Parameters: 7(Long) <== Columns: product_img_id, img_addr, img_desc, priority, create_time, product_id <== Row: 11, \upload\item\shopImage\5\20180629113433657450.jpg, null, null, 2018-06-29 11:34:37.0, 7 <== Row: 12, \upload\item\shopImage\5\20180629113433541021.jpg, null, null, 2018-06-29 11:34:37.0, 7 <== Row: 13, \upload\item\shopImage\5\20180629113434424572.jpg, null, null, 2018-06-29 11:34:37.0, 7 <== Total: 3 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] from current transaction ==> Preparing: DELETE FROM tb_product_img WHERE product_id = ? ==> Parameters: 7(Long) <== Updates: 3 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] from current transaction ==> Preparing: INSERT INTO tb_product_img ( img_addr, img_desc, priority, create_time, product_id ) VALUES ( ?, ?, ?, ?, ? ) , ( ?, ?, ?, ?, ? ) ==> Parameters: \upload\item\shopImage\5\20180701003247930380.jpg(String), null, null, 2018-07-01 00:32:48.299(Timestamp), 7(Long), \upload\item\shopImage\5\20180701003247961681.jpg(String), null, null, 2018-07-01 00:32:48.299(Timestamp), 7(Long) <== Updates: 2 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280] from current transaction ==> Preparing: UPDATE tb_product SET product_name = ?, product_desc = ?, img_addr = ?, normal_price = ?, promotion_price = ?, priority = ?, last_edit_time = ?, product_category_id = ? WHERE product_id = ? AND shop_id=? ==> Parameters: offical_product(String), product offical desc(String), \upload\item\shopImage\5\2018070100324625530.jpg(String), 100(String), 80(String), 66(Integer), 2018-07-01 00:32:45.683(Timestamp), 36(Long), 7(Long), 5(Long) <== Updates: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64a40280]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

检查数据库记录和磁盘上的文件,正确,单元测试通过。

Github地址

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

单元测试

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:你一定要知道的规划工具—影响地图
下一篇:创龙OMAPL138 PRU开发例程
相关文章