Skip to content

bianchengyuanyuan/tidb-example-java

 
 

Repository files navigation

tidb-example-java

Outline

It's an example for Java and TiDB. Contains subproject:

We use different frameworks or libraries to implement similar processes to reflect the solution for connecting to TiDB in different environments

This is a process about the game, each player has two attributes, coins and goods, and each player has their own unique id as an identifier. Players can trade with each other, provided that the coins and goods are sufficient

The process is as follows:

  1. Create a player
  2. Create some players
  3. Read players amount
  4. Read some players attributes
  5. Two player trade with insufficient coins or goods
  6. Two player trade with sufficient coins or goods

Dependency

plain-java-jdbc

It's an example used plain Java and JDBC to connect TiDB.

Running way

  1. Makefile(recommend)

    • Run make plain-java-jdbc
  2. Manual

    • Run create table sql in your TiDB
    • Into plain-java-jdbc
    • Run mvn clean package
    • Run java -jar target/plain-java-jdbc-0.0.1-jar-with-dependencies.jar

Expected output

plain-java-jdbc expected output

Code

plain-java-hibernate

Notes: Hibernate 6.0.0.Beta2 and above supports the TiDB dialect, so our examples will be written based on this version, and we recommend using this version when connecting to TiDB as well

It's an example used plain Java and Hibernate to connect TiDB.

Running way

  1. Makefile(recommend)

    • Run make plain-java-hibernate
  2. Manual

    • Into plain-java-hibernate
    • Run mvn clean package
    • Run java -jar target/plain-java-hibernate-0.0.1-jar-with-dependencies.jar

Expected output

plain-java-hibernate expected output

Code

plain-java-mybatis

It's an example used plain Java and Mybatis to connect TiDB.

Running way

  1. Makefile(recommend)

    • Run make plain-java-mybatis
  2. Manual

    • Into plain-java-mybatis
    • Run mvn clean package
    • Run java -jar target/plain-java-mybatis-0.0.1-jar-with-dependencies.jar

Code generate

  1. Makefile(recommend)

    • Run make plain-java-mybatis-gen
  2. Manual

    • Into plain-java-mybatis
    • Remove src/main/java/com/pingcap/dao/Player.java
    • Remove src/main/java/com/pingcap/dao/PlayerMapper.java
    • Remove src/main/resources/mapper/PlayerMapper.xml
    • Run mvn mybatis-generator:generate

Expected output

plain-java-mybatis expected output

Code

Config

Java Code

spring-jpa-hibernate

Notes: Hibernate 6.0.0.Beta2 and above supports the TiDB dialect, so our examples will be written based on this version, and we recommend using this version when connecting to TiDB as well

It's an example service used Spring Boot, JPA and Hibernate to connect TiDB. Provide a group of HTTP Restful interface.

Running way

  1. Makefile(recommend)

    1. First terminal
      • Run make spring-jpa-hibernate
    2. Second terminal
      • Run make request
    3. You can quit first terminal or run make stop to stop service
  2. Manual

    • Into spring-jpa-hibernate
    • Run mvn clean package
    • Run java -jar target/spring-jpa-hibernate-0.0.1.jar
    • Request:
      • Option 1 (recommend):
      • Option 2:
        • Using script to request. It's based on curl
      • Option 3:
        • Request HTTP Restful interface by other way

Expected output

  1. request expected output
  2. service expected output

Code

More Demo

About

A set of examples of using Java to connect to TiDB

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 90.9%
  • Makefile 7.9%
  • Shell 1.2%