SpringBoot+MybatisPlu+Druid集成ClickHouse

首先就是引入依赖

<!-- 集成clickhouse -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.21</version>
		</dependency>
		<dependency>
			<groupId>ru.yandex.clickhouse</groupId>
			<artifactId>clickhouse-jdbc</artifactId>
			<version>0.2.4</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.3.2</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.5.0</version>
		</dependency>
		<!--> 代码模板 </!-->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
		</dependency>
		<!-- 集成clickhouse -->

配置配置文件

server:
  port: 8085
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    click:
      driverClassName: ru.yandex.clickhouse.ClickHouseDriver
      url: jdbc:clickhouse://192.168.31.216:8123/default
      username: default
      password: jack9876
      initialSize: 10
      maxActive: 100
      minIdle: 10
      maxWait: 6000
  rabbitmq:
    ####连接地址
    host: 127.0.0.1
    ####端口号
    port: 5672
    ####账号
    username: guest
    ####密码
    password: guest

mybatis-plus:
  mapper-locations: classpath:mapper/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: true
    lazy-loading-enabled: true
    multiple-result-sets-enabled: true
    use-generated-keys: true
    default-statement-timeout: 60
    default-fetch-size: 100
  type-aliases-package: com.example.clickhouse.po

然后clickhouse数据库连接是粗腰自己注入Spring进行管理

@Configuration
public class DruidClickHouseConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.click")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

}

然后使用mybatis-plus自动生成工具生成代码,在使用代码生成工具建议使用mybatis-plus-generator 3.5.0以上版本,3.4版本会出现一些错误,节省我们的时间成本

public class CodeGeneratorConfig {
    //文件生成输出信息
    private static final String REPLACE_LOCAL_PATH = "/target/test-classes/";
    private static final String JAVA_PATH = "/src/main/java";
    private static final String author = "hong";
    //包名及文件名相关
    //模块名称
    private static final String moduleName = "";
    private static final String parentPackage = "com.example.clickhouse";
    private static final String mapperName = moduleName + "mapper";
    private static final String xmlName = mapperName + ".xml";
    private static final String entityPattern = "%sPo";//Service文件命名格式
    private static final String serviceNamePattern = "%sDao";//Service文件命名格式
    private static final String serviceImplNamePattern = "%sDaoImpl";//Service文件命名格式
    private static final String serviceName = moduleName + "dao";
    private static final String implName = moduleName + "dao.impl";
    private static final String pojoName = moduleName + "model.po";
    private static final String controllerName = moduleName + "controller";

    //数据库连接信息
    //数据库类型MySQL、Oracle等
    private static final String userName = "default";    private static final String password = "jack9876";
    private static final String jdbcUrl = "jdbc:clickhouse://192.168.31.216:8123/default";
    private static final String driverClassName = "ru.yandex.clickhouse.ClickHouseDriver";
    //表名前缀
    private static final String tablePrefix = "";
    //多表用逗号隔开"table1,table2"
    private static final String tables = "user";
   

    public static void main(String[] args) {
        // 获取项目路径
        String projectPath = ClassLoader.getSystemResource("").getPath().replace(REPLACE_LOCAL_PATH, "");
        // 全局配置
        GlobalConfig gc = GeneratorBuilder.globalConfigBuilder().fileOverride().openDir(false)
                .outputDir(projectPath + JAVA_PATH)
                .author(author)
                .openDir(true)
                .commentDate("yyyy-MM-dd")
                .build();
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig.Builder(jdbcUrl, userName, password)
                .dbQuery(new ClickHouseQuery()).schema("default").build();
        // 包配置
        PackageConfig pc = GeneratorBuilder.packageConfigBuilder()
                .parent(parentPackage)
                .controller(controllerName)
                .mapper(mapperName)
                .service(serviceName)
                .serviceImpl(implName)
                .xml(xmlName)
                .entity(pojoName)
                .build();
        // 策略配置
        StrategyConfig strategy = GeneratorBuilder.strategyConfigBuilder()
                .addInclude(tables)
                .addTablePrefix("")
                .controllerBuilder().enableHyphenStyle()
                .enableRestStyle()
                .serviceBuilder().formatServiceFileName(serviceNamePattern)
                .formatServiceImplFileName(serviceImplNamePattern)
                .entityBuilder().formatFileName(entityPattern)
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .enableLombok()
                .mapperBuilder().enableBaseColumnList()
                .enableBaseResultMap()
                .build();
        //模板配置
        TemplateConfig templateConfig = GeneratorBuilder.templateConfigBuilder().build();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc).global(gc).strategy(strategy).template(templateConfig).packageInfo(pc);
        mpg.execute(new FreemarkerTemplateEngine());

    }
}

接下就是操作写入数据库了,因clickhouse的SQL语法不是标准的SQL语法。所以建议使用自定义SQL语句来实现功能,不要使用Mybatis-Plu自带部分去操作SQL。
例如插入语句

<insert id="insertUser" useGeneratedKeys="false" parameterType="com.example.clickhouse.po.User">
      INSERT INTO default.user ( user_id, user_name, user_sex ) VALUES (#{userId},#{userName},#{userSex})
    </insert>

后面代码跟Mysql等操作基本相同,不在演示

你可能感兴趣的:(数据库相关,mysql,数据库,database)