Spring整合MyBatis分页插件

pom引入

        <!--MyBatis分页插件-->
        <!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser -->
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

applicationContext-spring.xml

    <!--配置MyBatis的SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--引用数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--扫描mapper映射文件-->
        <!--<property name="mapperLocations" value="classpath*:mapper/*.xml"/>-->
<!--        <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xml</value>
            </list>
        </property>-->
        <!--别名的配置-->
        <property name="typeAliasesPackage" value="cn.bdqn.ssm.bean"/>

        <!--配置分页插件-->
        <property name="plugins">
            <list>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            <!--方言:指定用什么数据库-->
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </list>
        </property>
        
        <!--引入MyBatis的配置文件-->
        <!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
    </bean>

使用

 // 一定在查询之前,调用分页
        Page<User> pages = PageHelper.startPage(2, 3);
        List<User> userList = userMapper.foreach_map(map);
        System.out.println("当前页:" + pages.getPageNum());
        System.out.println("每页大小:" + pages.getPageSize());
        System.out.println("总页数:" + pages.getPages());
        System.out.println("总记录数:" + pages.getTotal());
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容