一次请求的执行过程
实现首页显示
- 实现DAO层的功能
1. 在entity包下新建DiscussPost实体类,根据表内的字段定义属性id、userId、title、content、type、status、createTime、commentCount、score,并编写getter and setter方法
2. 在dao包下新建DIscussPostMapper接口,定义查询方法
@Mapper
public interface DiscussPostMapper {
// 显示首页的帖子,userId可不需要,为了后续加入个人主页的功能,offset表示查询起始行,limit为每页显示多少帖子
List<DiscussPost> selectDiscussPosts(int userId, int offset, int limit);
// @Param用于给参数起一个别名,
// 当方法有且只有一个参数,且使用在动态条件<if>中,则必须起别名
int selectDiscussPostRows(@Param("userId") int userId);
}
3. 在mapper包下新建discusspost-mapper.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.lyt.community.dao.DiscussMapper">
<sql id="selectFields">
<!--定义查询的字段,方便后续复用-->
id, user_id, title, content, type, status, create_time, comment_count, score
</sql>
<select id="selectDiscussPosts" resultType="DiscussPost">
<!--返回类型为DiscussPost构成的List,其中List为Java自带的类型不用特意声明,只需要声明自定义的类DiscussPost即可-->
select <include refid="selectFields"></include>
from discuss_post
<!--#{id}引入方法中的参数id-->
where status != 2
<if test="userId != 0">
and user_id = #{userId}
</if>
order by type desc, create_time desc
<!--按照type排序,1表示置顶,0表示普通,type一样则根据create_time排-