核心依赖引入
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
<!--Mybatis-plus ORM-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.9</version>
</dependency>
建立ClickHouse的测试数据库和表
配置数据源
spring:
application:
name: clickhouse-project
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:clickhouse://120.53.238.87:8123/study_db
driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
username: default
password:
mybatis-plus:
type-aliases-package: com.gton.user.entity
mapper-locations: classpath*:com/gton/user/mapper/xml/*Mapper.xml,classpath*:/mapper/**/*.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
cache-enabled: false #开启二级缓存
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台日志
check-config-location: true # 检查xml是否存在
type-enums-package: com.gton.enumPackage #通用枚举开启
global-config:
db-config:
logic-not-delete-value: 1
logic-delete-field: isDel
logic-delete-value: 0
server:
port: 18123
Mybatisplus的逆向工程或者EasyCode全自动代码生成器
完成MapperXML 、 MapperInterface 、 Entity 、 Controller 、Service
与ServiceImpl 的建立
Entity实体类如下
package com.gton.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* (TableClickhouse)表实体类
*
* @author 郭童
* @since 2023-05-21 20:14:14
*/
@Data
@Accessors(chain = true)
@ApiModel(value = "TableClickhouse", description = "EasyCode")
public class TableClickhouse implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_ID)
@ApiModelProperty("${column.comment}")
private Integer id;
@TableField(value = "name")
@ApiModelProperty("${column.comment}")
private String name;
@TableField(value = "age")
@ApiModelProperty("${column.comment}")
private Integer age;
@TableField(value = "data")
@ApiModelProperty("${column.comment}")
private String data;
}
Mapper核心就是要自己写Update/delete:他们与Mysql的不一样
删除:
ALTER TABLE study_db.table_clickhouse DELETE WHERE id = #{idw}
修改:
ALTER TABL
ClickHouse的修改和删除 SQL操作与Mysql不相同。注意它是异步的,不会返回执行结果!!
package com.gton.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import com.gton.entity.TableClickhouse;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* (TableClickhouse)表数据库访问层
*
* @author 郭童
* @since 2023-05-21 20:14:14
*/
@Mapper
@SuppressWarnings("all")
public interface TableClickhouseMapper extends BaseMapper<TableClickhouse> {
// 批量插入
int insertBatchSomeColumn(@Param("list") List<TableClickhouse> batchList);
/**
* Description: Clickhouse 的删除语句
*/
@Delete("ALTER TABLE study_db.table_clickhouse DELETE WHERE id = #{idw}")
Integer removeByIdsClickhouse(@Param("idw") Long idw);
/**
* Description: Clickhouse 修改Clickhouse
*/
@Update("ALTER TABLE study_db.table_clickhouse UPDATE name=#{namew} WHERE id = #{idw}")
boolean updateClickhouse(@Param("idw") Long idw, @Param("namew") String namew);
/**
* Description: Clickhouse 修改Clickhouse2
*/
@Update("ALTER TABLE study_db.table_clickhouse UPDATE " +
"name=#{name} ," +
"age=#{age} ," +
"data=#{data} " +
"WHERE id = #{id}")
Integer updateClickHouseById(TableClickhouse tableClickhouse);
}