1. 程式人生 > >spring-boot 整合mybatis的分頁外掛PageHelper版本問題

spring-boot 整合mybatis的分頁外掛PageHelper版本問題

Pagehelper 5.0.0及以上版本。使用的是這個類Pagehelper,我們在專案中可以這樣寫:
PageInterceptor pageHelper = new PageInterceptor();
properties.setProperty(“helperDialect”, “mysql”);

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.springboot.demo");
        // 分頁外掛
        PageInterceptor pageHelper = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        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});
        // 新增XML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {      bean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
            // 駝峰匹配          bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

Pagehelper 5.0.0以下版本。使用的是這個類Pagehelper,我們在專案中可以這樣寫:
PageHelper pageHelper = new PageHelper();
properties.setProperty(“dialect”, “mysql”);

public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.springboot.demo");
        // 分頁外掛
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        properties.setProperty("dialect", "mysql");
        pageHelper.setProperties(properties);
        // 新增外掛
        bean.setPlugins(new Interceptor[] { pageHelper });
        // 新增XML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
            // 駝峰匹配
            bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }