Invalid bound statement (not found): 不同原因

在项目整合过程中遇到MyBatis报Invalid bound statement (not found)错误,问题出在XML映射文件未被正确加载。尝试了多种解决方案,如确保namespace与Mapper方法匹配、XML id与方法名一致、资源配置、删除XML文件多余空格、处理mybatis-plus与mybatis配置冲突、理解resource目录结构等,最后发现是多数据源配置中MapperLocation设置不正确,需在具体数据源配置中设置setMapperLocations。
摘要由CSDN通过智能技术生成

 把多个项目合并到同一个项目,有个SQL写在Mapper.java里有的写在xml里。

运行时报错Invalid bound statement (not found)。读取不到xml中的SQL,但是Mapper中的SQL是可以运行的。就读取xml中的报错,很是费解。

试了很多很方法。例:classpath*:mapper/*.xml。

网上说的所有我都试了不管用:

1、namespace对应一致。

2、xml的id要和Mapper.java的方法名一致。

3、如果放在java目录下要配置pom文件里的resource,要不然maven打包不到。

4、把对应的xml文件删除行空格。(我觉得最灵异以及最可能成功的一个方法)。

5、mybatis-plus的配置会覆盖mybatis的配置。

6、resource目录创建文件夹的规则不同。以mapper.itm这种是不能分层的,读取是 是读取不到的。要一层一层创建文件夹或者mapper/item这种。

7、多数据源的MapperLocation,在application.yml(后缀properties这种都是一样的)配置文件中是不起作用的。要在数据源配置文件中设置setMapperLocations。这也是我遇到的问题。

org.springframework.core.io.Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml");
        bean.setMapperLocations(resources);


    &#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
多数据源Invalid bound statement not found是由于在集成mybatis-plus时,Mapper生成的代理类存在,但是调用BaseMapper中如selectById()方法报错所导致的。这个错误通常是由于mybatis-plus的MapperScannerConfigurer扫描到了多个Mapper接口,导致生成的代理类出现了冲突,从而无法正确绑定Mapper接口中的SQL语句。 解决这个问题的方法是在配置MapperScannerConfigurer时,指定不同的basePackage,以避免扫描到重复的Mapper接口。同时,还需要在配置文件中指定每个数据源对应的Mapper接口所在的位置,以确保mybatis-plus能够正确地生成代理类并绑定SQL语句。 以下是解决这个问题的步骤: 1.在配置文件中指定每个数据源对应的Mapper接口所在的位置,例如: ``` mybatis-plus: mapper-locations: - classpath*:mapper/*.xml configuration: map-underscore-to-camel-case: true global-config: db-config: id-type: auto type-aliases-package: com.example.demo.entity datasource: master: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver slave: url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath*:mapper/*.xml ``` 2.在配置MapperScannerConfigurer时,指定不同的basePackage,例如: ``` @Configuration @MapperScan(basePackages = "com.example.demo.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate") public class MasterDataSourceConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "datasource.master") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/master/*.xml")); return bean.getObject(); } @Bean(name = "masterTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "masterSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } @Configuration @MapperScan(basePackages = "com.example.demo.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate") public class SlaveDataSourceConfig { @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "datasource.slave") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/slave/*.xml")); return bean.getObject(); } @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "slaveSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3.在Mapper接口中使用@Mapper注解,例如: ``` @Mapper public interface UserMapper extends BaseMapper<User> { } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值