1. 大体思路
点击帖子,会打开一个链接,把帖子的内容显示完整。
按照正常的开发流程:数据层 - 服务层 - 页面。
2. 数据层
①在 dao.DiscussPostMapper类下添加增删改查方法:
selectDiscussPostById 根据主键查询帖子。
//根据主键查询帖子
DiscussPost selectDiscussPostById(int id);
②在 resources.mapper.discusspoat-mapper.xml 下添加上面Mapper对应的SQL语句
<select id="selectDiscussPostById" resultType="DiscussPost">
select <include refid="selectFields"></include>
from discuss_post
where id = #{id}
</select>
3. 服务层
在service.DiscussPostService 添加方法:
//根据ID查询帖子
public DiscussPost findDiscussPostById(int id){
return discussPostMapper.selectDiscussPostById(id);
}
4. 页面
在controller.DiscussPostController 控制器类中添加控制器方法,用来处理点击帖子查看详情时的页面跳转。
@RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model){
//查询得到帖子
DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
//将帖子传给模板
model.addAttribute("post",post);
//查询出发帖人
User user = userService.findUserById(post.getUserId());
model.addAttribute("user", user);
return "/site/discuss-detail";
}