2021-08-30

springboot + mybatis plus + hikari 分包实现多数据源 + 处理 Invalid bound statement

项目结构

三个数据源配置文件,三个mapper文件夹,三个mapper.xml文件夹

数据源配置

注意是 jdbc_url 不是 url

主数据源配置文件

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 javax.sql.DataSource;

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.hzlq.iot.mapper.zhltwlzx", sqlSessionTemplateRef = "zhltwlzxSqlSessionTemplate")
public class DataSourceConfigZhltwlzx {
    // 将这个对象放入Spring容器中
    @Bean
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    @ConfigurationProperties(prefix = "spring.datasource.zhltwlzx")
    public DataSource getDateSourceZhltwlzx() {
        return DataSourceBuilder.create().build();
    }


    @Bean
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory zhltwlzxSqlSessionFactory(@Qualifier("getDateSourceZhltwlzx") DataSource datasource)
            throws Exception {
            //重点:是 MybatisSqlSessionFactoryBean 不是 SqlSessionFactoryBean 
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/zhltwlzx/*.xml"));
        return bean.getObject();
    }


    @Bean
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate zhltwlzxSqlSessionTemplate(
            @Qualifier("zhltwlzxSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

其他数据源配置文件

去掉@Primary,里外改个名就行了

测试代码

在这里插入图片描述
service - serviceImpl - mapper - mapper.xml关系如上,均为自动生成,只有 serviceImpl 添加了一个@Service标签

import com.hzlq.iot.mapper.zhltwlzx.TbIotMapper;
import com.hzlq.iot.model.DO.TbIot;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class IotApplicationTests {
    @Resource
    TbIotMapper tbIotMapper;

    @Test
    void contextLoads() {
		//调用mybatis plus定义好的 selectList 方法
        List<TbIot> list = tbIotMapper.selectList(null);
        for (TbIot tbIot : list) {
            System.out.println(tbIot);
        }
    }

}

运行结果

在这里插入图片描述
在这里插入图片描述

我的纠错过程

1.bean 的名称与累的名称不对应
在这里插入图片描述
2.是 MybatisSqlSessionFactoryBean 不是 SqlSessionFactoryBean
在这里插入图片描述
3.是 jdbc_url 不是 url
在这里插入图片描述
若有其他原因可以参照单数据源先排错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值