Mybatis分页插件-PageHelper的使用

来源:互联网 发布:2016免费洗车软件 编辑:程序博客网 时间:2024/06/01 22:25

Mybatis分页插件-PageHelper的使用

怎样配置mybatis这里就不提了,我来说说我配置这个分页插件的过程吧。

下载JAR包

分页插件pagehelper.jar:

https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ 
http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/ 
由于使用了sql解析工具,你还需要下载jsqlparser.jar

4.1.0及以后版本需要0.9.4版本

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.4/ 
4.1.0以前版本需要0.9.1版本

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.1/

一、首先,在spring-mybatis.xml中是这样配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath:com/sinyat/api/weather/mapping/*.xml" />        <property name="typeAliasesPackage" value="com.sinyat.api.weather.model" />        <property name="plugins">            <array>                <bean class="com.github.pagehelper.PageHelper">//如果pagehelper是5.0以后的版本则是<bean class="com.github.pagehelper.PageIntercepter">                    <property name="properties">                        <value>                            dialect=mysql  //5.0以后的版本为 helperDialect=mysql                        </value>                    </property>                </bean>            </array>        </property>    </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

我是在spring里配置的,还可以在mybatis-config.xml里配置,有兴趣的话可以百度下。

二、需要分页,自然就还要一个查询了。用了PageHelper之后,查询语句就可以很简单了。

<select id="selectAll" resultMap="BaseResultMap">    select *  from citylist  </select>
  • 1
  • 2
  • 3
  • 4
  • 5

这是我的查询语句,查询城市列表。没有其他条件,就是查所有。当然,我这只是一个小的demo,没做那么麻烦,真正的使用,是需要条件查询的。 
现在来讲讲用了PageHelper之后的好处: 
那就是查询语句可以不用limit,但是就一点好处吗?自然不是。请接着往下看。

三、接口、实现类我就不再贴代码了,该怎么写还是怎么写,不需要传分页参数。然后是Controller:

@RequestMapping("showcity")    public ModelAndView showCityList(ModelAndView mv,            @RequestParam(required=true,defaultValue="1") Integer page,            @RequestParam(required=false,defaultValue="10") Integer pageSize){        PageHelper.startPage(page, pageSize);        List<CityList> list = cityListService.selectAll();        PageInfo<CityList> p=new PageInfo<CityList>(list);                mv.addObject("citylist", list);        mv.addObject("page", p);        mv.setViewName("weather/showCityList");        return mv;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

可以看到,方法里有两个参数,前面也说了,这是一个demo,仅仅只是一个非常简单的分页效果。

PageHelper.startPage(page, pageSize);
  • 1

这段代码表示,程序开始分页了,page默认值是1,pageSize默认是10,意思是从第1页开始,每页显示10条记录。

PageInfo这个类是插件里的类,这个类里面的属性还是值得看一看:

    //当前页    private int pageNum;    //每页的数量    private int pageSize;    //当前页的数量    private int size;    //排序    private String orderBy;    //由于startRow和endRow不常用,这里说个具体的用法    //可以在页面中"显示startRow到endRow 共size条数据"    //当前页面第一个元素在数据库中的行号    private int startRow;    //当前页面最后一个元素在数据库中的行号    private int endRow;    //总记录数    private long total;    //总页数    private int pages;    //结果集    private List<T> list;    //第一页    private int firstPage;    //前一页    private int prePage;    //下一页    private int nextPage;    //最后一页    private int lastPage;    //是否为第一页    private boolean isFirstPage = false;    //是否为最后一页    private boolean isLastPage = false;    //是否有前一页    private boolean hasPreviousPage = false;    //是否有下一页    private boolean hasNextPage = false;    //导航页码数    private int navigatePages;    //所有导航页号    private int[] navigatepageNums;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

页面的jsp代码:

 <div><a href="${pageContext.request.contextPath }/sys/role/list?page=${pageInfo.firstPage}">首页</a>  <a href="${pageContext.request.contextPath }/sys/role/list?page=${pageInfo.prePage }">上一页</a>    <a href="${pageContext.request.contextPath }/sys/role/list?page=${pageInfo.nextPage}">下一页</a>  <a href="${pageContext.request.contextPath }/sys/role/list?page=${pageInfo.lastPage}">最后</a>    <span>当前第${pageInfo.pageNum }页;总共${pageInfo.pages }页</span>    <form action="${pageContext.request.contextPath }/sys/role/list" style="display:inline;width:40px;height:18px">跳到第 <input type="text" name="page" style="width:20px;">页    <input type="submit" value="Go" style="width:26px;padding:1px;height:24px"></form>                </div>








原创粉丝点击