首先,在pom.xml文件中加入:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
然后,在resources中创建一个mybatis.config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<property name="params" value="pageNum=start;pageSize=limit;"/>
<!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="check"/>
</plugin>
</plugins>
</configuration>
然后再配置mybatis,如:
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:org/andy/shop/mapping/*.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
这里配置和ssm整合是一样的,就是在这里加多一句
<property name="configLocation" value="classpath:mybatis-config.xml"/>
就这样子整合完成了。
然后写一个实体类:
package com.terry.entity;
/**
* 分页查询
* @author KamTo Hung
*
*/
public class PageTerry {
private int limit = 10;//每页数据的数量,默认为十条
private int offset = 1;//第几页,默认为第一页
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
@Override
public String toString() {
return "PageTerry [limit=" + limit + ", offset=" + offset + "]";
}
}
在mapping中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.terry.dao.CinemaDao">
<sql id="select_sql">
select cinema_id as cinemaId,cinema_name as cinemaName,cinema_address as cinemaAddress,cinema_phone as cinemaPhone,cinema_message as cinemaMessage,cinema_score as cinemaScore from cinema
</sql>
<select id="findList" resultType="com.terry.entity.Cinema">
<include refid="select_sql"/>
<where><if test="cinemaId != null">and cinema_id like #{cinemaId}</if>
<if test="cinemaName != null">and cinema_name like #{cinemaName}</if>
<if test="cinemaAddress != null">and cinema_address like #{cinemaAddress}</if>
<if test="cinemaPhone != null">and cinema_phone like #{cinemaPhone}</if>
<if test="cinemaMessage != null">and cinema_message like #{cinemaMessage}</if>
<if test="cinemaScore != null">and cinema_score like #{cinemaScore}</if>
</where>
</select>
</mapper>
这里和你自己原本查询的方法是一样的,没什么改变;
public interface Service {
<T> List<T> findList(Object query);
}
这里接口也没变化;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml",
"classpath:spring-mybatis.xml" })
public class TestUserService extends AbstractJUnit4SpringContextTests {
@Resource(name="cinemaService")
private CinemaService cinemaService;
private static final Logger LOGGER = Logger.getLogger(TestUserService.class);
@Test
public void test08(){
PageTerry pageTerry = new PageTerry();
PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());
List<Cinema> list = cinemaService.findList(new Cinema());
LOGGER.info("++++++++++++++++++++++++++++++++++++++++"+JSON.toJSONString(list));
}
}
这里是测试类,重要的是
PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());
这条语句,第一个参数是第几页,第二个参数是每页显示的数据的数量;
结果为:
++++++++++++++++++++++++++++++++++++++++[{"cinemaAddress":"增城区增江街沿江东三路15号1978创意园B区1号","cinemaId":1,"cinemaMessage":"15:45,17:45,18:25,19:10,19:45,20:25,21:10,21:45,22:25","cinemaName":"1978电影城","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"番禺区亚运大道1号永旺梦乐城4F","cinemaId":2,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"CGV星聚汇影城(永旺店)","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"白云区尚景街1号白云尚城文化活动中心 ","cinemaId":3,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"IDC国际影城(白云尚城店) ","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"海珠区江燕路108号燕汇广场4楼 ","cinemaId":4,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"SFC上影影城(海珠燕汇店)","cinemaPhone":"123456","cinemaScore":5}]
只有前五条数据,实现了分页。
在controller中,也是加入
PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());
就能实现分页。
至于要计算一共有多少页,可以先获取数据的总数,然后
int total;//获取总数
int size = 10;//每页显示10条数据
int page = total % size == 0 ? total / size : total / size +1;//如果能除尽,则刚好,否则加1页
我会把page(页数)的值传到前端,跟着列出页数。
当点击某一页的时候,就会把当前的页数传到后台,根据:
PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());//pageTerry.getOffset()为某一页页数的值
至于上一页和下一页的实现,就获取当前某一页页数的值加一即可。
具体的后台ssm框架利用pagehelper实现分页就这样子~
前端的分页明天再说~