Springboot集成Clickhouse

工程结构:

Springboot集成Clickhouse

maven依赖

        <dependency>
            <groupId>ru.yandex.clickhouse</groupId>
            <artifactId>clickhouse-jdbc</artifactId>
            <version>0.2.4</version>
        </dependency>

配属数据源

spring:
  datasource:
    driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
    url: jdbc:clickhouse://192.168.8.145:8123/test
  application:
    name: clickouse-application
mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 9000

Controller

package com.geewise.bigdata.controller;

import com.geewise.bigdata.entity.Order;
import com.geewise.bigdata.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:41
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{skuId}")
    public BigDecimal getAmountBySkuId(
            @PathVariable(name = "skuId", required = true) String skuId
    ){
        return orderService.getTotalAmountBySkuId(skuId);
    }

    @GetMapping("/detail/{id}")
    public List<Order> getOderBySkuId(
            @PathVariable(name = "id", required = true) Integer id
    ){
        return orderService.getOrderById(id);
    }
}

entity

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author lgn
 * @created 2021-01-13 15:24
 */
@Data
public class Order {
    private Integer id;
    private String skuId;
    private BigDecimal totalAmount;
    private Date createTime;
}

service

import com.geewise.bigdata.entity.Order;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:39
 */
public interface OrderService {
    BigDecimal getTotalAmountBySkuId(String skuId);
    List<Order> getOrderById(Integer id);
}

ServiceImpl

import com.geewise.bigdata.entity.Order;
import com.geewise.bigdata.mapper.OrderMapper;
import com.geewise.bigdata.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:40
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public BigDecimal getTotalAmountBySkuId(String skuId) {
        return orderMapper.selectTotalAmountSkuId(skuId);
    }

    @Override
    public List<Order> getOrderById(Integer id) {
        return orderMapper.selectOrder(id);
    }
}

mapper

import com.geewise.bigdata.entity.Order;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:28
 */
public interface OrderMapper {
    BigDecimal selectTotalAmountSkuId(String skuId);
    List<Order> selectOrder(Integer Id);
}

Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.geewise.bigdata.mapper.OrderMapper">
    <sql id="baseSql">
        SELECT id as id,sku_id as skuId, total_amount as totalAmount,create_time as createTime
    </sql>

    <!--map-underscore-to-camel-case 大写转下划线, 所以column的配置需要写成驼峰 result property="totalAmount" column="totalAmount"-->
    <resultMap id="baseResult" type="com.geewise.bigdata.entity.Order">
        <id property="id" column="id" />
        <result property="skuId" column="skuId" />
        <result property="totalAmount" column="totalAmount" />
        <result property="createTime" column="createTime" />
    </resultMap>
    <select id="selectTotalAmountSkuId" parameterType="String" resultType="java.math.BigDecimal">
      SELECT SUM(total_amount) as sum_amount FROM `order` WHERE sku_id=#{skuId}
    </select>

    <select id="selectOrder" parameterType="Integer" resultMap="baseResult">
      <include refid="baseSql"></include>
        FROM `order` WHERE id = #{Id}
    </select>

</mapper>

测试数据:

create table order(id UInt32,sku_id String,total_amount Decimal(16,2),create_time Datetime) engine =MergeTree partition by toYYYYMMDD(create_time) primary key (id) order by (id,sku_id);

查看一下数据库(可视化数据库安装翻看我以前的文章)

Springboot集成Clickhouse

启动项目

Springboot集成Clickhouse

访问一下接口

Springboot集成Clickhouse
访问成功。

上一篇:Kruskal算法(二)之 C++详解


下一篇:Java中Double保留后小数位的几种方法