在 foodie-dev-pojo 模块中的 com.imooc.pojo.vo 包内新建 ItemCommentVO 类
package com.imooc.pojo.vo;import java.util.Date;/*** 用于展示商品评价的 VO* Created by 92578 on 2020/8/22 21:28**/public class ItemCommentVO {private Integer commentLevel;private String content;private String sepcName;private Date createdTime;private String userFace;private String nickname;public Integer getCommentLevel() {return commentLevel;}public void setCommentLevel(Integer commentLevel) {this.commentLevel = commentLevel;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getSepcName() {return sepcName;}public void setSepcName(String sepcName) {this.sepcName = sepcName;}public Date getCreatedTime() {return createdTime;}public void setCreatedTime(Date createdTime) {this.createdTime = createdTime;}public String getUserFace() {return userFace;}public void setUserFace(String userFace) {this.userFace = userFace;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}}
在 foodie-dev-mapper 模块中的 com.imooc.mapper 包下面创建 ItemsMapperCustom 接口
package com.imooc.mapper;import com.imooc.pojo.vo.ItemCommentVO;import org.apache.ibatis.annotations.Param;import java.util.List;import java.util.Map;public interface ItemsMapperCustom {public List<ItemCommentVO> queryItemComments(@Param("paramsMap") Map<String, Object> map);}
在 foodie-dev-service 模块中的 ItemService 接口内添加 queryPagedComments 方法
package com.imooc.service;import com.imooc.pojo.Items;import com.imooc.pojo.ItemsImg;import com.imooc.pojo.ItemsParam;import com.imooc.pojo.ItemsSpec;import com.imooc.pojo.vo.CommentLevelCountsVO;import com.imooc.pojo.vo.ItemCommentVO;import java.util.List;/*** Created by 92578 on 2020/8/22 16:17**/public interface ItemService {/*** 根据商品 id 查询详情** @param itemId* @return*/public Items queryItemById(String itemId);/*** 根据商品 id 查询商品图片列表** @param itemId* @return*/public List<ItemsImg> queryItemImgList(String itemId);/*** 根据商品 id 查询商品规格** @param itemId* @return*/public List<ItemsSpec> queryItemSpecList(String itemId);/*** 根据商品 id 查询商品参数** @param itemId* @return*/public ItemsParam queryItemsParam(String itemId);/*** 根据商品 id 查询商品的评价等级数量** @param itemId*/public CommentLevelCountsVO queryCommentCounts(String itemId);/*** 根据商品 id 查询商品的评价(分页)** @param itemId* @param level* @return*/public List<ItemCommentVO> queryPagedComments(String itemId, Integer level);}
在 foodie-dev-service 模块中完善 ItemServiceImpl 类,实现 queryPagedComments 接口
package com.imooc.service.impl;import com.imooc.enums.CommentLevel;import com.imooc.mapper.*;import com.imooc.pojo.*;import com.imooc.pojo.vo.CommentLevelCountsVO;import com.imooc.pojo.vo.ItemCommentVO;import com.imooc.service.ItemService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import tk.mybatis.mapper.entity.Example;import java.util.HashMap;import java.util.List;import java.util.Map;/*** Created by 92578 on 2020/8/22 16:18**/@Servicepublic class ItemServiceImpl implements ItemService {@Autowiredprivate ItemsMapper itemsMapper;@Autowiredprivate ItemsImgMapper itemsImgMapper;@Autowiredprivate ItemsSpecMapper itemsSpecMapper;@Autowiredprivate ItemsParamMapper itemsParamMapper;@Autowiredprivate ItemsCommentsMapper itemsCommentsMapper;@Autowiredprivate ItemsMapperCustom itemsMapperCustom;/*** 根据商品 id 查询详情** @param itemId* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic Items queryItemById(String itemId) {return itemsMapper.selectByPrimaryKey(itemId);}/*** 根据商品 id 查询商品图片列表** @param itemId* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<ItemsImg> queryItemImgList(String itemId) {Example itemsImgExp = new Example(ItemsImg.class);Example.Criteria criteria = itemsImgExp.createCriteria();criteria.andEqualTo("itemId", itemId);return itemsImgMapper.selectByExample(itemsImgExp);}/*** 根据商品 id 查询商品规格** @param itemId* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<ItemsSpec> queryItemSpecList(String itemId) {Example itemsSpecExp = new Example(ItemsSpec.class);Example.Criteria criteria = itemsSpecExp.createCriteria();criteria.andEqualTo("itemId", itemId);return itemsSpecMapper.selectByExample(itemsSpecExp);}/*** 根据商品 id 查询商品参数** @param itemId* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic ItemsParam queryItemsParam(String itemId) {Example itemsParamExp = new Example(ItemsParam.class);Example.Criteria criteria = itemsParamExp.createCriteria();criteria.andEqualTo("itemId", itemId);return itemsParamMapper.selectOneByExample(itemsParamExp);}/*** 根据商品 id 查询商品的评价等级数量** @param itemId*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic CommentLevelCountsVO queryCommentCounts(String itemId) {Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);Integer totalCounts = goodCounts + normalCounts + badCounts;CommentLevelCountsVO countsVO = new CommentLevelCountsVO();countsVO.setTotalCounts(totalCounts);countsVO.setGoodCounts(goodCounts);countsVO.setNormalCounts(normalCounts);countsVO.setBadCounts(badCounts);return countsVO;}/*** 根据商品 id 查询商品的评价(分页)** @param itemId* @param level* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<ItemCommentVO> queryPagedComments(String itemId, Integer level) {Map<String, Object> map = new HashMap<>();map.put("itemId", itemId);map.put("level", level);List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);return list;}@Transactional(propagation = Propagation.SUPPORTS)Integer getCommentCounts(String itemId, Integer level) {ItemsComments condition = new ItemsComments();condition.setItemId(itemId);if (level != null) {condition.setCommentLevel(level);}return itemsCommentsMapper.selectCount(condition);}}
在 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>
