根据帖子的主键id查询贴子的详情内容。
DiscussPostMapper接口
DiscussPost selectDiscussPostById(int id);
discusspost-mapper.xml
<select id="selectDiscussPostById" resultType="DiscussPost">
select <include refid="selectFields"></include>
from discuss_post
where id = #{id}
</select>
DiscussPostService
public DiscussPost findDiscussPostById(int id) {
return discussPostMapper.selectDiscussPostById(id);
}
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";
}
index.html,改超链接。| |是常量和变量拼接的。取帖子主键用${map.post.id}
th:href="@{|/discuss/detail/${map.post.id}|}"
discuss-detail.html,改动态标题、头像、用户名、发帖时间、帖子内容
th:utext="${post.title}"
th:src="${user.headerUrl}"
th:utext="${user.username}"
th:text="${#dates.format(post.createTime,'yyyy-MM-dd HH:mm:ss')}"
th:utext="${post.content}"