这两天做SpringBoot整合Mybatis项目的时候,插入时报错:3:45:59.936 DEBUG [http-nio-8080-exec-8] o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver 133 - Resolving exception from handler [public java.lang.String com.yooli.sale.html.controller.reduce.ReduceController.saveApply()]: java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yooli.sale.dao.reduce.ReduceMapper.insert org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yooli.sale.dao.reduce.ReduceMapper.insert at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) at com.sun.proxy.$Proxy94.insert(Unknown Source) 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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy96.insert(Unknown Source) at com.yooli.sale.service.impl.reduce.ReduceServiceImpl.saveReduceApply(ReduceServiceImpl.java:30) at com.yooli.sale.service.impl.reduce.ReduceServiceImpl$$FastClassBySpringCGLIB$$a3f22b1a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at com.yooli.sale.service.impl.reduce.ReduceServiceImpl$$EnhancerBySpringCGLIB$$e92e34fb.saveReduceApply(<generated>) at com.yooli.sale.impl.reduce.ReduceDemoFacadeServiceImpl.saveReduceApply(ReduceDemoFacadeServiceImpl.java:28) at com.alibaba.dubbo.common.bytecode.Wrapper1.invokeMethod(Wrapper1.java) at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:45) at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:71) at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:48) at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:52) at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:61) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:74) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:41) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:71) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:131) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:37) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:37) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68) at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:98) at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96) at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:168) at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:50) at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:79) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
系统结构:
SpringBoot启动:
@SpringBootApplication(scanBasePackages = "com.example.demo") //@SpringBootApplication(scanBasePackages = "com.example.demo", exclude = DataSourceAutoConfiguration.class) @EnableConfigurationProperties @EnableScheduling @EnableAsync public class ProvideApplication { public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = SpringApplication.run(ProvideApplication.class, args); } }
Mapper的应用DataSource:
`@Configuration@MapperScan(basePackages = "com.example.demo.dao.reduce", sqlSessionTemplateRef = "saleSqlSessionTemplate")public class SaleDataSource {@Bean(name = "autosaleData")@ConfigurationProperties(prefix = "sale.datasource") // application.properteis中对应属性的前缀public DataSource sentinelData() {return DataSourceBuilder.create().build();}
@Bean(name = "dqSqlSessionFactory")
public SqlSessionFactory sentinelSqlSessionFactory(@Qualifier("autosaleData") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
@Bean(name = "dqTransactionManager")
public DataSourceTransactionManager sentinelTransactionManager(@Qualifier("autosaleData") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "saleSqlSessionTemplate")
public SqlSessionTemplate sentinelSqlSessionTemplate(@Qualifier("dqSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
`
Mapper接口层:@Mapper @Repository public interface ReduceMapper { int insert(YlReduceApply record); }
Mapper.xml内容:<insert id="insert" parameterType="com.exmaple.demo.entity.YlReduceApply" useGeneratedKeys="true" keyProperty="id"> insert into YL_DEMO_INFO <trim prefix="(" suffix=")" suffixOverrides="," > <if test="loanId != null" > LOAN_ID, </if> CREATE_TIME, UPDATE_TIME </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="loanId != null" > #{loanId,jdbcType=VARCHAR}, </if> current_time(), current_time() </trim>
后经过排查,发现是在引入DataSource的时候,对Mapper.xml的扫描配置路径需要调整:改为:@Bean(name = "dqSqlSessionFactory") public SqlSessionFactory sentinelSqlSessionFactory(@Qualifier("autosaleData") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*/*.xml")); return bean.getObject(); }
问题解决!