zoukankan      html  css  js  c++  java
  • mybatis分页插件 pageHelper 在dao、 service , 各模块单元测试及Web层配置使用

     框架:spring+springmvc+mybatis

    使用maven来管理项目
    1:引入依赖jar包
    2:添加pageHelper插件
    3:Dao层单元测试,书写代码
    4:服务层单元测试:书写代码
    5:Web层使用:书写代码

    1:引入依赖jar包 pom.xml

    <!-- 引入mybatis的 pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>


    2:添加pageHelper插件
      idea编辑器ctrl+N 查找类路径,输入pageIntercepter

     spring 中叫做拦截器,在mybatis配置中叫做插件
    此类路径是配置插件的值如下com.github.pagehelper.PageInterceptor

    <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。
                  当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
                <!--<property name="reasonable" value="true"/>-->
            </plugin>
        </plugins>

    哪么mybatis插件要配置到mybatis全局配置变中,即mybatis-config.xml
    特别强调, mybatis配置是有顺序的,查看如下
    ctrl+点击<configuration>
    可以查看配置文件在environments前边一行,复制如上<plugins>……</plugins>

    <!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>

     3:书写代码

    a)在调用dao层书写,采用junit单元测试

         //测试分页后,可删除
        @Test
        public void listRecruitsDaoPerPage(){
         //连接数据库,且数据配置如上步骤2:添加pageHelper插件 SqlSession sqlSession = getSqlSession(); //获取代理对象 RecruitDao mapper = sqlSession.getMapper(RecruitDao.class);
         //此包一定要放在调用数据库之前,即mapper.listRecruitsDaoPerpage PageHelper.startPage(2,3); //2表示是起始页,3表示每页显示行数 List<Recruit> recruits = mapper.listRecruitsDaoPerPage("南通"); //PageInfo封装分页信息,把上一行,记录集recruits,通过构造函数传递给PageInfo计算
            PageInfo<Recruit> pageInfo= new PageInfo<Recruit>(recruits,3);  //3表示有很多分页中,中列出三个页码
    
            System.out.println(pageInfo); //打印能看到PageInfo对象提供所有页码属性,相当多,提供周全
            System.out.println("打印页码"+ Arrays.toString(pageInfo.getNavigatepageNums()));
            Integer i=0;
            for (Recruit recruit:recruits){
                i+=1;
                System.out.println(i+"、"+recruit.getRid()+recruit.getJobTitle()+recruit.getCityCounty());
            }
        }

     4:服务层单元测试书写代码

     因为服务层,单元测试,管理bean是通过applicationContent.xml
    为了加载plugins,需在数据库工厂中,加载插件如下配置,具体见如下代码
    <!--添加pageHelper 分页过虑器, -->
    <property name="plugins">
      
    </property>
    
     <!--引入Mybatis文件-->
            <bean class="org.mybatis.spring.SqlSessionFactoryBean">
                <!--    添加pageHelper 分页过虑器,           begin-->
                <property name="plugins">
                    <array>
                        <bean class="com.github.pagehelper.PageInterceptor">
                            <property name="properties">
                                <value>
                                      helperDialect=mysql
                                </value>
                            </property>
                        </bean>
                    </array>
                </property>
        <!-- 添加pageHelper 分页过虑器,           end -->

    <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis的配置文件 --> <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。 *是个通配符,代表所有的文件,**代表所有目录下 --> <!--强调:xxxMapper.xml配置在resources文件夹下,classpath*:没加*报错--> <property name="mapperLocations" value="classpath*:com/ibaiqi/spider/dao/mapper/*.xml" /> </bean>    

     5:Web层使用:书写代码
    要在web.xml指向的spring-mvc.xml 添加插件配置

        <!--引入Mybatis文件-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--添加pageHelper插件 分页过虑器,           -->
            <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <value>
                                helperDialect=mysql
                            </value>
                        </property>
                    </bean>
                </array>
            </property>
    
            <property name="dataSource" ref="dataSource"/>
            <!-- 加载mybatis的配置文件 -->
            <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
    *是个通配符,代表所有的文件,**代表所有目录下 -->
            <!--强调:xxxMapper.xml配置在resources文件夹下,classpath*:没加*报错-->
            <property name="mapperLocations" value="classpath*:com/ibaiqi/spider/dao/mapper/*.xml" />
        </bean>



    做产品的程序,才是好的程序员!
  • 相关阅读:
    C# EntityFramework 入门之 Code First
    import cx_Oracle ImportError: DLL load failed: 找不到指定的模块。
    unnitest用例按顺序执行方法总结
    Grid + selenium分布式执行自动化测试
    selenium自动化测试工具的使用总结
    使用SQLAlchemy操作已存在的数据库的表
    map()函数 lambda函数 zip()函数的使用
    使用SQLAlchemy操作MySQL以及执行原生的sql语句
    python创建文件/文件夹,判断文件/文件夹是否存在以及os.path模块的使用
    python函数的四种参数定义方式和传递方式
  • 原文地址:https://www.cnblogs.com/asplover/p/12618162.html
Copyright © 2011-2022 走看看