当前位置: 代码迷 >> 综合 >> springcloud+seata+mybatis出现org.apache.ibatis.binding.BindingException: Invalid bound statement not
  详细解决方案

springcloud+seata+mybatis出现org.apache.ibatis.binding.BindingException: Invalid bound statement not

热度:25   发布时间:2023-12-17 21:40:22.0

文章目录

  • 问题
    • 连接池配置
    • 程序入口配置
    • 完整配置

问题

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): XXX.XXX.XXX.XXXXXXXXXXXXX
在使用springcloud+seata+mybatis集成的时候出现了BindingException,起初以为是因为maven多模块之间依赖的问题,在pom文件添加了如下配置

	<build><resources><!-- maven项目中src源代码下的xml等资源文件编译进classes文件夹,注意:如果没有这个,它会自动搜索resources下是否有mapper.xml文件,如果没有就会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): XXX.XXX.XXX.XXXXXXXXXXXXX--><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><!--将resources目录下的配置文件编译进classes文件 --><resource><directory>src/main/resources</directory></resource></resources></build>

发现没有用!!!

这就有点烦,后来通过查阅官方整合案例,发现需要在配置连接池代理的时候指定一下需要加载的resources mapper

连接池配置

代码如下

	...@Beanpublic SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSourceProxy);factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml"));factoryBean.setTransactionFactory(new SpringManagedTransactionFactory());return factoryBean.getObject();}...

程序入口配置

在程序入口中的@SpringBootApplication注解添加:exclude = DataSourceAutoConfiguration.class 属性

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(scanBasePackages = "io.seata.samples", exclude = DataSourceAutoConfiguration.class)
public class SpringbootMybatisOrderApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringbootMybatisOrderApplication.class, args);}}

完整配置

请结合自己的项目情况,主要配置还是指定一下资源路径

添加配置后完美解决,记一笔

连接池完整配置如下

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration
public class DataSourceConfig {
    @Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource() {
    DruidDataSource druidDataSource = new DruidDataSource();return druidDataSource;}@Primary@Bean("dataSource")public DataSourceProxy dataSource(DataSource druidDataSource) {
    return new DataSourceProxy(druidDataSource);}@Beanpublic SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSourceProxy);factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml"));factoryBean.setTransactionFactory(new SpringManagedTransactionFactory());return factoryBean.getObject();}
}

官方案例地址:https://github.com/seata/seata-samples/tree/master/springboot-mybatis

  相关解决方案