SoFunction
Updated on 2025-04-23

SpringBoot+MyBatis Integration ClickHouse Practical Record

Integrating Spring Boot, MyBatis, and ClickHouse allows you to efficiently interact with the ClickHouse database using Java-developed applications. Here is a basic step guide to help you complete this integration process:

1. Add dependencies

First, in yourAdd necessary Maven dependencies to the file. You need to introduce Spring Boot Starter, MyBatis Spring Boot Starter, and ClickHouse JDBC driver.

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- MyBatis Spring Boot Starter -->
    <dependency>
        <groupId></groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version> <!-- Please select the version as needed -->
    </dependency>
    <!-- ClickHouse JDBC Driver -->
    <dependency>
        <groupId></groupId>
        <artifactId>clickhouse-jdbc</artifactId>
        <version>0.3.2</version> <!-- Please select the version as needed -->
    </dependency>
    <!-- Other dependencies -->
</dependencies>

2. Configure the data source

existorConfigure the data source to connect to ClickHouse.

:

# ClickHouse Database Connection Configuration=jdbc:clickhouse://localhost:8123/default
=your_username
=your_password
-class-name=
# MyBatis configuration-aliases-package=
-locations=classpath:mapper/*.xml

:

spring:
  datasource:
    url: jdbc:clickhouse://localhost:8123/default
    username: your_username
    password: your_password
    driver-class-name: 
mybatis:
  type-aliases-package: 
  mapper-locations: classpath:mapper/*.xml

3. Create Entity Class (Entity)

Create the corresponding Java entity class for your table. For example, if you have a nameusersYou can create a tableUserEntity class.

package ;
public class User {
    private Long id;
    private String name;
    private Integer age;
    // Getters and Setters
}

4. Create a Mapper interface

Use MyBatis annotations or XML mapping files to define SQL statements. Here we use XML mapping files as an example.

:

package ;
import ;
import ;
import ;
@Mapper
public interface UserMapper {
    List<User> findAll();
    void insert(User user);
}

resources/mapper/:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-////DTD Mapper 3.0//EN"
  "/dtd/">
<mapper namespace="">
    <select  resultType="">
        SELECT * FROM users
    </select>
    <insert  parameterType="">
        INSERT INTO users (id, name, age) VALUES (#{id}, #{name}, #{age})
    </insert>
</mapper>

5. Write service layer code

Write service layer code to call the methods of the Mapper interface.

package ;
import ;
import ;
import ;
import ;
import ;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> getAllUsers() {
        return ();
    }
    public void saveUser(User user) {
        (user);
    }
}

6. Test with the controller

Finally, create a simple controller to test whether your service is working properly.

package ;
import ;
import ;
import ;
import .*;
import ;
@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public List<User> getUsers() {
        return ();
    }
    @PostMapping
    public void createUser(@RequestBody User user) {
        (user);
    }
}

After making sure all configurations are correct, start the Spring Boot application and access the API endpoint to test whether you can successfully communicate with the ClickHouse database.

This is the article about SpringBoot+MyBatis integration of ClickHouse practice. For more related SpringBoot MyBatis integration of ClickHouse content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!