使用mybatis plus出现分页无效、自定义的sql查询报错问题

2年前 (2022) 程序员胖胖胖虎阿
249 0 0

最近使用mybatis plus,出现几个小问题,在这里记录一下:

1.自定义的sql查询方法无法正常执行的问题

2022-01-13 20:58:22.582 ERROR 64652 --- [nio-8080-exec-1] c.c.s.w.s.a.AbstractRestExceptionHandler : handle [500] exception with error key[forbidden].

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 
at com.xx.xx.xx.web.springboot.advice.AbstractRestExceptionHandler.handle(AbstractRestExceptionHandler.java:79)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:428)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:75)
	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141)
	at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80)
	at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1320)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1131)
...
...
...
Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:101)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1688)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:100)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:95)
	at com.sun.proxy.$Proxy133.getTenantGroupAttrConfigList(Unknown Source)
...
...
...

以上就是主要的报错信息,最后发现问题是因为application.xml中mybatis plus的配置写错了:

mybatis:
  basepackage: com.xx.xx.xx.dao
  # 多个路径用逗号或者分号分隔
  mapper-locations: classpath*:mybatis/**/*.xml

因为我是从mybatis升级到plus的,所以这边配置最初是mybatis,最后发现他俩的前缀名称不一样的:
使用mybatis plus出现分页无效、自定义的sql查询报错问题

需要改成mybatis-plus:

mybatis-plus:
  basepackage: com.icss.cig.spms.standard.organization.dao
  # 多个路径用逗号或者分号分隔
  mapper-locations: classpath*:mybatis/**/*.xml

然后就OK了,正常了!!

2.mybatis自带的分页方法不生效的问题

下面是分页查询列表的方法,代码生成器自动生成的:

	@Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TenantGroupAttr> page = this.page(
                new Query<TenantGroupAttr>().getPage(params),
                new QueryWrapper<TenantGroupAttr>()
        );

        return new PageUtils(page);
    }

分页的入参和出参:
使用mybatis plus出现分页无效、自定义的sql查询报错问题

实际上有7条数据,但是没有分页成功,而且执行的sql尾部并没有拼接上 “LIMIT”分页语句。

该问题是因为缺少配置类,需加上这个类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor getPaginationInterceptor() {
        return new PaginationInterceptor();
    }

}

然后问题解决:
使用mybatis plus出现分页无效、自定义的sql查询报错问题

3.使用mybatis plus,控制台不打印sql日志,需要加个配置:

mybatis-plus:
  #控制台打印sql
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

相关文章

暂无评论

暂无评论...