【Spring】SpringBoot 配置MySql和Doris多数据源

        嗨, 大家好, 最近公司想要使用DorisDB做数据存储, 经过几天对Doris的研究,发现Dorsi连接方式其实用的就是JDBC连接方式。今天把这两天的学习成果记录一下。 也希望对看到此文章的人有所帮助。对于文章中的问题, 也希望大神给出指点。具体doris的安装配置这里就不细说了, 官网都有相关资料。

        SpringBoot配置Mysql和Doris数据源, 其实跟配置Mysql多个数据源是一个样的。

1.maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mo.xue</groupId>
    <artifactId>doris-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>doris-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.项目结构

 

3. application.yml配置

server:
  port: 8080

#数据库连接配置
spring:
  datasource:
    mysql: # mysql配置
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: 数据库地址, 这里会根据版本不同名字不一样(有的是url)
      username: 账号
      password: 密码
    doris: # doris配置
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: 数据库地址
      username: 账号
      password: 密码

4. Mysql和Doris 配置类

4.1 Mysql配置类

@Configuration
//basePackages 这里是mapper所在包路径, 根据自己项目调整
@MapperScan(basePackages = "com.mo.xue.doristest.mapper.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlConfig {
    //这里是mapper.xml路径, 根据自己的项目调整
    private static final String MAPPER_LOCATION = "classpath*:mapper/mysql/*.xml";
    //这里是数据库表对应的entity实体类所在包路径, 根据自己的项目调整 
    private static final String TYPE_ALIASES_PACKAGE = "com.mo.xue.doristest.bean.mysql.*"; 

    @Primary //这个注解的意思是默认使用当前数据源
    @Bean(name="mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("mysqlSqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        bean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        return bean.getObject();
    }

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

4.2 Doris配置类

@Configuration
@MapperScan(basePackages  = "com.mo.xue.doristest.mapper.doris" , sqlSessionFactoryRef = "dorisSqlSessionFactory")
public class DorisConfig {
    private static final String MAPPER_LOCATION = "classpath*:mapper/doris/*.xml";
    private static final String TYPE_ALIASES_PACKAGE = "com.mo.xue.doristest.bean.doris.*";

    @Bean("dorisDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.doris")
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("dorisSqlSessionFactory")
    public SqlSessionFactory dorisSqlSessionFactory(@Qualifier("dorisDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        bean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        return bean.getObject();
    }

    @Bean("dorisSqlSessionTemplate")
    public SqlSessionTemplate dorisSqlSessionTemplate(@Qualifier("dorisSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 按照以上步骤配置完成, 即可支持多数据源, 我的fireWatch是mysql数据库表, DwsIotDevice是doris数据库表。 单元测试接口都请求成功, 因为数据隐私问题, 我只把接口成功截图放这。接口请求时间很长问题请忽略, 因为我debug的原因。

 

 5.遇到的问题

5.1 经常遇到的问题 Invalid bound statement (not found), 根据问题参阅资料解决, 如果都没问题, 说明还是配置文件有问题, 再仔细定位一下。

问题参阅资料: ​​​​​​解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题_写代码的蜗牛-CSDN博客_org.apache.ibatis.binding

5.2 经常遇到的问题 XXX table not found , 这个问题应该是配置文件只有一个成功了,另一个数据源的配置没有生效。前提是两个, 表只存在其中一个数据源中才会出现这个错误。

6.参阅资料:

 springboot-整合多数据源配置 - AizenSousuke - 博客园

  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
SpringBoot可以使用MyBatis-Plus实现多数据配置。多数据配置是指在一个应用中配置多个数据库连接,并能够根据需要选择使用哪个数据库。通过MyBatis-Plus,可以方便地配置和使用多个数据。 在配置数据时,可以使用MyBatis-Plus提供的注解和配置类来实现。可以根据需要配置多个数据,每个数据对应一个数据库。例如,可以配置MySQL数据库、PostgreSQL数据库和Doris数据库作为不同的数据。 具体配置方法可以参考引用和引用中提到的文章。这些文章详细介绍了如何在SpringBoot中整合MyBatis-Plus并配置动态数据。你可以根据这些文章的参考来配置你的多数据。 在配置完成后,你可以根据需要在代码中选择使用哪个数据进行数据库操作。通过配置数据,你可以在一个应用中同时操作多个数据库,方便实现复杂的业务逻辑。 总之,SpringBoot和MyBatis-Plus提供了方便的方式来配置和使用多数据,可以满足在一个应用中操作多个数据库的需求。你可以参考相关文档并按照需求进行配置和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【应用】SpringBoot -- 基于 MyBatis-Plus 实现多数据配置](https://blog.csdn.net/zqf787351070/article/details/127775519)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot整合MyBatisPlus配置动态数据的方法](https://download.csdn.net/download/weixin_38659646/12749115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值