目前小编在学习spring+springmvc+mybatis+maven整合开发,在开发后台的时候肯定会用到分页,度娘上面各种五花八门的,代码长串长串的,就没一个能实现的。今天刚填完坑,所以记录一下!
1、首先展示一下效果吧,此处我也写了模糊查询的,当然我没有写css样式,但是功能实现了。
模糊查询展示
2、代码部分(使用PageHelper插件实现分页)
(1)、在项目的pom.xml中添加2个jar包:pagehelper、jsqlparser
<!--
分页插件
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper
-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!--
分页要用到此包
https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser
-->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.2</version>
</dependency>
(2)、在maybatis配置文件中添加一下代码(我的配置文件:spring-mybatis.xml)
<!-- 分页插件 -->
<property name="plugins">
<array>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
重点说一下:此段代码不能放错位置,否则你会发现,PageHelper.startPage(pageNum, pageSize);不管怎么都不能生效!
(3)、在xxxMapper.xml中写你的SQL语句:(我的是:AdminuserMapper.xml)
<!--
用户角色关联模糊查询(根据用户名)
id="getByLike":对应dao(持久层)中AdminuserMapper的getByLike()方法
property="role":对应model层中Adminuser的属性role
javaType="list":表示role返回的是list
-->
<resultMap type="bg.model.Adminuser" id="roleResultMap" extends=