#1
This guide shows how to connect Spring Boot applications to a ClickHouse database with simple configuration and setup.

1. Add Dependencies

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>com.clickhouse</groupId>
  <artifactId>clickhouse-jdbc</artifactId>
  <version>0.6.2</version>
</dependency>

2. Configure ClickHouse Connection

In application.properties:
spring.datasource.url=jdbc:clickhouse://localhost:8123/default
spring.datasource.username=default
spring.datasource.password=
spring.datasource.driver-class-name=com.clickhouse.jdbc.ClickHouseDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

3. Create Entity

@Entity
public class User {
  @Id @GeneratedValue
  private Long id;
  private String name;
  private int age;
}

4. Repository

public interface UserRepository extends JpaRepository<User, Long> {}

5. Service

@Service
public class UserService {
  private final UserRepository repo;

  public UserService(UserRepository repo) {
    this.repo = repo;
  }

  public User save(User user) {
    return repo.save(user);
  }

  public List<User> findAll() {
    return repo.findAll();
  }
}

6. REST Controller

@RestController
@RequestMapping("/users")
public class UserController {
  private final UserService service;

  public UserController(UserService service) {
    this.service = service;
  }

  @PostMapping
  public User add(@RequestBody User user) {
    return service.save(user);
  }

  @GetMapping
  public List<User> getAll() {
    return service.findAll();
  }
}

7. Test

  1. Start ClickHouse locally.
  2. Run Spring Boot app.
  3. Use Postman or curl to POST /users and GET /users.

image quote pre code