当前位置:网站首页>多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
2022-07-24 14:17:00 【柴神】
1、检查mybatis.xml文件namespace名称是否和Mapper接口的全限定名是否一致
2、检查Mapper接口的方法在mybatis.xml中的每个语句的id是否一致
3、检查Mapper接口方法返回值是否匹配select元素配置的ResultMap,或者只配置ResultType
4、检查yml文件中的mapper的XML配置路径是否正确
5、Mybatis中接口与映射文件一定要同名或者必须在同一个包下,这个我没试过,好像是可以不同名的。
6、配置数据源的SqlSessionFactoryBean要使用MyBatisSqlSessionFactoryBean,这个也是鬼扯,MybatisPlus和Mybatis分清楚再说

7、编译没有把XML拷贝过来,可以用这招:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
8、 启动会默认通过org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration * 类,我们是自定义的,所以需要排除MybatisAutoConfiguration
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)9、Mapper接口文件,不同数据源需要放置在不同包下面。
可能的原因都在这里了,各位慢用!!!
附上SqlSessionFactoryBean代码
ds1.java:
package com.****.****.Configurer;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.***.***.Mapper.ds1"}, sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class MybatisDS1Config {
@Bean(name = "ds1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
/**
* 配置事务管理器,不然事务不起作用
*
* @return
*/
@Bean
public PlatformTransactionManager transactionManagerDS1() {
return new DataSourceTransactionManager(this.dataSource());
}
@Primary
@Bean(name = "ds1SqlSessionFactory")
public SqlSessionFactoryBean ds1sqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapping/ds1/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("com.jmop.happinesswhisper.Entity");
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean;
/**
* 这里在applications.properties里面配置了
* mybatis.type-aliases-package=com.jwt.springboot.dao
* mybatis.mapper-locations=classpath:mapper/*Mapper.xml
* 但多数据源情况下执行sql总会报:org.apache.ibatis.binding.BindingException:
* Invalid bound statement (not found)........
* 原因是 this.mapperLocations 为null
*
* 注!!!!这里有大坑, 因为这里是自定义的sqlSessionFactoryBean,所以导致
* 没有启动时没有通过org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
* 类的sqlSessionFactory(DataSource dataSource)方法自动装配sqlSessionFactoryBean
* 自定义的sqlSessionFactoryBean所以也没设置mapperLocations
* 故自定义实例化sqlSessionFactoryBean这里需要手动设置mapperLocations
* 可参考:https://developer.aliyun.com/article/754124
*/
}
}
ds2,java
package com.***.***.Configurer;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.***.***.Mapper.ds2", sqlSessionFactoryRef = "ds2SqlSessionFactory")
public class MybatisDS2Config {
@Bean(name = "ds2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
/**
* 配置事务管理器,不然事务不起作用
*
* @return
*/
@Bean
public PlatformTransactionManager transactionManagerDS2() {
return new DataSourceTransactionManager(this.dataSource());
}
@Bean("ds2SqlSessionFactory")
public SqlSessionFactory ds2sqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapping/ds2/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("com.jmop.happinesswhisper.Entity");
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}
}
yml配置:
spring:
datasource:
ds1: #主数据库,生产数据库
username: ***
password: ***
#url中database为对应的数据库名称 //数据库名字
jdbc-url: jdbc:mysql://***:3306/crmdb?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
ds2: #从数据库,分析数据库
username: ***
password: ***
#url中database为对应的数据库名称 //数据库名字
jdbc-url: jdbc:mysql://***:3306/jmsns?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver文件目录结构:

边栏推荐
- sql server语法—创建数据库
- Introduction to Xiaoxiong school
- 栈与队列——225. 用队列实现栈
- 【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组
- 达梦实时主备集群搭建
- JS judge whether the data is empty
- JS get object attribute value
- OC sets the image fillet, and the image is not deformed
- Detailed explanation of address bus, data bus and control bus
- Mini examination - examination system
猜你喜欢

看完这篇文章,才发现我的测试用例写的就是垃圾

5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字

2022 IAA industry category development insight series report - phase II

Notes on the use of IEEE transaction journal template

Multithreaded common classes

茅台冰淇淋“逆势”走红,跨界之意却并不在“卖雪糕”

Mini examination - examination system

使用树莓派做Apache2 HA实验

After reading this article, I found that my test cases were written in garbage

Centos7安装达梦单机数据库
随机推荐
看完这篇文章,才发现我的测试用例写的就是垃圾
SQL Server syntax - create database
Default color setting in uiswitch off state
C language -- three ways to realize student information management
[oauth2] III. interpretation of oauth2 configuration
CAS atomic type
Similarities and differences between nor flash and NAND flash
Can't remember regular expressions? Here I have sorted out 99 common rules
Regular expression and bypass cases
Detailed analysis of common command modules of ansible service
Flink comprehensive case (IX)
天然气潮流计算matlab程序
Must use destructuring props assignmenteslint
Attributeerror: module 'distutils' has no attribute' version error resolution
Detailed explanation of switch link aggregation [Huawei ENSP]
Notes on the use of IEEE transaction journal template
Lazy loading of pictures
Remove the treasure box app with the green logo that cannot be deleted from iPhone
The difference and relation among list, set and map
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog