mybatis中分页插件的使用:(基于SSM+Maven+jsp)

在这里插入图片描述
最近在自己学习过程中,学到mybatis时遇到分页的问题,尤记起mybatis中有一个分页的插件的使用,于是开始学习,但是使用的过程中还是遇到各种的bug,如果学会的话,其实很简单,但是第一次使用还是遇到不少的坑,下面和大家分享一下第一次使用中遇到的坑以及解决的方式。因为正在学习的是SSM框架,所以如果还没使用过的建议学完以后再来参考,不然有些配置可能会出现疑惑。好了,进入正题。
首先在配置文件中进行配置:
1、引入jar包,使用的maven项目,所以直接引入依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>

如果是导入jar包的,那么可以直接下载,放在工程中引入即可

2、spring和mybatis完美结合,所以配置文建可以配置在spring配置文件中:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 扫描model包
    如果 typeAliasesPackage不进行配置,resultType就得写全名resultType="com.tf.entity.User",
    但是如果配置了
    <property name="typeAliasesPackage" value="com.tf.entity"/>,
    resultType就可以不用写上包名了,而是直接书写类名即可
    -->
    <property name="typeAliasesPackage" value="com.tf.entity"/>
    <!-- 扫描sql配置文件:mapper需要的xml文件-->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <props>
                        <prop key="dialect">Oracle</prop>
                        <prop key="offsetAsPageNum">false</prop>
                        <prop key="rowBoundsWithCount">true</prop>
                        <prop key="reasonable">true</prop>
                        <prop key="pageSizeZero">true</prop>
                        <prop key="returnPageInfo">check</prop>
                    </props>
                </property>
            </bean>
        </array>
    </property>
</bean>
<property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <props>
                        <prop key="dialect">Oracle</prop>
                        <prop key="offsetAsPageNum">false</prop>
                        <prop key="rowBoundsWithCount">true</prop>
                        <prop key="reasonable">true</prop>
                        <prop key="pageSizeZero">true</prop>
                        <prop key="returnPageInfo">check</prop>
                    </props>
                </property>
            </bean>
        </array>
    </property>

这一段代码就是分页插件需要引入的核心内容,前面的东西如果你的项目要跑数据库都是必须配置的。注意还有一个属性dialect要根据自己的实际数据库来配置,如果是mysql那么就要修改成mysql

还有一个坑:就是如果引入的jar包是5.0版本以后的,那么在spring配置文件中会直接报错,所以配置的时候要注意,如果一定要使用5.0版本的,那么就需要使用另外一种配置方式,就是在mybatis配置文件中直接配置插件。

3、数据持久层和业务层的代码该怎么写就怎么写,没有任何的变化

4、接着就是控制层代码了:

@RequestMapping("findResume")
public ModelAndView findResumeList(@RequestParam(required=true,defaultValue="1") Integer pageNo,
                                   @RequestParam(required=false,defaultValue="5") Integer pageSize){
    PageHelper.startPage(pageNo, pageSize);
    ModelAndView mv=new ModelAndView();
    List<Resume> list = resumeService.findAllResume();
    PageInfo<Resume> page=new PageInfo<Resume>(list);
    //将查询到的所有记录数保存在域对象中
    mv.addObject("list",list);
    //将查询出来的信息带上分页信息保存在域对象中
    mv.addObject("page",page);
    mv.setViewName("success");
    return mv;
}

5、在jsp或者html中引入分页:

<c:if test="${page.isFirstPage==true}">
  <button class="btn btn-default btn-info disabled">首页</button>
  <button class="btn btn-default btn-info disabled">上一页</button>
</c:if>
<c:if test="${page.isFirstPage!=true}">
  <a href="findResume?pageNo=${page.firstPage}" class="btn btn-default btn-info">首页</a>
  <a href="findResume?pageNo=${page.prePage}" class="btn btn-default btn-info">上一页</a>
</c:if>
<c:if test="${page.isLastPage==true}">
  <button class="btn btn-default btn-info disabled">下一页</button>
  <button class="btn btn-default btn-info disabled">尾页</button>
</c:if>
<c:if test="${page.isLastPage!=true}">
  <a href="findResume?pageNo=${page.nextPage}" class="btn btn-default btn-info">下一页</a>
  <a href="findResume?pageNo=${page.lastPage}" class="btn btn-default btn-info">尾页</a>
</c:if>

使用JSTL和EL表达式必须导入依赖:

<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
一定要导入jsp中的jstl库,不然可能会报错的。
这样mybatis的分页插件的使用就结束了。注意上面代码中有一些东西是自己项目的东西,需要自己修改。

代码是不完整的,如果要具体代码,网上有很多类似的功能,我记录的只是在这个过程中遇到的一些问题,虽然代码很简单,但还是需要细心,当然这是使用插件,能力强的完全可以自己写分页代码和分页逻辑,可以样式还可以自己修改,写出更好看、更高效的分页代码。我也在学习的路上。

发布了33 篇原创文章 · 获赞 37 · 访问量 4390

猜你喜欢

转载自blog.csdn.net/weixin_42142899/article/details/103269281