com.github.pagehelper.PageInterceptor Java Examples

The following examples show how to use com.github.pagehelper.PageInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterDataSourceConfig.java    From springBoot-study with Apache License 2.0 6 votes vote down vote up
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
        throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(clusterDataSource);
    //分页插件
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");
    interceptor.setProperties(properties);
    sessionFactory.setPlugins(new Interceptor[] {interceptor});
    
    
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
    return sessionFactory.getObject();
}
 
Example #2
Source File: MybatisConfigurer.java    From spring-boot-api-project-seed with Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    PackagesSqlSessionFactoryBean sqlSessionFactory = new PackagesSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    sqlSessionFactory.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);

    //配置分页插件,详情请查阅官方文档
    PageInterceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //分页尺寸为0时查询所有纪录不再执行分页
    properties.setProperty("pageSizeZero", "true");
    //页码<=0 查询第一页,页码>=总页数查询最后一页
    properties.setProperty("reasonable", "true");
    //支持通过 Mapper 接口参数来传递分页参数
    properties.setProperty("supportMethodsArguments", "true");
    interceptor.setProperties(properties);

    //添加插件
    //为了防止插件被重复注册,可以在启动类中使用"@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)"排除默认的配置
    sqlSessionFactory.setPlugins(new Interceptor[]{interceptor});

    //添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
    return sqlSessionFactory.getObject();
}
 
Example #3
Source File: OrmConfig.java    From v-mock with MIT License 5 votes vote down vote up
/**
 * 适配pagehelper代码
 */
@Bean
public PageInterceptor paginationInterceptor() {
    PageInterceptor pageInterceptor = new PageInterceptor();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("helperDialect", DbType.SQLITE.getDb());
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("params", "count=countSql");
    pageInterceptor.setProperties(properties);
    return pageInterceptor;
}
 
Example #4
Source File: DataSourceConfig.java    From momo-cloud-permission with Apache License 2.0 5 votes vote down vote up
private PageInterceptor pageInterceptor() {
    //分页插件
    PageInterceptor pageHelper = new PageInterceptor();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "false");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    properties.setProperty("autoRuntimeDialect", "true");
    pageHelper.setProperties(properties);
    return pageHelper;
}
 
Example #5
Source File: MasterDataSourceConfig.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
        throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(masterDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
    //分页插件
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");
    interceptor.setProperties(properties);
    sessionFactory.setPlugins(new Interceptor[] {interceptor});
    
    return sessionFactory.getObject();
}
 
Example #6
Source File: PageHelperAutoConfiguration.java    From pagehelper-spring-boot with MIT License 5 votes vote down vote up
@PostConstruct
public void addPageInterceptor() {
    PageInterceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //先把一般方式配置的属性放进去
    properties.putAll(pageHelperProperties());
    //在把特殊配置放进去,由于close-conn 利用上面方式时,属性名就是 close-conn 而不是 closeConn,所以需要额外的一步
    properties.putAll(this.properties.getProperties());
    interceptor.setProperties(properties);
    for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
        sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
    }
}
 
Example #7
Source File: PageHelperAutoConfiguration.java    From loc-framework with MIT License 5 votes vote down vote up
@PostConstruct
public void addPageInterceptor() {
  PageInterceptor interceptor = new PageInterceptor();
  Properties properties = pageHelperProperties.getProperties();
  interceptor.setProperties(properties);
  Optional.ofNullable(sqlSessionFactoryList).ifPresent(list -> list.forEach(
      sqlSessionFactory -> sqlSessionFactory.getConfiguration().addInterceptor(interceptor)));
}
 
Example #8
Source File: PageHelperAutoConfiguration.java    From pagehelper-spring-boot with MIT License 5 votes vote down vote up
@PostConstruct
public void addPageInterceptor() {
    PageInterceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //先把一般方式配置的属性放进去
    properties.putAll(pageHelperProperties());
    //在把特殊配置放进去,由于close-conn 利用上面方式时,属性名就是 close-conn 而不是 closeConn,所以需要额外的一步
    properties.putAll(this.properties.getProperties());
    interceptor.setProperties(properties);
    for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
        sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
    }
}