Mybatis分页插件
Posted 吐槽村
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis分页插件相关的知识,希望对你有一定的参考价值。
mybatis配置
<!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pagehelper.PageHelper"> <property name="properties"> <props> <prop key="offsetAsPageNum">true</prop> <prop key="rowBoundsWithCount">true</prop> <prop key="pageSizeZero">true</prop> <prop key="reasonable">false</prop> <prop key="supportMethodsArguments">false</prop> </props> </property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/tucaocun/mapping/*.xml"></property> <property name="plugins"> <list> <!-- <ref bean="offsetLimitIntercepter" /> --> <ref bean="pagehelper" /> </list> </property> </bean>
java 类,写一个class继承这个抽象类
public abstract class AbstractPageForm<T extends AbstractPageForm<T>> implements Serializable { private static final long serialVersionUID = 1L; /** * @Description 页码为首页 */ @DefaultValue("1") @QueryParam("pageNum") protected int pageNum = 1; /** * @Description 每页显示数量,默认为10 */ @DefaultValue("10") @QueryParam("pageSize") protected int pageSize = 10; public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * @Title enablePaging * @Description 启用分页 * @return */ @SuppressWarnings("unchecked") public final T enablePaging() { PageHelper.startPage(pageNum, pageSize); return (T) this; } }
mapper.xml (CourseForm继承了AbstractPageForm的抽象类)
<?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.*.*DAO" > <resultMap id="BaseResultMap" type="com.*.Class" > <id column="id" property="id" jdbcType="INTEGER" /> </resultMap> <select id="selectCourseList" resultMap="BaseResultMap" parameterType="com.*.form.CourseForm" > select <include refid="Base_Column_List" /> from TABLE_NAME </select> </mapper>
分页类示例:
@Override public PageInfo<Class> queryList(CourseForm courseForm) {
//Class需要替换成自己的类 List<Class> selectList = organizationDAO.selectList(courseForm.enablePaging()); PageInfo<Class> pageInfo = new PageInfo<Class>(selectList); return pageInfo; }
pageInfo,POM配置
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.5</version> </dependency>
以上是关于Mybatis分页插件的主要内容,如果未能解决你的问题,请参考以下文章