今天面试了一个工作3年的小伙子,竟然连一个分页功能都说不出来…… 这实在不应该呀。不努力35岁你就危险了。
mysql分页:limit 起始下标 每页数量
前端需要传的参数最少有两个,第一个是页数,第二个是每页展示的数量。通过这两个参数我们可以获取起始的下标:( 页数 – 1 ) * 每页数量
至于其它的参数条件大家就自己按照业务需求来就行了,比如根据名字是模糊查询呀,或者根据地址查询等等。
分页查询我们一般使用map来进行传值,通过map传值给mybayis。
//控制成代码
Map map = new HashMap();
int pageNo = Integer.parseInt(req.getParameter("pageNo"));
int pageSize = Integer.parseInt(req.getParameter("pageSize"));
map.put("startIndex", (pageNo - 1) * pageSize);
map.put("pageSize", pageSize);
map.put("name", new String(req.getParameter("name").getBytes("ISO-8859-1"), "UTF-8"));
StuDaoImpl daoImpl = new StuDaoImpl();
Poxy poxy = new Poxy(daoImpl);
StuDao stuDao = (StuDao) Proxy.newProxyInstance(daoImpl.getClass().getClassLoader(),
daoImpl.getClass().getInterfaces(), poxy);
Map resMap = stuDao.pageStu(map);
resp.getWriter().print(resMap);
//到层实现类
public Map pageStu(Map map) {
Map pageMap = new HashMap();
int num = SqlSessionUtil.getSqlSession().selectOne("pageCount", map);
List students = SqlSessionUtil.getSqlSession().selectList("selectPageStudent", map);
pageMap.put("pageNum", num);
pageMap.put("dataList", students);
return pageMap;
}
//sql语句
select
count(1)
from
student
and name like '%' #{name} '%'
and address = #{address}
select
*
from
student
and name like '%' #{name} '%'
and address = #{address}
limit
#{startIndex} , #{pageSize}
mybatis分页就是这么简单,为什么回答不上来会有那么多人呢…….
限 时 特 惠: 本站每日持续更新海量各大内部创业教程,一年会员只需98元,全站资源免费下载 点击查看详情
站 长 微 信: lzxmw777
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。