开发思路:评论功能实现主要分为:
1、创建评论表及表关联
2、xml处理逻辑以及相关文件
3、Controller层编写
1、创建评论表及表关联
创建动态评论表:t_dynamic_reply
动态评论表和动态表及用户表外键关联
2、xml处理逻辑以及相关文件
编写DynamicReplyMapper.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.zhuoying.yingyongji.dao.DynamicReplyDao"> <resultMap id="baseResultMap" type="com.zhuoying.yingyongji.model.DynamicReply"> <id property="id" column="id" /> <result property="userId" column="user_id" /> <result property="replyDynamicId" column="reply_dynamic_id" /> <result property="replyReplyId" column="reply_reply_id" /> <result property="replyUserId" column="reply_user_id" /> <result property="content" column="content" /> <result property="createTime" column="create_time" /> <result property="floorNum" column="floor_num" /> <association property="user" column="user_id" select="com.zhuoying.yingyongji.dao.UserDao.findByIdSimple" /> <association property="replyUser" column="reply_user_id" select="com.zhuoying.yingyongji.dao.UserDao.findByIdSimple" /> </resultMap> <resultMap id="detailResultMap" extends="baseResultMap" type="com.zhuoying.yingyongji.model.DynamicReply"> </resultMap> <sql id="baseTable">t_dynamic_reply</sql> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO <include refid="baseTable" />(user_id,reply_dynamic_id,reply_reply_id,reply_user_id,content,create_time,floor_num) VALUES <foreach collection="array" item="item" separator=","> (#{item.userId},#{item.replyDynamicId},#{item.replyReplyId},#{item.replyUserId},#{item.content},#{item.createTime},#{item.floorNum}) </foreach> </insert> <select id="list" resultMap="baseResultMap"> SELECT * FROM <include refid="baseTable" /> <if test="param != null"> <where> <if test="param.userId != null">AND user_id=#{param.userId}</if> <if test="param.replyDynamicId != null">AND reply_dynamic_id=#{param.replyDynamicId}</if> <if test="param.replyReplyId != null">AND reply_reply_id=#{param.replyReplyId}</if> <if test="param.replyUserId != null">AND reply_user_id=#{param.replyUserId}</if> <if test="param.content != null">AND content=#{param.content}</if> <if test="param.createTime != null">AND create_time=#{param.createTime}</if> <if test="param.floorNum != null">AND floor_num=#{param.floorNum}</if> </where> </if> </select> <select id="findMaxFloorNum" resultType="java.lang.Integer"> SELECT IFNULL(MAX(floor_num),0) FROM <include refid="baseTable" /> WHERE reply_dynamic_id=#{dynamicId} </select> <select id="findById" resultMap="detailResultMap"> SELECT * FROM <include refid="baseTable" /> WHERE id=#{id} </select> <select id="findByIdSimple" resultMap="baseResultMap"> SELECT id FROM <include refid="baseTable" /> WHERE id=#{id} </select> <update id="updateByIds"> UPDATE <include refid="baseTable" /> <set> <if test="param.userId != null">user_id=#{param.userId},</if> <if test="param.replyDynamicId != null">reply_dynamic_id=#{param.replyDynamicId},</if> <if test="param.replyReplyId != null">reply_reply_id=#{param.replyReplyId},</if> <if test="param.replyUserId != null">reply_user_id=#{param.replyUserId},</if> <if test="param.content != null">content=#{param.content},</if> <if test="param.createTime != null">create_time=#{param.createTime},</if> <if test="param.floorNum != null">floor_num=#{param.floorNum},</if> </set> WHERE id IN <foreach collection="ids" item="item" open="(" separator="," close=")"> #{item} </foreach> </update> <delete id="deleteByIds"> DELETE FROM <include refid="baseTable" /> WHERE id IN <foreach collection="array" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete> </mapper>
model层的 DynamicReply 类
@Data @Accessors(chain = true) public class DynamicReply { private Long id; private Long userId; private Long replyDynamicId; private Long replyReplyId; private Long replyUserId; private String content; private Date createTime; private Integer floorNum; private User user; private User replyUser; private String createTimeDesc; }
Dao层 DynamicReplyDao 类
@Repository public interface DynamicReplyDao extends BaseDao<DynamicReply, Long> { Integer findMaxFloorNum(Long dynamicId); }
由于BaseDao接口已经封装好xml使用的方法,所以直接继承
public interface BaseDao<T, ID> { int insert(T... beans); List<T> list(@Param("param") T beans); List<T> listDetail(@Param("param") T param); int count(@Param("param") T param); T findById(ID id); T findByIdSimple(ID id); int updateByIds(@Param("param") T param, @Param("ids") ID... ids); int deleteByIds(ID... ids); }
3、Controller层编写
@Api(tags = DynamicController.TAG_DESC + "接口") @RestController @RequestMapping("/api/dynamic/reply") public class DynamicReplyController { public static final String TAG_DESC = "动态评论"; @Autowired private DynamicService dynamicService; @Autowired private DynamicReplyService dynamicReplyService; @Transactional @ApiOperation("新增" + TAG_DESC) @PostMapping("/add") public JsonResult add(@ValidRequestParam(desc = "用户Id") Long user_id, @ValidRequestParam(desc = "回复动态Id") Long reply_dynamic_id, @ValidParam(desc = "回复评论id") Long reply_reply_id, @ValidParam(desc = "回复用户id") Long reply_user_id, @ValidRequestParam(desc = "内容") String content, @ValidRequestParam(desc = "创建时间", defaultValue = ValidDateType.CURRENT_TIME) Date create_time) { dynamicService.updateByIds(new Dynamic().setUpdateTime(new Date()), reply_dynamic_id); DynamicReply dynamicReply = new DynamicReply() .setUserId(user_id) .setReplyDynamicId(reply_dynamic_id) .setReplyReplyId(reply_reply_id) .setReplyUserId(reply_user_id) .setContent(content) .setCreateTime(create_time) .setFloorNum(dynamicReplyService.findMaxFloorNum(reply_dynamic_id) + 1); dynamicReplyService.insert(dynamicReply); return JsonResult.okAndReturnId(dynamicReply.getId()); } @ApiOperation("查询动态评论列表") @GetMapping("/list") public JsonResult list(@ApiParam("用户id") Long user_id, @ApiParam("回复动态id") Long reply_dynamic_id, @ApiParam("回复评论id") Long reply_reply_id, @ApiParam("回复用户id") Long reply_user_id, @ApiParam("内容") String content, @ApiParam("创建时间") Date create_time, @ValidModel PageParam pageParam, @RequestParam(defaultValue = "id desc") @ApiParam("排序") String sort) { DynamicReply dynamicReply = new DynamicReply() .setUserId(user_id) .setReplyDynamicId(reply_dynamic_id) .setReplyReplyId(reply_reply_id) .setReplyUserId(reply_user_id) .setContent(content) .setCreateTime(create_time); PageHelper.startPage(pageParam.getPage_num(), pageParam.getPage_size(), sort); return JsonResult.data(new MyPageInfo<>(dynamicReplyService.list(dynamicReply))); } @ApiOperation("根据id查询动态评论") @GetMapping("/get") public JsonResult select(@RequestParam Long id) { return JsonResult.data(dynamicReplyService.findById(id)); } @ApiOperation("根据id查询动态(基础)") @GetMapping("/get/simple") public JsonResult selectSimple(@RequestParam Long id) { return JsonResult.data(dynamicReplyService.findByIdSimple(id)); } @ApiOperation("修改动态评论") @PostMapping("/update") public JsonResult update(@RequestParam Long[] id, @ValidParam(desc = "用户id") Long user_id, @ValidParam(desc = "回复动态id") Long reply_dynamic_id, @ValidParam(desc = "回复评论id") Long reply_reply_id, @ValidParam(desc = "回复用户id") Long reply_user_id, @ValidParam(desc = "内容") String content, @ValidParam(desc = "创建时间") Date create_time) { DynamicReply dynamicReply = new DynamicReply() .setUserId(user_id) .setReplyDynamicId(reply_dynamic_id) .setReplyReplyId(reply_reply_id) .setReplyUserId(reply_user_id) .setContent(content) .setCreateTime(create_time); dynamicReplyService.updateByIds(dynamicReply, id); return JsonResult.ok(); } @ApiOperation("删除动态评论") @PostMapping("/delete") public JsonResult delete(@RequestParam Long[] id) { dynamicReplyService.deleteByIds(id); return JsonResult.ok(); } }
4、Api文档
用于记录本人的开发经历
新手小白,请多体谅、指教