乐趣区

关于java:springboot整合mybatis拦截器分页

pom.xml

    <!-- 引入分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
     <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>

yml
spring:
application:

name: spring-cloud-dynamic

datasource:

# 类型
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/f2f?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 
initial-size: 2
max-idle: 10
min-idle: 1
max-wait: 60000
max-active: 20 #最大闲暇连接数
#多久进行一次检测,检测须要敞开的闲暇连贯
time-between-eviction-tuns-millis: 60000

MybatisConfig
/**

  • @author lanys
  • @Description:
  • @date 23/7/2021 下午 8:38
    */

@Configuration
@EnableTransactionManagement
@PropertySource(value = “classpath:application.yml”, ignoreResourceNotFound = true)
public class MybatisConfig implements TransactionManagementConfigurer {

@Value("${mybatis.mapper-locations}")
private String mapper;
@Value("${mybatis.type-aliases-package}")
private String aliases;
@Autowired
private DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    // 设置数据源
    bean.setDataSource(dataSource);
    // 设置 xml
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper));
    // 设置别名
    bean.setTypeAliasesPackage(aliases);
    // 增加分页插件
    bean.setPlugins(new Interceptor[]{pageInterceptor()});
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();}
/**
 * 分页拦截器
 * @return
 */
private PageInterceptor pageInterceptor() {PageInterceptor pageInterceptor = new PageInterceptor();
    // 详见 com.github.pagehelper.page.PageParams
    Properties p = new Properties();
    // RowBounds 是否进行 count 查问 [利率期货](https://www.gendan5.com/ff/if.html) - 默认不查问
    p.setProperty("rowBoundsWithCount", "true");
    // 当设置为 true 的时候,如果 page size 设置为 0(或 RowBounds 的 limit=0),就不执行分页,返回全副后果
    p.setProperty("pageSizeZero", "true");
    // 分页合理化
    p.setProperty("reasonable", "false");
    // 是否反对接口参数来传递分页参数,默认 false
    p.setProperty("supportMethodsArguments", "true");
    // 设置数据库方言,也能够不设置,会动静获取
    p.setProperty("helperDialect", "mysql");
    pageInterceptor.setProperties(p);
    return pageInterceptor;
}
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {return new DataSourceTransactionManager(dataSource);
}

}

退出移动版