博客系统 - 注册功能-博客评论
第1关:博客评论
Step1Mapper.java
package net.educoder.mapper;
import net.educoder.entity.TComment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
@Mapper
public interface Step1Mapper {
/**
* 添加评论
*
* 使用的表为:t_comment
*
* @param comment 评论对象
*/
/********** Begin **********/
@Insert("insert into t_comment(commentContent,blogId,createTime,userId,replyId) values (#{commentContent},#{blogId},#{createTime},#{userId},#{replyId})")
@Options(useGeneratedKeys = true, keyProperty = "commentId")
void AddComment(TComment comment);
/********** End **********/
}
Step1ServiceImpl.java
package net.educoder.service.impl;
import net.educoder.entity.TComment;
import net.educoder.mapper.Step1Mapper;
import net.educoder.service.Step1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
@Transactional
public class Step1ServiceImpl implements Step1Service {
/**
* Step1Mapper 对象注入
*/
@Autowired
Step1Mapper mapper;
/**
* 创建评论
*
* @param comment 评论对象
* @return 评论ID
*/
@Override
public long AddComment(TComment comment) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = format.format(date);
/********** Begin **********/
//1.设置评论创建时间
comment.setCreateTime(dateStr);
//2.添加评论
mapper.AddComment(comment);
//3.获取评论ID
long commentId = comment.getCommentId();
return commentId;
/********** End **********/
}
}
Step1Controller.java
package net.educoder.controller;
import net.educoder.entity.TComment;
import net.educoder.service.Step1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/blog")
public class Step1Controller {
/**
* Step1Service 对象注入
*/
@Autowired
Step1Service step1Service;
/**
* 评论接口
*
* @param comment 评论对象
* @return 返回JSON对象,格式如下:
* <p>
* {
* "commentId":评论ID
* }
*/
@RequestMapping("/comment")
@ResponseBody
public Map<String, Object> comment(TComment comment) {
Map<String, Object> map = new HashMap<>();
/********** Begin **********/
long commentId = step1Service.AddComment(comment);
map.put("commentId", commentId);
/********** End **********/
return map;
}
}
第2关:评论列表
Step2Mapper.java
package net.educoder.mapper;
import net.educoder.entity.TComment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface Step2Mapper {
/**
* 通过博客ID,查询该博客下的所有评论
* <p>
* 使用的表为:t_comment
*
* @param blogId 博客ID
* @return 返回评论列表
*/
/********** Begin **********/
@Select("select * from t_comment where blogId = #{blogId}")
List<TComment> findCommentByBlogId(String blogId);
/********** End **********/
}
Step2ServiceImpl.java
package net.educoder.service.impl;
import net.educoder.entity.TComment;
import net.educoder.mapper.Step2Mapper;
import net.educoder.service.Step2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class Step2ServiceImpl implements Step2Service {
/**
* Step2Mapper 对象注入
*/
@Autowired
Step2Mapper mapper;
/**
* 通过博客ID,获取该博客下的所有评论
*
* @param blogId 博客ID
* @return 返回评论列表
*/
@Override
public List<TComment> findCommentByBlogId(String blogId) {
/********** Begin **********/
List<TComment> commentList = mapper.findCommentByBlogId(blogId);
return commentList;
/********** End **********/
}
}
Step2Controller.java
package net.educoder.controller;
import net.educoder.entity.TComment;
import net.educoder.service.Step2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/blog")
public class Step2Controller {
/**
* Step2Service 对象注入
*/
@Autowired
Step2Service step2Service;
/**
* 通过博客ID,查询评论列表
*
* @param blogId 博客ID
* @return 返回JSON对象,格式如下:
* <p>
* {
* "commentList":评论列表
* }
*/
@RequestMapping("/findComment")
@ResponseBody
public Map<String, Object> findComment(String blogId) {
Map<String, Object> map = new HashMap<>();
/********** Begin **********/
List<TComment> commentList = step2Service.findCommentByBlogId(blogId);
map.put("commentList", commentList);
/********** End **********/
return map;
}
}