Mysql+Mybatis分页查询

Mysql+Mybatis分页查询一,首先做一个查询所有并显示daopublicinterfaceProductDAO{ publicList<Product>list();}mapper<mappernamespace="hust.mm.dao.ProductDAO"> <selectid="list"resultType="Product"> s..

大家好,又见面了,我是你们的朋友全栈君。

一,首先做一个查询所有并显示

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

<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>

这里以每页三条数据分页显示

三,完善分页

可以想到,当在首页点击上一页和在尾页点击下一页,应该没有反应或者做出相应处理。有两种解决方案,

  1. 使用jstl或el语句判断start参数是否小于0或大于total-分页大小
  2. 在controller对start进行判断

四,分页的其他方案

上述的分页是利用了mybatis的动态SQL以及MySQL数据库特有的limit语句。有一定的特殊性,可以使用PageHelper这一类分页插件来进行分页开发。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137181.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 打开天窗说亮话

    打开天窗说亮话

    2021年8月5日
    59
  • 深入了解 B-Tree 和 B+Tree 的区别

    深入了解 B-Tree 和 B+Tree 的区别

    2022年2月17日
    43
  • clientwidth_JavaScript中的clientWidth和clientHeight[通俗易懂]

    clientwidth_JavaScript中的clientWidth和clientHeight[通俗易懂]clientwidthUsingclientWidthandclientHeightyou’reabletogetthepixeldimensionsofanHTMLelement.ThedimensionsarecalculatedusingthedimensionsofcontentinsidetheHTMLelement,alongw…

    2022年7月22日
    9
  • 距离和相似度度量方法

    距离和相似度度量方法http://blog.csdn.net/pipisorry/article/details/45651315在机器学习和数据挖掘中,我们经常需要知道个体间差异的大小,进而评价个体的相似性和类别。最常见的是数据分析中的相关分析,数据挖掘中的分类和聚类算法,如K最近邻(KNN)和K均值(K-Means)等等。不同距离度量的应用场景根据数据特性的不同,可以采用不同的度量方法。whi…

    2022年6月19日
    33
  • thinkphp访问路径_thinkphp laravel

    thinkphp访问路径_thinkphp laravel下载地址:http://code.google.com/p/kindeditor/downloads/list基本配置:KE.show({id:’your_editor_id’,width:’700px’,height:’400px’});首先引入kindeditor.js文件;用show方法传入一个数组,里面是你的配置信息,然后

    2022年10月12日
    1
  • 《Java编程思想》总结

    《Java编程思想》总结语言实际上是帮助程序员更容易地操作计算机的工具,选择何种语言来编程,是Java还是C++,本质上相当于“选择腾讯视频还是优酷视频来观看电视节目(那么选择汇编语言就是选择了电视机)”。正如腾讯视频是腾讯公司的产品,Java是美国公司Sun的产品。希望读者能明白:语言只是工具。

    2022年7月9日
    20

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号