• 六、mybatis分页插件集成


    本文基于上一篇“集成mybatis”内容

    1、添加依赖

    <!-- mybatis-pageHelper -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>4.1.0</version>
            </dependency>

    2、修改配置

    package com.biniu.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.github.pagehelper.PageHelper;
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    
    import javax.sql.DataSource;
    import java.util.Properties;
    
    /**
     * @author lay
     * @date 2018/4/15.
     * @time 15:06
     */
    @Configuration
    @MapperScan("com.biniu.dao")
    public class MyBatisConfig {
        @Bean
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            // 数据源
            bean.setDataSource(dataSource());
            // sql文件
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
            // 分页插件
            PageHelper pageHelper = new PageHelper();
            Properties properties = new Properties();
            // 禁用分页参数合理化
            properties.setProperty("reasonable", "false");
            properties.setProperty("supportMethodsArguments", "true");
            properties.setProperty("returnPageInfo", "check");
            properties.setProperty("params", "count=countSql");
            properties.setProperty("helperDialect", "mysql");
            pageHelper.setProperties(properties);
            bean.setPlugins(new Interceptor[]{pageHelper});
            return bean.getObject();
        }
    
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource() {
            return new DruidDataSource();
        }
    }

    3、使用示例

    public Object listUser() {
            PageHelper.startPage(1, 10);
            List<Object> users = userDao.listUser();
            PageInfo<Object> pageInfo = new PageInfo<>(users);
            return pageInfo;
        }
  • 相关阅读:
    Codeforces Round #622 (Div. 2)
    Knapsack Cryptosystem 牛客团队赛
    HDU 2586(LCA欧拉序和st表)
    P3865 【模板】ST表
    P2023 [AHOI2009]维护序列 区间加乘模板
    P1558 色板游戏 线段树(区间修改,区间查询)
    Codeforces Round #621 (Div. 1 + Div. 2) D
    Codeforces Round #620 (Div. 2) E
    Educational Codeforces Round 82 (Rated for Div. 2)
    洛谷P1638 逛画展
  • 原文地址:https://www.cnblogs.com/lay2017/p/8847920.html
Copyright © 2020-2023  润新知