SpringBoot+mybatis-plus配置多数据源报错Invalid bound statement (not found)解决

项目场景:

SpringBoot+ mybatisplus 配置多数据源
application.properties配置

spring.datasource.algorithm.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.algorithm.jdbc-url=jdbc:mysql://127.0.0.1:3306/algorithm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.algorithm.username=root
spring.datasource.algorithm.password=root1

spring.datasource.event-center.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.event-center.jdbc-url=jdbc:mysql://127.0.0.1:3306/event_center?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.event-center.username=root
spring.datasource.event-center.password=roo1

注:多数据源配置情况下,datasource的url需要更改成jdbc-url

数据源1配置:

@Configuration
@MapperScan(basePackages = "com.iwhale.dal.mapper.algorithm",
        sqlSessionFactoryRef = "algorithmSessionFactory",
        sqlSessionTemplateRef = "algorithmSqlSessionTemplate")
public class AlgorithmDataSourceConfig {

    @Primary
    @Bean(name = "algorithmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.algorithm")
    public DataSource algorithmDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "algorithmTransactionManager")
    public DataSourceTransactionManager algorithmTransactionManager(@Qualifier("algorithmDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean("algorithmSessionFactory")
    public SqlSessionFactory algorithmSessionFactory(@Qualifier("algorithmDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/algorithm/*.xml"));
        return bean.getObject();
    }

    @Primary
    @Bean("algorithmSqlSessionTemplate")
    public SqlSessionTemplate algorithmSqlSessionTemplate(@Qualifier("algorithmSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

数据源2配置:

@Configuration
@MapperScan(basePackages = "com.iwhale.dal.mapper.eventCenter",
        sqlSessionFactoryRef = "eventCenterSessionFactory",
        sqlSessionTemplateRef = "eventCenterSqlSessionTemplate")
public class EventCenterDataSourceConfig {

    @Bean(name = "eventCenterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.event-center")
    public DataSource eventCenterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "eventCenterTransactionManager")
    public DataSourceTransactionManager eventCenterTransactionManager(@Qualifier("eventCenterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("eventCenterSessionFactory")
    public SqlSessionFactory eventCenterSessionFactory(@Qualifier("eventCenterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/eventCenter/*.xml"));
        return bean.getObject();
    }

    @Bean("eventCenterSqlSessionTemplate")
    public SqlSessionTemplate eventCenterSqlSessionTemplate(@Qualifier("eventCenterSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

问题描述

调用接口是控制台报错:Invalid bound statement (not found)


原因分析:

在创建SqlSessionFactory时,使用的是SqlSessionFactoryBean


解决方案:

SqlSessionFactory时,将SqlSessionFactoryBean换成MybatisSqlSessionFactoryBean。
即:
使用mybatis时,用SqlSessionFactoryBean;
使用mybatis-plus时,使用MybatisSqlSessionFactoryBean

参考链接:https://blog.csdn.net/lizhao1226/article/details/129066606

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
配置多数据源: 1. 在pom.xml中添加mybatis-plus和druid依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 在application.yml中添加数据源配置: ```yaml spring: datasource: druid: one: url: jdbc:mysql://localhost:3306/one?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver two: url: jdbc:mysql://localhost:3306/two?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #默认数据源 primary: one ``` 3. 在代码中使用@DS注解切换数据源: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @DS("one") @Override public User getUserById(Long id) { return userMapper.selectById(id); } @DS("two") @Override public User getUserByName(String name) { return userMapper.selectOne(new QueryWrapper<User>().eq("name", name)); } } ``` 配置分页插件: 1. 在pom.xml中添加分页插件依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在配置类中配置分页插件: ```java @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } } ``` 3. 在controller层中使用分页: ```java @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { Page<User> page = new Page<>(pageNum, pageSize); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("status", 1); return userService.page(page, queryWrapper); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值