基于Spring+SpringMVC+MyBatis开发书评网图书详情页-显示评论列表开发

Posted 被雨遗忘的夏天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Spring+SpringMVC+MyBatis开发书评网图书详情页-显示评论列表开发相关的知识,希望对你有一定的参考价值。

一、 概要

定位

评论列表的开发,需要获取图书信息、会员信息以及评论信息,而Book已经封装好,所以本次实体类和mybatis底层配置只需要创建会员(Member)评论(Evaluation)。

 

效果图

 

项目结构图

PS: 绿色为本次提交代码

Java工程

resources

webapp

 

二、 开发流程

1、 创建实体类

1.1 Member

package com.imooc.reader.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.Date;

/**
 * @ClassName Member
 * @Description 会员实体类
 * @date 2021/5/10 20:07
 * @Param
 * @return
 */
// 映射对应数据表
// mysql8的小伙伴注意一下,member被选为保留字段,避免冲突,可以加上反引号
@TableName("member")
public class Member {
    // 会员编号
    @TableId(type = IdType.AUTO)
    private Long memberId;
    // 用户名
    private String username;
    // 密码
    private String password;
    // 盐值
    private Integer salt;
    // 昵称
    private String nickname;
    // 创建时间
    private Date createTime;

    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getSalt() {
        return salt;
    }

    public void setSalt(Integer salt) {
        this.salt = salt;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

 

1.2 Evaluation

package com.imooc.reader.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.Date;

/**
 * @ClassName Evaluation
 * @Description 评论信息实体类
 * @date 2021/5/10 11:45
 * @Param
 * @return
 */
// 映射数据库表格evaluation
@TableName("evaluation")
public class Evaluation {
    // 映射主键id,自增
    @TableId(type= IdType.AUTO)
    // 评论编号
    private Long evaluationId;
    // 图书编号
    private Long bookId;
    // 短评内容
    private String content;
    // 评分(5分制)
    private Integer score;
    // 会员编号
    private Long memberId;
    // 创建时间
    private Date createTime;
    // 点赞数量
    private Integer enjoy;
    // 审核状态 enable-有效 disable-禁用
    private String state;
    // 禁用理由
    private String disableReason;
    // 禁用时间
    private Date disableTime;

    // book属性具有Book的关联对象
    // exist说明book属性没有对应字段,不会参与到sql语句自动生成,需要手动查询
    @TableField(exist = false)
    private Book book;

    // 和book对象一样
    @TableField(exist = false)
    private Member member;

    public Long getEvaluationId() {
        return evaluationId;
    }

    public void setEvaluationId(Long evaluationId) {
        this.evaluationId = evaluationId;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getEnjoy() {
        return enjoy;
    }

    public void setEnjoy(Integer enjoy) {
        this.enjoy = enjoy;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getDisableReason() {
        return disableReason;
    }

    public void setDisableReason(String disableReason) {
        this.disableReason = disableReason;
    }

    public Date getDisableTime() {
        return disableTime;
    }

    public void setDisableTime(Date disableTime) {
        this.disableTime = disableTime;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }
}

 

2、 创建Mapper接口

2.1 MemberMapper

package com.imooc.reader.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.Member;

/**
 * @ClassName MemberMapper
 * @Description 会员mapper接口
 * @date 2021/5/10 20:16
 * @Param
 * @return
 */
public interface MemberMapper extends BaseMapper<Member> {
}

 

 

2.2 EvaluationMapper

package com.imooc.reader.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.Evaluation;

/**
 * @ClassName EvaluationMapper
 * @Description 评论mapper接口
 * @date 2021/5/10 11:54
 * @Param
 * @return
 */
public interface EvaluationMapper extends BaseMapper<Evaluation> {
}

 

 

3、 创建相对应的mapper.xml

3.1 member.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.reader.mapper.MemberMapper">

</mapper>

 

 

3.2 evaluation.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.reader.mapper.EvaluationMapper">

</mapper>

 

 

4、 Service层创建服务接口

package com.imooc.reader.service;

import com.imooc.reader.entity.Evaluation;

import java.util.List;

/**
 * @ClassName EvaluationService
 * @Description 短评服务接口
 * @date 2021/5/10 11:55
 * @Param
 * @return
 */
public interface EvaluationService {
    /**
     * 按图书编号查询有效短评
     * @param bookId 图书编号
     * @return 评论列表
     */
    public List<Evaluation> selectByBookId(Long bookId);
}

 

 

5、 实现Service层的服务接口

package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Book;
import com.imooc.reader.entity.Evaluation;
import com.imooc.reader.entity.Member;
import com.imooc.reader.mapper.BookMapper;
import com.imooc.reader.mapper.EvaluationMapper;
import com.imooc.reader.mapper.MemberMapper;
import com.imooc.reader.service.EvaluationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName EvaluationServiceImpl
 * @Description 实现短评服务接口
 * @date 2021/5/10 11:56
 * @Param
 * @return
 */

@Service("evaluationService")
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class EvaluationServiceImpl implements EvaluationService {
    @Resource
    private EvaluationMapper evaluationMapper;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private MemberMapper memberMapper;

    /**
     * 按图书编号查询有效短评
     *
     * @param bookId 图书编号
     * @return 评论列表
     */
    public List<Evaluation> selectByBookId(Long bookId) {

        Book book = bookMapper.selectById(bookId);

        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<Evaluation>();
        // 首先对BookId进行筛选
        queryWrapper.eq("book_id", bookId);
        // 筛选有效短评
        queryWrapper.eq("state", "enable");
        // 按创建时间降序排序
        queryWrapper.orderByDesc("create_time");

        List<Evaluation> evaluationList = evaluationMapper.selectList(queryWrapper);

        // 在获取到对应的 evaluationList 集合后,提取每一个评论对象,查询该评论所对应的会员、图书信息
        for(Evaluation eva:evaluationList){
            Member member = memberMapper.selectById(eva.getMemberId());
            eva.setMember(member);
            eva.setBook(book);
        }

        return evaluationList;
    }
}

 

 

6、 前端FreeMarker实现

<div class="reply pl-2 pr-2">
        <#-- 利用freemake的list标签,进行遍历 -->
        <#list evaluationList as evaluation>
            <div>
                <div>
                    <span class="pt-1 small text-black-50 mr-2">${evaluation.createTime?string('MM-dd')}</span>
                    <span class="mr-2 small pt-1">${evaluation.member.nickname}</span>
                    <span class="stars mr-2" data-score="${evaluation.score}"></span>

                    <button type="button" data-evaluation-id="${evaluation.evaluationId}"
                            class="btn btn-success btn-sm text-white float-right" style="margin-top: -3px;">
                        <img style="width: 24px;margin-top: -5px;" class="mr-1"
                             src="https://img3.doubanio.com/f/talion/7a0756b3b6e67b59ea88653bc0cfa14f61ff219d/pics/card/ic_like_gray.svg"/>
                        <span>${evaluation.enjoy}</span>
                    </button>
                </div>

                <div class="row mt-2 small mb-3">
                    ${evaluation.content}
                </div>
                <hr/>
            </div>
        </#list>
    </div>

 

 

以上是关于基于Spring+SpringMVC+MyBatis开发书评网图书详情页-显示评论列表开发的主要内容,如果未能解决你的问题,请参考以下文章

spring与mybati整合方法

自己开发的音乐视频网站

Spring Boot整合Mybati之逆向工程

抽奖活动啦!5本SpringMVC+MyBatis相关3本Android Studio相关6本Kafka相关

spring+mybati java config配置引起的bean相互引用日志报警告问题

Spring基于注解及SpringMVC