Mysql+Mybatis分页查询——数据库系列学习笔记
生活随笔
收集整理的這篇文章主要介紹了
Mysql+Mybatis分页查询——数据库系列学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、首先做一個查詢所有并顯示
dao
public interface ProductDAO {public List<Product> list(); }mapper
<mapper namespace="hust.mm.dao.ProductDAO"><select id="list" resultType="Product">select * from product</select> </mapper>Controller
@RequestMapping("/list.do") public ModelAndView productlist(){ModelAndView mav = new ModelAndView();List<Product> products = productDao.list();mav.addObject("products", products);mav.setViewName("productList");return mav; }jsp
<table align="center"><th><td>id</td><td>name</td><td>price</td></th><c:forEach items="${products }" var="p" varStatus="st"><tr><td>${p.id }</td><td>${p.name }</td><td>${p.price }</td></tr></c:forEach> </table>以上簡要給出了一個表中的所有數據
二、分頁顯示
修改dao:傳入起始查詢位置和每次需查詢的條數
public interface ProductDAO {public List<Product> list();public List<Product> list(@Param("start") int start, @Param("count") int count); }修改mapper:限制每次的查詢數量,每次從數據庫中第start條數據開始查詢,查詢count條。
<mapper namespace="hust.mm.dao.ProductDAO"><select id="list" resultType="Product">select * from product<if test="start!=null and count!=null">limit #{start},#{count}</if></select> </mapper>修改Controller
@RequestMapping("/list.do") public ModelAndView productlist(int start){ModelAndView mav = new ModelAndView();List<Product> products = productDao.list(start,3);mav.addObject("products", products);mav.addObject("start", start);mav.setViewName("productList");return mav; }修改jsp:每頁顯示三條數據,支持翻頁
<table align="center"><th><td>id</td><td>name</td><td>price</td></th><c:forEach items="${products }" var="p" varStatus="st"><tr><td>${p.id }</td><td>${p.name }</td><td>${p.price }</td></tr></c:forEach><tr><td><a href="list.do?start=${start-3 }">上一頁</a></td><td><a href="list.do?start=${start+3 }">下一頁</a></td></tr> </table>總結
以上是生活随笔為你收集整理的Mysql+Mybatis分页查询——数据库系列学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql中limit的用法——数据库系列学
- 下一篇: Leecode02-两数相加——Leec