简单记录一下Springboot配置不同数据源(clickhouse和mysql)

 Maven依赖

 
        
            ru.yandex.clickhouse
            clickhouse-jdbc
            0.1.52
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.17
        
        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.1
        

 application.yml配置

spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    dynamic:
      primary: mysql
      strict: false
      datasource:
        #可以配置多个
        mysql:
          url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DATABASE:easysite_3_1_2}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&createDatabaseIfNotExist=true
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        clickhouse:
          url: jdbc:clickhouse://hadoop102:8123
          username: default
          password:
          driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
      flyway:
        locations: classpath:mysql/migration

 pojo:UserInfo

注意:这里的BaseEntity是公司封装的框架,里面有已经封装好的字段,譬如ID,IsDeleted等字段。结合Flyway可以自动创建table。

我把Maven依赖贴出来


        
            net.microcental
            easysite-common-all
            3.1.2
        
package net.microcental.easysite.demo.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import net.microcental.easysite.common.core.domain.entity.BaseEntity;

import java.io.Serializable;

@Data
@TableName("cs_user_info")
public class UserInfo extends BaseEntity {
//    private static final long serialVersionUID = 1L;
//    private Integer id;
    @TableField("")
    private String userName;
    private String passWord;
    private String phone;
    private String email;
    private String createDay;
}

 Mapper

注意:可以用@DB注解标识在类上面,表示该类所有方法走clickhouse这个数据源。

package net.microcenta

你可能感兴趣的:(spring,boot,java)