Mybatis-PageHelper icon indicating copy to clipboard operation
Mybatis-PageHelper copied to clipboard

开启asyncCount 时,打印的sql语句不全

Open Taogang00 opened this issue 2 years ago • 2 comments

  • [ ] 我已在 issues 搜索类似问题,并且不存在相同的问题.

异常模板

使用环境

  • PageHelper 版本: 6.0.0
  • 数据库类型和版本: h2
  • JDBC_URL: xxx

问题描述

我们有一个自己的sql 打印拦截器,当没有开启asyncCount 时,能正常的打印select count() 和select * 两个语句,但是当我打开asyncCount 时,就只打印了select * ,没有打印select count(*)

JavaPeizhi

    @Bean
    @ConditionalOnMissingBean
    public PageInterceptor pageInterceptor() {
        Properties properties = new Properties();
        properties.setProperty("asyncCount", "true");
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }
@RequiredArgsConstructor
@Intercepts(value = {
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MybatisPrintSqlInterceptor implements Interceptor {

    private MybatisProperties mybatisProperties;

    public MybatisPrintSqlInterceptor(MybatisProperties mybatisProperties) {
        this.mybatisProperties = mybatisProperties;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];

        boolean contains = mybatisProperties.getPrintSqlCommandTypes().contains(ms.getSqlCommandType());
        if (contains) {
            BoundSql boundSql;
            if (args.length == 2 || args.length == 4) {
                boundSql = ms.getBoundSql(args[1]);
            } else {
                boundSql = (BoundSql) args[5];
            }

            //1.打印将要执行的sql
            String name = Thread.currentThread().getName();
            String sql = getSqlWithValues(boundSql.getSql(), buildParameterValues(ms.getConfiguration(), boundSql));
        //......................
        Page<UserRole> objectPage = PageHelper.startPage(1, 3);

        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(UserRole::getUserId, "test");
        List<UserRole> list = userRoleService.list(lambdaQueryWrapper);

        Assertions.assertEquals(list.size(), 3);
        System.out.println(objectPage);
        Assertions.assertEquals(objectPage.getTotal(), COUNT);

期望的结果:

打印select count() 语句

实际结果:

1.[main]执行的SQL: SELECT roleId,userId FROM UserRole WHERE (userId LIKE '%test%') LIMIT 3
2.[main]执行的Statement: com.guanwei.mybatis.mapper.UserRoleMapper.selectList 3.[main]执行用时: 29 毫秒 Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=20, pages=7, reasonable=false, pageSizeZero=true}[UserRole(roleId=111, userId=test111), UserRole(roleId=1110, userId=test1110), UserRole(roleId=1221, userId=test1221)]

实际看出来,total 有值,但是没有打印查询数量的sql,这需要怎么配置

Taogang00 avatar Jan 15 '24 13:01 Taogang00

是这里说明的吧 image

Taogang00 avatar Jan 16 '24 00:01 Taogang00

是这里说明的原因,可以尝试能否避免递归并且支持其他拦截器。

abel533 avatar Jan 16 '24 05:01 abel533