现在有一需求,要求页面显示悬赏列表,要求该悬赏未过期,没有人接受以及附带分页查询。
这里我们很容易能得到满足该需求的sql语句:
select *
from bounty
where DeadTime > nowtime AND status = 0 limit (page-1) , count
其中nowtime为当前时间,0是我设置的未接收参数,page为页数,count为每页条数.
当项目使用mybatis自动生成增删改查代码功能时,我们要运行上述sql语句必须要在**Mapper.xml文件中添加动态sql函数。
我们新建一个select函数
由于这里我们需要传三个参数,date、page以及count且函数类型不同,所以我们只能选择用一个自定义对象来保存三个参数并传入**Mapper.xml文件.
public class Paging {
private String date;
private int page;
private int count;
}
这里注意一下,我们自定义的class里面一定要手动添加getter和setter方法,可以用快捷键alt+Ins选择getter and setter快捷生成。
**Mapper.xml中的自定义sql语句如下:
<select id="selectTime" resultMap="BaseResultMap" parameterType="com.springmvc.entity.Paging" >
select *
from bounty
where DeadTime > #{date} AND status = 0 limit #{page} , #{count}
</select>
这样就能将对象的三个参数全部都成功传入该语句。
最后在dao文件以及service文件中添加该函数,ok,可以使用了。