在 foodie-dev-pojo 模块中的 com.imooc.pojo.vo 包内新建 ItemCommentVO 类

    1. package com.imooc.pojo.vo;
    2. import java.util.Date;
    3. /**
    4. * 用于展示商品评价的 VO
    5. * Created by 92578 on 2020/8/22 21:28
    6. **/
    7. public class ItemCommentVO {
    8. private Integer commentLevel;
    9. private String content;
    10. private String sepcName;
    11. private Date createdTime;
    12. private String userFace;
    13. private String nickname;
    14. public Integer getCommentLevel() {
    15. return commentLevel;
    16. }
    17. public void setCommentLevel(Integer commentLevel) {
    18. this.commentLevel = commentLevel;
    19. }
    20. public String getContent() {
    21. return content;
    22. }
    23. public void setContent(String content) {
    24. this.content = content;
    25. }
    26. public String getSepcName() {
    27. return sepcName;
    28. }
    29. public void setSepcName(String sepcName) {
    30. this.sepcName = sepcName;
    31. }
    32. public Date getCreatedTime() {
    33. return createdTime;
    34. }
    35. public void setCreatedTime(Date createdTime) {
    36. this.createdTime = createdTime;
    37. }
    38. public String getUserFace() {
    39. return userFace;
    40. }
    41. public void setUserFace(String userFace) {
    42. this.userFace = userFace;
    43. }
    44. public String getNickname() {
    45. return nickname;
    46. }
    47. public void setNickname(String nickname) {
    48. this.nickname = nickname;
    49. }
    50. }

    在 foodie-dev-mapper 模块中的 com.imooc.mapper 包下面创建 ItemsMapperCustom 接口

    1. package com.imooc.mapper;
    2. import com.imooc.pojo.vo.ItemCommentVO;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. import java.util.Map;
    6. public interface ItemsMapperCustom {
    7. public List<ItemCommentVO> queryItemComments(@Param("paramsMap") Map<String, Object> map);
    8. }

    在 foodie-dev-service 模块中的 ItemService 接口内添加 queryPagedComments 方法

    1. package com.imooc.service;
    2. import com.imooc.pojo.Items;
    3. import com.imooc.pojo.ItemsImg;
    4. import com.imooc.pojo.ItemsParam;
    5. import com.imooc.pojo.ItemsSpec;
    6. import com.imooc.pojo.vo.CommentLevelCountsVO;
    7. import com.imooc.pojo.vo.ItemCommentVO;
    8. import java.util.List;
    9. /**
    10. * Created by 92578 on 2020/8/22 16:17
    11. **/
    12. public interface ItemService {
    13. /**
    14. * 根据商品 id 查询详情
    15. *
    16. * @param itemId
    17. * @return
    18. */
    19. public Items queryItemById(String itemId);
    20. /**
    21. * 根据商品 id 查询商品图片列表
    22. *
    23. * @param itemId
    24. * @return
    25. */
    26. public List<ItemsImg> queryItemImgList(String itemId);
    27. /**
    28. * 根据商品 id 查询商品规格
    29. *
    30. * @param itemId
    31. * @return
    32. */
    33. public List<ItemsSpec> queryItemSpecList(String itemId);
    34. /**
    35. * 根据商品 id 查询商品参数
    36. *
    37. * @param itemId
    38. * @return
    39. */
    40. public ItemsParam queryItemsParam(String itemId);
    41. /**
    42. * 根据商品 id 查询商品的评价等级数量
    43. *
    44. * @param itemId
    45. */
    46. public CommentLevelCountsVO queryCommentCounts(String itemId);
    47. /**
    48. * 根据商品 id 查询商品的评价(分页)
    49. *
    50. * @param itemId
    51. * @param level
    52. * @return
    53. */
    54. public List<ItemCommentVO> queryPagedComments(String itemId, Integer level);
    55. }

    在 foodie-dev-service 模块中完善 ItemServiceImpl 类,实现 queryPagedComments 接口

    1. package com.imooc.service.impl;
    2. import com.imooc.enums.CommentLevel;
    3. import com.imooc.mapper.*;
    4. import com.imooc.pojo.*;
    5. import com.imooc.pojo.vo.CommentLevelCountsVO;
    6. import com.imooc.pojo.vo.ItemCommentVO;
    7. import com.imooc.service.ItemService;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.stereotype.Service;
    10. import org.springframework.transaction.annotation.Propagation;
    11. import org.springframework.transaction.annotation.Transactional;
    12. import tk.mybatis.mapper.entity.Example;
    13. import java.util.HashMap;
    14. import java.util.List;
    15. import java.util.Map;
    16. /**
    17. * Created by 92578 on 2020/8/22 16:18
    18. **/
    19. @Service
    20. public class ItemServiceImpl implements ItemService {
    21. @Autowired
    22. private ItemsMapper itemsMapper;
    23. @Autowired
    24. private ItemsImgMapper itemsImgMapper;
    25. @Autowired
    26. private ItemsSpecMapper itemsSpecMapper;
    27. @Autowired
    28. private ItemsParamMapper itemsParamMapper;
    29. @Autowired
    30. private ItemsCommentsMapper itemsCommentsMapper;
    31. @Autowired
    32. private ItemsMapperCustom itemsMapperCustom;
    33. /**
    34. * 根据商品 id 查询详情
    35. *
    36. * @param itemId
    37. * @return
    38. */
    39. @Transactional(propagation = Propagation.SUPPORTS)
    40. @Override
    41. public Items queryItemById(String itemId) {
    42. return itemsMapper.selectByPrimaryKey(itemId);
    43. }
    44. /**
    45. * 根据商品 id 查询商品图片列表
    46. *
    47. * @param itemId
    48. * @return
    49. */
    50. @Transactional(propagation = Propagation.SUPPORTS)
    51. @Override
    52. public List<ItemsImg> queryItemImgList(String itemId) {
    53. Example itemsImgExp = new Example(ItemsImg.class);
    54. Example.Criteria criteria = itemsImgExp.createCriteria();
    55. criteria.andEqualTo("itemId", itemId);
    56. return itemsImgMapper.selectByExample(itemsImgExp);
    57. }
    58. /**
    59. * 根据商品 id 查询商品规格
    60. *
    61. * @param itemId
    62. * @return
    63. */
    64. @Transactional(propagation = Propagation.SUPPORTS)
    65. @Override
    66. public List<ItemsSpec> queryItemSpecList(String itemId) {
    67. Example itemsSpecExp = new Example(ItemsSpec.class);
    68. Example.Criteria criteria = itemsSpecExp.createCriteria();
    69. criteria.andEqualTo("itemId", itemId);
    70. return itemsSpecMapper.selectByExample(itemsSpecExp);
    71. }
    72. /**
    73. * 根据商品 id 查询商品参数
    74. *
    75. * @param itemId
    76. * @return
    77. */
    78. @Transactional(propagation = Propagation.SUPPORTS)
    79. @Override
    80. public ItemsParam queryItemsParam(String itemId) {
    81. Example itemsParamExp = new Example(ItemsParam.class);
    82. Example.Criteria criteria = itemsParamExp.createCriteria();
    83. criteria.andEqualTo("itemId", itemId);
    84. return itemsParamMapper.selectOneByExample(itemsParamExp);
    85. }
    86. /**
    87. * 根据商品 id 查询商品的评价等级数量
    88. *
    89. * @param itemId
    90. */
    91. @Transactional(propagation = Propagation.SUPPORTS)
    92. @Override
    93. public CommentLevelCountsVO queryCommentCounts(String itemId) {
    94. Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
    95. Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
    96. Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
    97. Integer totalCounts = goodCounts + normalCounts + badCounts;
    98. CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
    99. countsVO.setTotalCounts(totalCounts);
    100. countsVO.setGoodCounts(goodCounts);
    101. countsVO.setNormalCounts(normalCounts);
    102. countsVO.setBadCounts(badCounts);
    103. return countsVO;
    104. }
    105. /**
    106. * 根据商品 id 查询商品的评价(分页)
    107. *
    108. * @param itemId
    109. * @param level
    110. * @return
    111. */
    112. @Transactional(propagation = Propagation.SUPPORTS)
    113. @Override
    114. public List<ItemCommentVO> queryPagedComments(String itemId, Integer level) {
    115. Map<String, Object> map = new HashMap<>();
    116. map.put("itemId", itemId);
    117. map.put("level", level);
    118. List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);
    119. return list;
    120. }
    121. @Transactional(propagation = Propagation.SUPPORTS)
    122. Integer getCommentCounts(String itemId, Integer level) {
    123. ItemsComments condition = new ItemsComments();
    124. condition.setItemId(itemId);
    125. if (level != null) {
    126. condition.setCommentLevel(level);
    127. }
    128. return itemsCommentsMapper.selectCount(condition);
    129. }
    130. }

    在 foodie-dev-mapper 模块中的 resource\mapper 文件夹里创建 ItemsMapperCustom.xml

    <?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.imooc.mapper.ItemsMapperCustom">
        <select id="queryItemComments" parameterType="Map" resultType="com.imooc.pojo.vo.ItemCommentVO">
            select ic.comment_level as commentLevel,
                   ic.content       as content,
                   ic.sepc_name     as sepcName,
                   ic.created_time  as createdTime,
                   u.face           as userFace,
                   u.nickname       as nickname
            from items_comments as ic
                     left join users as u on ic.user_id = u.id
            where ic.item_id = #{paramsMap.itemId}
                <if test=" paramsMap.level != null and paramsMap.level != '' ">
                    and ic.comment_level = #{paramsMap.level}
                </if>
        </select>
    </mapper>