spring boot 配置多数据源 踩坑 BindingException: Invalid bound statement (not found)

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2023-09-22 14:32
  • 1949人已阅读
简介 在上一篇:《【已解决】SpringBoot多数据源的时候,mybatis报错提示:Invalidboundstatement(notfound)》凯哥(公众号:凯哥Java) 已经接受了,在SpringBoot配置多数据源时候,因为自己马虎,导致的一个坑。下面,凯哥在介绍配置多数据源时候的坑:模仿另一个项目的配置,但第二个数据源一直报异常:2022-05-25 16:18:24

🔔🔔🔔好消息!好消息!🔔🔔🔔

有需要的朋友👉:联系凯哥 微信号 kaigejava2022

在上一篇:《【已解决】Spring Boot多数据源的时候,mybatis报错提示:Invalid bound statement (not found)》 凯哥(公众号:凯哥Java) 已经接受了,在Spring Boot配置多数据源时候,因为自己马虎,导致的一个坑。下面,凯哥在介绍配置多数据源时候的坑:

模仿另一个项目的配置,但第二个数据源一直报异常:

2022-05-25 16:18:24.054-[http-nio-9001-exec-1]-[ ERROR ] [ c.y.trade.admin.exception.GlobalExceptionHandler : 52 ] - 未处理的异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.kaigejava.trade.admin.dao.tsd.TsdSysEnumDicDao.queryAll
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:107)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:94)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
    at com.sun.proxy.$Proxy133.queryAll(Unknown Source)
    at com.kaigejava.trade.admin.controller.TestController.getEnums(TestController.java:39)
    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:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.yuecheng.trade.admin.common.interceptor.HttpServletRequestWrapperFilter.doFilter(HttpServletRequestWrapperFilter.java:48)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
2022-05-25 16:18:24.102-[http-nio-9001-exec-1]-[ WARN  ] [ o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver : 198 ] - Resolved [org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yuecheng.trade.admin.dao.tsd.TsdSysEnumDicDao.queryAll]

查资料后,始终认为是配置的mapper.xml和dao的问题。但无论怎么改,都不行。

最后发现之前那个项目,第二个数据源对应的dao,都没有加@Mapper注解,遂去掉后即正常了。(可能和我将第一个数据源配置成Primary有关系?)

暂不清楚具体原因,仅此记录。

------------- 后记 ------------- 

经测试,果然是因为@Primary注解引起的。引起问题注解的代码如下:

@Configuration

@MapperScan(basePackages = "com.kaigejava.trade.admin.dao", sqlSessionFactoryRef = "defaultSqlSessionFactory")

public class TradeSystemConfig {

    /**

     * Bean 将这个对象放入Spring容器中

     * Primary 表示这个数据源是默认数据源

     * ConfigurationProperties 读取application.properties中的配置参数映射成为一个对象

     * prefix 表示参数的前缀

     *

     * @return {@link DataSource}

     */

    @Bean(name = "defaultDataSource")

    @Primary

    @ConfigurationProperties(prefix = "spring.datasource.tts")

    public DataSource getDateSourceTts() {

        return DataSourceBuilder.create().build();

    }


    /**

     * 表示这个数据源是默认数据源

     * Qualifier 表示查找Spring容器中名字为defaultDataSource的对象

     *

     * @param datasource {@link DataSource}

     * @return @{link SqlSessionFactory}

     * @throws Exception ex

     */

    @Bean(name = "defaultSqlSessionFactory")

    @Primary

    public SqlSessionFactory defaultSqlSessionFactory(@Qualifier("defaultDataSource") DataSource datasource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();

        configuration.setMapUnderscoreToCamelCase(true);

        bean.setConfiguration(configuration);

        bean.setDataSource(datasource);

        // 设置mybatis的xml所在位置

        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));

        return bean.getObject();

    }


    /**

     * 表示这个数据源是默认数据源

     *

     * @param sessionFactory {@link SqlSessionFactory}

     * @return {@link SqlSessionTemplate}

     */

    @Bean("defaultSqlSessionTemplate")

    @Primary

    public SqlSessionTemplate defaultSqlSessionTemplate(

            @Qualifier("defaultSqlSessionFactory") SqlSessionFactory sessionFactory) {

        return new SqlSessionTemplate(sessionFactory);

    }

}

可以去掉这个注解,然后Dao便可以正常写上@Mapper注解了。


来源:https://www.cnblogs.com/Denny_Yang/p/16309822.html

TopTop