mybatis 使用pagehelper-spring-boot-starter 与 pagehelper 的区别

你说的曾经没有我的故事 提交于 2020-01-07 02:11:37

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

一、在使用 

pagehelper-spring-boot-starter时

在pom.xml 引入

<!-- mybatis 分页 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

在application.yml 配置 分页插件信息

#mybatis 分页
pagehelper:
  helperDialect: oracle
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

新建一个分页封装工具

public class PageUtils {

    /**
     * 将分页信息封装到统一的接口
     * @param pageRequest
     * @param pageInfo
     * @return
     */
    public static PageResult getPageResult(QueryParams pageRequest, PageInfo<?> pageInfo) {
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalSize(pageInfo.getTotal());
        pageResult.setTotalPages(pageInfo.getPages());
        pageResult.setContent(pageInfo.getList());
        return pageResult;
    }
}

在service 使用分页插件

//queryParams.getPage() 页数,queryParams.getRows() 行数 
PageHelper.startPage(queryParams.getPage(),queryParams.getRows());
List<SysUser> list = sysUserMapper.listPage();
PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(list);
PageResult pageResult = PageUtils.getPageResult(queryParams,pageInfo);

二、在使用pagehelper  时

在pom.xml 引入

<!-- mybatis分页-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.4</version>
</dependency>

在mybatisConfig.xml加上

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory"
      class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    <property name="typeAliasesPackage" value="com.xxx.xxx.common.model"></property>
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                <property name="properties">
                    <value>
                        helperDialect=oracle
                        reasonable=true
                        supportMethodsArguments=true
                        params=count=countSql
                        autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
        </array>
    </property>
    <!-- 配置mybatis配置文件的位置 -->
    <!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
</bean>

工具类新建一个也是一样的就可以,调用方式一样,就是在不同的地方配置,一个是使用spring boot 的自动配置,

一个是我们自己在mybatisConfig.xm 自己配置。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!