From e647b112da7e3b18f0e688671d6a787e579d0a81 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Wed, 31 May 2023 22:56:50 +0300 Subject: [PATCH] BAEL-6024 - Guide to YugabyteDB --- persistence-modules/pom.xml | 1 + .../spring-data-yugabytedb/pom.xml | 55 +++++++++++++++++++ .../src/main/java/com/baeldung/Main.java | 32 +++++++++++ .../src/main/java/com/baeldung/User.java | 44 +++++++++++++++ .../java/com/baeldung/UserRepository.java | 6 ++ .../src/main/resources/application.properties | 4 ++ 6 files changed, 142 insertions(+) create mode 100644 persistence-modules/spring-data-yugabytedb/pom.xml create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 9361e26d7d..0046165d7c 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -108,6 +108,7 @@ spring-jooq spring-mybatis spring-persistence-simple + spring-data-yugabytedb fauna spring-data-rest diff --git a/persistence-modules/spring-data-yugabytedb/pom.xml b/persistence-modules/spring-data-yugabytedb/pom.xml new file mode 100644 index 0000000000..a14f0e7114 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + spring-data-yugabytedb + 1.0 + spring-data-yugabytedb + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.postgresql + postgresql + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java new file mode 100644 index 0000000000..27b087790a --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java @@ -0,0 +1,32 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main implements CommandLineRunner { + + @Autowired + private UserRepository userRepository; + + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } + + @Override + public void run(String... args) throws InterruptedException { + + int iterationCount = 1_000; + int elementsPerIteration = 100; + + for (int i = 0; i < iterationCount; i++) { + for (long j = 0; j < elementsPerIteration; j++) { + User user = new User(); + userRepository.save(user); + } + Thread.sleep(1000); + } + } +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java new file mode 100644 index 0000000000..278bc6c9ae --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java @@ -0,0 +1,44 @@ +package com.baeldung; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String name; + + Long getId() { + return id; + } + + void setId(Long id) { + this.id = id; + } + + String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java new file mode 100644 index 0000000000..d04047d416 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java @@ -0,0 +1,6 @@ +package com.baeldung; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties b/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties new file mode 100644 index 0000000000..fdb3d50ec8 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:postgresql://localhost:5433/yugabyte +spring.datasource.username=yugabyte +spring.datasource.password=yugabyte +spring.jpa.hibernate.ddl-auto=create