Java + springboot + mybatis implements the comment and sub reply comment interface

Time:2021-9-1

1. Create table

biz_comment

Java + springboot + mybatis implements the comment and sub reply comment interface

biz_users

Java + springboot + mybatis implements the comment and sub reply comment interface

The fields, types and lengths of the table can be used according to your business needs. Only the required fields are provided here.

2. Generate biz by automatically generating code_ Comment table

domain :
explain:
@TableField(exist = false)

Indicates that this field is not in the table. It is a self-defined attribute and does not need mybatis to find this field.

private List<Comment> subComments;

Put the child comments into the list collection as an attribute of comment. Comment is all parent comments 
And subcomments are all child comments of this parent. You can write one alone if you like 
Comment VO to encapsulate all sub comments( For example: in an article, a replied, and B thought a was right 
A. C also thought a was right and replied to A. at this time, a is the parent. Let's put a into the comment and pass the cycle 
Ring and set methods put B and C into subcomments to become sub comments of A. this step is very important. I hope you can 
Understand this step before moving on.
package com.cema.manage.project.manage.comment.domain;

/**
 *Comment form biz_ comment
 *
 * @author reasahi
 * @date 2021-04-22
 */
@TableName(value = "biz_comment")
public class Comment extends Model<Comment>
{
private static final long serialVersionUID=1L;

/**Number*/
    @TableId(value = "id")
    private Integer id;
/**User ID
 */
    @TableField(value = "bu_id")
    private Integer buId;
/**Article ID*/
    @TableField(value = "bp_id")
    private Integer bpId;
/**Reply user ID*/
    @TableField(value = "wc_id")
    private Integer wcId;
/**Content*/
    @TableField(value = "wc_content")
    private String wcContent;
/**Comment time*/
    @TableField(value = "create_time")
    private String createTime;

    @TableField(exist = false)
    private List<Comment> subComments;

    public List<Comment> getSubComments() {
        return subComments;
    }

    public void setSubComments(List<Comment> subComments) {
        this.subComments = subComments;
    }

    /**
 *Settings: Numbering
 */
public void setId(Integer id)
        {
            this.id = id;
        }

/**
 *Get: number
 */
public Integer getId()
        {
        return id;
        }
/**
 *Setting: user ID

 */
public void setBuId(Integer buId)
        {
            this.buId = buId;
        }

/**
 *Get: user ID

 */
public Integer getBuId()
        {
        return buId;
        }
/**
 *Setting: Article ID
 */
public void setBpId(Integer bpId)
        {
            this.bpId = bpId;
        }

/**
 *Get: Article ID
 */
public Integer getBpId()
        {
        return bpId;
        }
/**
 *Setting: reply user ID
 */
public void setWcId(Integer wcId)
        {
            this.wcId = wcId;
        }

/**
 *Get: reply user ID
 */
public Integer getWcId()
        {
        return wcId;
        }
/**
 *Settings: content
 */
public void setWcContent(String wcContent)
        {
            if(wcContent!=null){
            if(wcContent.trim().isEmpty()){
            this.wcContent =null;
            }else{
            this.wcContent = wcContent;
            }
            }
        }

/**
 *Get: content
 */
public String getWcContent()
        {
        return wcContent;
        }
/**
 *Setting: comment time
 * @param createTime
 */
public void setCreateTime(String createTime)
        {
            this.createTime = createTime;
        }

/**
 *Get: comment time
 * @return
 */
public String getCreateTime()
        {
        return createTime;
        }

@Override
protected Serializable pkVal(){
        return this.id;
        }
        }

mapper :
explain:
The two interfaces correspond to two queries respectively. Level1query is the query parent and the other is the query child.

package com.cema.manage.project.manage.comment.mapper;

/**
 *Comment data layer
 * 
 * @author reasahi
 * @date 2021-04-22
 */
public interface CommentMapper  extends BaseMapper<Comment>
{

    List<Comment> level1Query(Map<String,Object> map);
    List<Comment> level2Query(Map<String,Object> map);

}

xml :
explain:

bu_ ID is the user ID, WC_ ID is the ID of the user who replied. Through WC_ If the ID is null, it can be judged that all the query results are null 
Parent ID( Imagine that if you comment on an article, you have an ID, or user name, and the ID / user name is 
It's bu_ ID, you commented on the article. The article has no WC_ ID, so the default value null is stored in the database. Of course, you can 
To save 0 or other, just feel free. This is the parent. Imagine that this time you don't comment with your ID / user name 
Zhang, you commented on many reviewers a under an article, and you replied to A. how is this stored in the database? isn't it? 
You need to take your bu_ Bu of ID and a_ IDs are stored in the database. In fact, a's Bu_ ID is WC_ ID, right? That's not true 
Yes, you can use WC_ ID judge all the people who replied to a in the same article. We just need to take out this key 
wc_id。)
<?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.cema.manage.project.manage.comment.mapper.CommentMapper">

    <!--  biz_comment  -->
    <resultMap id="resultMapComment" type="com.cema.manage.project.manage.comment.domain.Comment">
        <id column="id" property="id"></id>
        <result column="bu_id" property="buId"></result>
        <result column="bp_id" property="bpId"></result>
        <result column="wc_id" property="wcId"></result>
        <result column="wc_content" property="wcContent"></result>
        <result column="create_time" property="createTime"></result>
    </resultMap>

    <select id="level1Query" resultMap="resultMapComment" parameterType="map">
        SELECT u.bu_mail,
               c.bp_id,
               c.wc_id,
               c.bu_id,
               c.create_time,
               c.wc_content
        FROM biz_comment c
                 LEFT JOIN biz_users_1 u ON c.bu_id = u.id
        WHERE c.wc_id IS NULL
          AND c.bp_id = #{bp_id}
        ORDER BY c.create_time DESC LIMIT #{current}, #{size};
    </select>

    <select id="level2Query" resultMap="resultMapComment" parameterType="map">
        SELECT u.bu_mail,
               c.bp_id,
               c.wc_id,
               c.bu_id,
               c.create_time,
               c.wc_content
        FROM biz_comment c
                 LEFT JOIN biz_users_1 u ON c.wc_id = u.id
        WHERE c.wc_id IS NOT NULL
          AND bu_id = #{bu_id}
          AND bp_id = #{bp_id}
        ORDER BY c.create_time DESC
    </select>

</mapper>

service :

package com.cema.manage.project.manage.comment.service;

/**
 *Comment service layer
 *
 * @author reasahi
 * @date 2021-04-22
 */
public interface ICommentService extends IService<Comment> {

    List<Comment> getCommentList(Map<String,Object> map);

}

serviceImpl :
explain:

If you can understand the above, it is also well understood here. First, call level1query to get the parent, and loop through the parent to get the parent 
bu_ id,bp_ The ID is stored in the map to level2query, then the child comments are traversed in turn, and finally all parents / children are stored in the collection, 
Return to the front end.
package com.cema.manage.project.manage.comment.service;

/**
 *Comment service layer implementation
 *
 * @author reasahi
 * @date 2021-04-22
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public List<Comment> getCommentList(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Comment> resultList = new ArrayList<>();
        List<Comment> parentComments = commentMapper.level1Query(map);
        for (Comment c : parentComments) {
            resultMap.put("bu_id",c.getBuId());
            resultMap.put("bp_id",c.getBpId());
            List<Comment> subComments = commentMapper.level2Query(resultMap);
            List<Comment> reply = new ArrayList<>();
            for (Comment c1 : subComments) {
                reply.add(c1);
            }
            c.setSubComments(reply);
            resultList.add(c);
        }
        return resultList;
    }

}

controller :
explain:
All user IDs need to be obtained through a token. If not, they can be written to death for testing. It doesn’t matter. Current is the page number, and the front end will not return 0, because no one will see page 0, right? They all start from the first page, so do a simple processing. Let’s return to JSON for development.

    @PostMapping("getCommentList")
    @ResponseBody
    public String getCommentList(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = (String) request.getAttribute("token");
        Users users = (Users) redisService.get(token);
        Integer bu_id = users.getId();
        String current = request.getParameter("current");
        String size = request.getParameter("size");
        String bp_id = request.getParameter("bp_id");
        resultMap.put("bu_id", bu_id);
        resultMap.put("current", (Integer.parseInt(current) - 1) * Integer.parseInt(size));
        resultMap.put("size", Integer.parseInt(size));
        resultMap.put("bp_id", Integer.parseInt(bp_id));
        List<Comment> comments = commentService.getCommentList(resultMap);
        JSONObject jsonStr = jsonDataUtilsInterFase.getJsonObject(comments,
                attributeUtilsInterFase.getAttributeName(String.class,
                        TestAttributeUtils.FILTER_PROTECTED), TestJSONDataUtils.SUCCESS_CODE1);
        return jsonStr.toString();
    }

5. Summary

Now that the code is complete, you can start the test. The returned JSON is as follows:

{
    "code": 1, 
    "Datalist": [father's comment list "
        {
            "Buid": user ID, 
            "WCID": reply to user ID, 
            "Createtime": creation time, 
            "Subcomments": [// son comments list
                {
                    "Buid": user ID, 
                    "WCID": reply to user ID, 
                    "Createtime": creation time, 
                    "Subcomments": [Level 3 replies regardless of data], 
                    "Bpid": Article ID, 
                    "Id": primary key, 
                    "Wccontent": "replying to user comments"
                }, 
                {
                    "Buid": user ID, 
                    "WCID": reply to user ID, 
                    "Createtime": creation time, 
                    "Subcomments": [Level 3 replies regardless of data], 
                    "Bpid": Article ID, 
                    "Id": primary key, 
                    "Wccontent": "replying to user comments"
                }
            ], 
            "Bpid": Article ID, 
            "Id": primary key, 
            "Wccontent": "replying to comments on articles"
        }
    ], 
    "timetamp": 1619322835508
}

This interface focuses on SQL writing and data structure. You need to filter out all parent and child comments through SQL. The data structure is a parent-child package. You can use the list set or other packages to package a VO. This article only provides an idea, and you can do better.

byeBye ^ ^~

Recommended Today

“Self test” stay up late to summarize 50 Vue knowledge points, all of which will make you God!!!

preface Hello everyone, I’m Lin Sanxin. A lot of things have happened these days (I won’t say what’s specific). These things have scared me to treasure my collection these yearsVue knowledge pointsI took out my notes and tried my best to recall them. Finally, I realized these 50Knowledge points(let’s not be too vulgar. It’s not […]