• SSM框架中分页插件PageInterceptor


    package com.ita.config;
    
    import com.github.pagehelper.PageInterceptor;
    import org.apache.ibatis.plugin.Interceptor;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.mapper.MapperScannerConfigurer;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    
    import javax.sql.DataSource;
    import java.util.Properties;
    
    public class MyBatisConfig {
        /**配置PageInterceptor分页插件*/
        @Bean
        public PageInterceptor getPageInterceptor() {
            PageInterceptor pageIntercptor = new PageInterceptor();
            Properties properties = new Properties();
            properties.setProperty("value", "true");
            pageIntercptor.setProperties(properties);
            return pageIntercptor;
        }
        /*
        定义MyBatis的核心连接工厂bean,
        等同于<bean class="org.mybatis.spring.SqlSessionFactoryBean">
         参数使用自动装配的形式加载dataSource,
        为set注入提供数据源,dataSource来源于JdbcConfig中的配置
         */
        @Bean
        public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource,@Autowired PageInterceptor pageIntercptor){
            SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
            //等同于<property name="dataSource" ref="dataSource"/>
            ssfb.setDataSource(dataSource);
            Interceptor[] plugins={pageIntercptor};
            ssfb.setPlugins(plugins);
            return ssfb;
        }
      
    }

     1.在 pom.xml 中添加如下:

    <properties>
      <pagehelper-version>5.0.0</pagehelper-version>
      <jsqlparser-version>0.9.5</jsqlparser-version>
    </properties>
    
    <!--pagehelper-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>${pagehelper-version}</version>
    </dependency>
    
    <!-- pagehelper的依赖包:jsqlparser -->
    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>${jsqlparser-version}</version>
    </dependency>


    2.在 spring-mybatis.xml 中添加如下

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:mapping/**/*.xml"></property>
        <!--pageHelper-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            helperDialect=postgresql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    userDao

    ArrayList<Users> getUserList();

    mapping.xml

    <select id="getUserList"  resultType="com.hanpeng.entity.Users">
       select * from tb_users
    </select>



    userService

    public void test(){
       PageHelper.startPage(1, 5);
       ArrayList<Users> list = userDao.getUserList();
       PageInfo<Users> page = new PageInfo<Users>(list);
       System.out.println("总数量:" + page.getTotal());
       System.out.println("当前页查询记录:" + page.getList().size());
       System.out.println("当前页码:" + page.getPageNum());
       System.out.println("每页显示数量:" + page.getPageSize());
       System.out.println("总页:" + page.getPages());
    }

    controller

    @RequestMapping("pageHelperTest")
    public void getList(){
       userService.test();
    }


    REF

    https://blog.csdn.net/qq_39200980/article/details/104895549

    https://blog.csdn.net/qq_33609401/article/details/83749083

    https://blog.csdn.net/qq_39765090/article/details/87174950

  • 相关阅读:
    HackingLab-据说MD5加密很安全,真的是么
    HackingLab-猜猜这是经过了多少次加密
    MySQL8.0安装
    fastdfs 单节点部署多目录
    关于ssh密钥配置还需要输入密码的终极解决办法
    VMware 配置虚拟机NAT 方式上网
    redis bind的正确配置
    filebeat修改索引名字
    zabbix-server卡死不动
    ssh免秘钥快速配对。
  • 原文地址:https://www.cnblogs.com/emanlee/p/15733934.html
Copyright © 2020-2023  润新知