Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
Taogang00 opened this issue Jan 15, 2024 · 2 comments
Open
1 task

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

Taogang00 opened this issue Jan 15, 2024 · 2 comments

Comments

@Taogang00
Copy link

  • 我已在 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
Copy link
Author

是这里说明的吧
image

@abel533
Copy link
Collaborator

abel533 commented Jan 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants