web开发学习笔记五——thymeleaf

本文记录了使用Thymeleaf实现Web开发的过程,包括首页数据展示、DAO层、Service层和Controller层的功能实现,以及分页功能的详细步骤。通过创建实体类、接口、Mapper文件和测试,结合Thymeleaf模板引擎动态渲染首页内容,实现了对数据库中数据的分页展示。
摘要由CSDN通过智能技术生成

一次请求的执行过程

实现首页显示

- 实现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排-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值