当前位置:网站首页>多数据源配置下,解决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文件目录结构:

边栏推荐
- Wechat applet todo case
- 天然气潮流计算matlab程序
- 【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组
- Rasa 3.x learning series -rasa fallbackclassifier source code learning notes
- 记不住正则表达式?这里我整理了99个常用正则
- The solution to the error of [installation detects that the primary IP address of the system is the address assigned by DHCP] when installing Oracle10g under win7
- Dameng real-time active and standby cluster construction
- After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
- Meaning of 7 parameters of thread pool
- Not configured in app.json (uni releases wechat applet)
猜你喜欢

Centos7安装达梦单机数据库

Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%

天然气潮流计算matlab程序

Simple understanding and implementation of unity delegate

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测

Dameng real-time active and standby cluster construction

mysql

The server switches between different CONDA environments and views various user processes
![[oauth2] III. interpretation of oauth2 configuration](/img/31/90c79dbc91ee15c353ec46544c8efa.png)
[oauth2] III. interpretation of oauth2 configuration

Class loading mechanism and parental delegation mechanism
随机推荐
C multithreaded lock collation record
字符串——459. 重复的子字符串
Complete set of JS array common operations
The sliding window of Li Kou "step by step" (209. The smallest sub array, 904. Fruit baskets)
2022.7.22 simulation match
DDD based on ABP -- Entity creation and update
Similarities and differences between nor flash and NAND flash
Grpc middleware implements grpc call retry
本机异步网络通信执行快于同步指令
Detailed explanation of switch link aggregation [Huawei ENSP]
Wechat applet todo case
Class loading mechanism and parental delegation mechanism
String - 459. Repeated substrings
茅台冰淇淋“逆势”走红,跨界之意却并不在“卖雪糕”
Nessus security testing tool tutorial
Stack and queue -- 232. Implement queue with stack
[oauth2] II. Authorization method of oauth2
Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布
Automated penetration scanning tool
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]