Skip to content

tidb-samples/tidb-java-springboot-jpa-quickstart

Repository files navigation

How to Connect to TiDB Using Spring Boot and Spring Data JPA

Java CI with Maven

English | 中文

This is an example project by PingCAP that demonstrates connecting to TiDB using Spring Boot and Spring Data JPA. Additionally, this project utilizes Hibernate as the provider for Spring Data JPA.

TiDB is a MySQL-compatible database. Spring is a popular open-source Java container framework, and we've chosen to use Spring Boot as the way to use Spring.

Prerequisites

Getting Started

1. Clone the Example Code Repository to Your Local Machine

git clone https://github.com/tidb-samples/tidb-java-springboot-jpa-quickstart.git
cd tidb-java-springboot-jpa-quickstart

2. Configure Connection Information

(Option 1) TiDB Serverless
  1. In the TiDB Cloud console, open the Clusters page, select your TiDB Serverless cluster, go to the Overview page, and click the Connect button in the upper right corner.

  2. Ensure that the configuration in the window matches your runtime environment.

    • Endpoint Type is Public
    • Connect With is General
    • Operating System matches your runtime environment

    If you are running in Windows Subsystem for Linux (WSL), switch to the corresponding Linux distribution.

  3. Click Generate password to generate a password.

    If you have generated a password before, you can use the original password or click Reset Password to generate a new one.

  4. Run the following command to copy and rename env.sh.example to env.sh:

    cp env.sh.example env.sh
  5. Copy and paste the corresponding connection string into env.sh. Change the example results as follows.

    export TIDB_HOST='{gateway-region}.aws.tidbcloud.com'
    export TIDB_PORT='4000'
    export TIDB_USER='{prefix}.root'
    export TIDB_PASSWORD='{password}'
    export TIDB_DB_NAME='test'
    export USE_SSL='true'

    Replace placeholders {} with values obtained from the Connect window.

    TiDB Serverless requires the use of a secure connection, so the value of USE_SSL should be set to true.

  6. Save the file.

(Option 2) TiDB Dedicated
  1. In the TiDB Cloud Web Console, select your TiDB Dedicated cluster, go to the Overview page, click the Connect button in the upper right corner. Click Allow Access from Anywhere.

    For more configuration details, refer to TiDB Dedicated Standard Connection Guide.

  2. Run the following command to copy and rename env.sh.example to env.sh:

    cp env.sh.example env.sh
  3. Copy and paste the corresponding connection string into env.sh. Change the example results as follows.

    export TIDB_HOST='{host}.clusters.tidb-cloud.com'
    export TIDB_PORT='4000'
    export TIDB_USER='{prefix}.root'
    export TIDB_PASSWORD='{password}'
    export TIDB_DB_NAME='test'
    export USE_SSL='false'

    Replace placeholders {} with values obtained from the Connect window, and configure the certificate path downloaded in previous steps.

  4. Save the file.

(Option 3) Self-Hosted TiDB
  1. Run the following command to copy and rename env.sh.example to env.sh:

    cp env.sh.example env.sh
  2. Copy and paste the corresponding connection string into env.sh. Change the example results as follows.

    export TIDB_HOST='{tidb_server_host}'
    export TIDB_PORT='4000'
    export TIDB_USER='root'
    export TIDB_PASSWORD='{password}'
    export TIDB_DB_NAME='test'
    export USE_SSL='false'

    Replace placeholders {} with values corresponding to your TiDB setup. If you are running TiDB on your local machine, the default Host address is 127.0.0.1, and the password is empty.

  3. Save the file.

3. Run the Example Code

  1. Run the following command to start the service written in the example code:

    make
  2. Open another terminal and initiate the request script:

    make request

4. Expected Output

Expected Output

Notes

Regarding the third-party libraries and frameworks used, you can refer to their respective official documentation:

Next Steps

About

A simple Java app that connects to a TiDB database cluster and performs basic CRUD operations with Spring Boot, Spring Data JPA, and Hibernate.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published