当前位置:网站首页>商品评论信息与评论信息分类
商品评论信息与评论信息分类
2022-07-27 16:18:00 【猫的幻想曲】
商品详情分析-显示商品评论信息:
数据库实现:

实体类:
package com.qfedu.fmmall.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductCommentsVO {
/**
* ID
*/
private String commId;
/**
* 商品id
*/
private String productId;
/**
* 商品名称
*/
private String productName;
/**
* 订单项(商品快照)ID 可为空
*/
private String orderItemId;
/**
* 是否匿名(1:是,0:否)
*/
private Integer isAnonymous;
/**
* 评价类型(1好评,0中评,-1差评)
*/
private Integer commType;
/**
* 评价等级 1:好评 2:中评 3:差评
*/
private Integer commLevel;
/**
* 评价内容
*/
private String commContent;
/**
* 评价晒图(JSON {img1:url1,img2:url2} )
*/
private String commImgs;
/**
* 评价时间 可为空
*/
private Date sepcName;
/**
* 是否回复(0:未回复,1:已回复)
*/
private Integer replyStatus;
/**
* 回复内容
*/
private String replyContent;
/**
* 回复时间
*/
private Date replyTime;
/**
* 是否显示(1:是,0:否)
*/
private Integer isShow;
/**
* users属性 :封装评论对应的数据
*/
/**
* 评论用户id 用户名须脱敏
*/
private String userId;
private String username;
private String nickname;
private String userImg;
}在文件mapper接口定义查询方法:
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.ProductComments;
import com.qfedu.fmmall.entity.ProductCommentsVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductCommentsMapper extends GeneralDAO<ProductComments> {
public List<ProductCommentsVO> selectCommentsByProductId(String productId);
}映射文件:
<?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.qfedu.fmmall.dao.ProductCommentsMapper" >
<resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.ProductComments" >
<id column="comm_id" property="commId" jdbcType="VARCHAR" />
<result column="product_id" property="productId" jdbcType="VARCHAR" />
<result column="product_name" property="productName" jdbcType="VARCHAR" />
<result column="order_item_id" property="orderItemId" jdbcType="VARCHAR" />
<result column="user_id" property="userId" jdbcType="VARCHAR" />
<result column="is_anonymous" property="isAnonymous" jdbcType="INTEGER" />
<result column="comm_type" property="commType" jdbcType="INTEGER" />
<result column="comm_level" property="commLevel" jdbcType="INTEGER" />
<result column="comm_content" property="commContent" jdbcType="VARCHAR" />
<result column="comm_imgs" property="commImgs" jdbcType="VARCHAR" />
<result column="sepc_name" property="sepcName" jdbcType="TIMESTAMP" />
<result column="reply_status" property="replyStatus" jdbcType="INTEGER" />
<result column="reply_content" property="replyContent" jdbcType="VARCHAR" />
<result column="reply_time" property="replyTime" jdbcType="TIMESTAMP" />
<result column="is_show" property="isShow" jdbcType="INTEGER" />
</resultMap>
<resultMap id="ProductCommentsVOMap" type="com.qfedu.fmmall.entity.ProductCommentsVO" >
<id column="comm_id" property="commId" jdbcType="VARCHAR" />
<result column="product_id" property="productId" jdbcType="VARCHAR" />
<result column="product_name" property="productName" jdbcType="VARCHAR" />
<result column="order_item_id" property="orderItemId" jdbcType="VARCHAR" />
<result column="is_anonymous" property="isAnonymous" jdbcType="INTEGER" />
<result column="comm_type" property="commType" jdbcType="INTEGER" />
<result column="comm_level" property="commLevel" jdbcType="INTEGER" />
<result column="comm_content" property="commContent" jdbcType="VARCHAR" />
<result column="comm_imgs" property="commImgs" jdbcType="VARCHAR" />
<result column="sepc_name" property="sepcName" jdbcType="TIMESTAMP" />
<result column="reply_status" property="replyStatus" jdbcType="INTEGER" />
<result column="reply_content" property="replyContent" jdbcType="VARCHAR" />
<result column="reply_time" property="replyTime" jdbcType="TIMESTAMP" />
<result column="is_show" property="isShow" jdbcType="INTEGER" />
<result column="user_id" property="userId" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="nickname" property="nickname" jdbcType="VARCHAR" />
<result column="user_img" property="userImg" jdbcType="VARCHAR" />
</resultMap>
<select id="selectCommentsByProductId" resultMap="ProductCommentsVOMap">
select
u.username,
u.nickname,
u.user_img,
c.comm_id,
c.product_id,
c.product_name,
c.order_item_id,
c.user_id,
c.is_anonymous,
c.comm_type,
c.comm_level,
c.comm_content,
c.comm_imgs,
c.sepc_name,
c.reply_status,
c.reply_content,
c.reply_time,
c.is_show
from product_comments c
inner join users u
on u.user_id = c.user_id
where c.product_id = #{productId}
</select>
</mapper>productCommentsServiceImpl类:
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ProductCommentsMapper;
import com.qfedu.fmmall.entity.ProductCommentsVO;
import com.qfedu.fmmall.service.ProductCommentsService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-27 16:44
*/
@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {
@Autowired
private ProductCommentsMapper productCommentsMapper;
@Override
public R listCommentsByProductId(String productId) {
List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommentsByProductId(productId);
R r = new R(ResStatus.OK, "success", productCommentsVOS);
return r;
}
}
测试:
@Test
public void testSelectComments(){
List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommentsByProductId("3");
for(ProductCommentsVO p:productCommentsVOS){
System.out.println(p);
}
}测试结果:
ProductCommentsVO(commId=1, productId=3, productName=咪咪虾条, orderItemId=10000001, isAnonymous=1, commType=1, commLevel=1, commContent=我买这不是来吃的,就是玩儿, commImgs={}, sepcName=Tue Apr 27 14:51:10 CST 2021, replyStatus=0, replyContent=null, replyTime=Tue Apr 27 14:51:32 CST 2021, isShow=1, userId=4, username=heihei, nickname=二狗, userImg=img/default.png)
ProductCommentsVO(commId=2, productId=3, productName=咪咪虾条, orderItemId=10000002, isAnonymous=0, commType=1, commLevel=1, commContent=nice, commImgs={img1:“mmxt2.png”}, sepcName=Tue Apr 27 14:53:20 CST 2021, replyStatus=1, replyContent=你好我也好, replyTime=Tue Apr 27 14:53:37 CST 2021, isShow=1, userId=5, username=Tony, nickname=托尼, userImg=img/default.png)根据用户id查询商品信息评论,并显示出用户信息和评论信息;
评论信息分页:

定义pageHelper:
package com.qfedu.fmmall.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageHelper<T> {
//总记录数
private int count;
//总页数
private int pageCount;
//分页数据
private List<T> list;
}
改造商品评论接口:
分页查询:
修改mapper接口:
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.ProductComments;
import com.qfedu.fmmall.entity.ProductCommentsVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductCommentsMapper extends GeneralDAO<ProductComments> {
/**
* 根据商品id分页查询评论信息
* @param productId 商品id
* @param start 起始索引
* @param pageSize 查询条数
* @return
*/
public List<ProductCommentsVO> selectCommentsByProductId(@Param("productId") String productId,
@Param("start") int start,
@Param("pageSize") int pageSize);
}在商品评论的映射文件mapper上加入:

service接口:
package com.qfedu.fmmall.service;
import com.qfedu.fmmall.vo.R;
import org.springframework.stereotype.Service;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-27 16:42
*/
public interface ProductCommentsService {
/**
* 根据商品id实现评论的分页查询
* @param productId 商品id
* @param pageNum 查询页码
* @param pageSize 每页显示条数
* @return
*/
public R listCommentsByProductId(String productId,int pageNum,int pageSize);
}
productCommentsService的实现类;
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ProductCommentsMapper;
import com.qfedu.fmmall.entity.ProductComments;
import com.qfedu.fmmall.entity.ProductCommentsVO;
import com.qfedu.fmmall.service.ProductCommentsService;
import com.qfedu.fmmall.utils.PageHelper;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-27 16:44
*/
@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {
@Autowired
private ProductCommentsMapper productCommentsMapper;
@Override
public R listCommentsByProductId(String productId,int pageNum,int pageSize ) {
// List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommentsByProductId(productId);
//1.根据商品id查询总记录数
Example example = new Example(ProductComments.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("productId",productId);
int count = productCommentsMapper.selectCountByExample(example);
//2.计算总页数(必须确定每页显示多少条 pageSize)
int pageCount = count%pageSize==0?count/pageSize : count/pageSize+1;
//3.查询当前数据(因为评论中需要用户信息,因此需要链表查询--自定义)
int start = (pageNum - 1)* pageSize;
List<ProductCommentsVO> list = productCommentsMapper.selectCommentsByProductId(productId, start, pageSize);
R r = new R(ResStatus.OK, "success", new PageHelper<ProductCommentsVO>(count,pageCount,list));
return r;
}
}
评价统计接口实现:
productCommentsService接口:
/**
* 根据商品id统计当前商品的评价信息
* @param productId
* @return
*/
public R getCommentsCountByProductId(String productId);productCommentsServiceImpl实现类:
@Override
public R getCommentsCountByProductId(String productId) {
//1.查询当前商品评价的总数
Example example = new Example(ProductComments.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("productId",productId);
int total = productCommentsMapper.selectCountByExample(example);
//2.查询好评评价数
criteria.andEqualTo("commType",1);
int goodTotal = productCommentsMapper.selectCountByExample(example);
//3.查询中评评价数
Example example1 = new Example(ProductComments.class);
Example.Criteria criteria1 = example1.createCriteria();
criteria1.andEqualTo("productId",productId);
criteria1.andEqualTo("commType",0);
int midTotal = productCommentsMapper.selectCountByExample(example1);
//4.查询坏评评价数
Example example2 = new Example(ProductComments.class);
Example.Criteria criteria2 = example2.createCriteria();
criteria2.andEqualTo("productId",productId);
criteria2.andEqualTo("commType",-1);
int badTotal = productCommentsMapper.selectCountByExample(example2);
//5.计算好评率
double percent = (Double.parseDouble(goodTotal+"") / Double.parseDouble(total+""))*100;
String percentValue = (percent+"").substring(0,(percent+"").lastIndexOf(".")+3);
HashMap<String,Object> map = new HashMap<>();
map.put("total",total);
map.put("goodTotal",goodTotal);
map.put("midTotal",midTotal);
map.put("badTotal",badTotal);
map.put("percent",percentValue);
R r = new R(ResStatus.OK, "success", map);
return r;
}边栏推荐
猜你喜欢

MySQL basic statement

Preliminary introduction to C miscellaneous lecture linked list

2021.8.9 note request

Hbuilder submission code

2021.8.6 notes jsoup

图文结合,完美解释MySQL逻辑备份的实现流程

Visual studio code installation tutorial (super detailed)

2021.8.7 note Servlet

How to realize the full-text content retrieval of word, PDF and txt files?

全身多功能按摩仪芯片-DLTAP602SD
随机推荐
org.apache.catalina.core.StandardContext. startInternal Context [] startup failed due to previous err
How to realize the full-text content retrieval of word, PDF and txt files?
Graphical interface programming
修改input中placeholder样式
知识图谱 — jieba、pyhanlp、smoothnlp工具实现中文分词(词性表)
2021.8.1 Notes database design
V-bind and V-for
idea 2020.1社区版下载体验
Wechat applet multi file upload
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
Mybtis-Plus常用的内置方法
Set the arc button to be displayed in the middle
2021.7.31 note view
我人都傻了,CompletableFuture和OpenFegin一起使用竟然报错
Modify placeholder style in input
Uni app for wechat login (to be finished)
Canvas draws graphics according to coordinate points
2021.7.30 note index
2021.7.13笔记 子查询
Vue uses keep alive to realize page caching