嗨, 大家好, 最近公司想要使用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 - 博客园

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐