diff --git a/java/oceanbase-spring-boot-mysql-jpa/README-CN.md b/java/oceanbase-spring-boot-mysql-jpa/README-CN.md new file mode 100644 index 0000000..6124426 --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/README-CN.md @@ -0,0 +1,279 @@ +# Spring Boot 连接 OceanBase 指南(使用 Spring Data JPA) + +[English](README.md) | 简体中文 + +本文介绍如何通过 OceanBase 官方 SpringBoot 连接示例连接 OceanBase 数据库。 +由于 OceanBase 支持 MySQL 模式与 Oracle 模式,因此可以使用 MySQL 驱动连接 OceanBase。 +## 快速开始 + +### 在 pom.xml 中首先加入 Spring Boot 与 Spring Data JPA 相关的驱动, 以及 MySQL 驱动,pom.xml 参考[OceanBase SpringBoot 连接示例](https://www.oceanbase.com/docs/community-observer-cn-10000000000900914) 示例。 + +```xml + + 4.0.0 + + com.oceanbase.samples + oceanbase-spring-boot-mysql-jpa + 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + oceanbase-spring-boot + + + UTF-8 + 1.8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + spring-boot-starter-json + org.springframework.boot + + + + + mysql + mysql-connector-java + 8.0.25 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + junit + junit + 3.8.1 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.8.5 + + + +``` + +### 在 application.yml 文件加入数据库连接信息等。 + +```yaml +server: + port: 8081 +spring: + jpa: + database: mysql + show-sql: true + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://host:port/test?characterEncoding=UTF-8 + username: ***** + password: ***** +#spring.jpa.hibernate.ddl-auto=update +jackson: + serialization: + indent_output: true +``` +### 测试用例: + +#### 1.定义简单实体: +```java +package com.oceanbase.samples.entity; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.Objects; + +/* + * CREATE TABLE TEST(id integer, name varchar2(50)) + * + */ +@Entity +@Table( name = "test" ) +public class TestEntity implements Serializable { + + private static final long serialVersionUID = -6578740021873269176L; + + @Id + // @GeneratedValue(strategy=GenerationType.AUTO) //oracle 没有自增策略,添加该注解可以自动生成一个序列,提供自增主键,若数据库已有相关序列,可以忽 //略该注解。 + @Column(name = "id") + private Integer testId; + + @Column( name = "name" ) + private String TestName; + + + + public TestEntity(){ + + } + + public TestEntity(String bauer) { + this.TestName = bauer; + } + + + public Integer getTestId() { + return testId; + } + + public void setTestId(Integer testId) { + this.testId = testId; + } + + public String getTestName() { + return TestName; + } + + public void setTestName(String testName) { + TestName = testName; + } + + @Override + public String toString() { + return "TestEntity{" + + "testId=" + testId + + ", TestName='" + TestName + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestEntity that = (TestEntity) o; + return Objects.equals(testId, that.testId) && Objects.equals(TestName, that.TestName); + } + + @Override + public int hashCode() { + return Objects.hash(testId, TestName); + } +} +``` +#### 2.创建简单查询: +```java +package com.oceanbase.samples.repository; + +import com.oceanbase.samples.entity.TestEntity; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface TestEntityRepository extends CrudRepository { + List findByTestName(String lastName); + + List findByTestNameContaining(String testName); + + TestEntity findById(int id); +} +``` + +#### 3.在 controller 创建测试用例,测试增删改查: +```java +package com.oceanbase.samples.controller; + +import com.oceanbase.samples.entity.TestEntity; +import com.oceanbase.samples.repository.TestEntityRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@RestController +@RequestMapping("/") +public class TestController { + private static final Logger log = LoggerFactory.getLogger(TestController.class); + + @Autowired + private TestEntityRepository testEntityRepository; + + @PostMapping("/save") + public Boolean save(@RequestBody TestEntity testSaveEntity) { + TestEntity resultEntity = testEntityRepository.save(testSaveEntity); + log.info("save result: {}", resultEntity); + return resultEntity != null; + } + + @GetMapping("/{id}") + public TestEntity findById(@PathVariable("id") int id) { + log.info("find by id: {}", id); + TestEntity resultEntity = testEntityRepository.findById(id); + log.info("find result: {}", resultEntity); + return resultEntity; + } + + @GetMapping("/findAll") + public List findAll() { + log.info("find all"); + return StreamSupport.stream(testEntityRepository.findAll().spliterator(), false) + .filter(Objects::nonNull) + .peek(entity -> log.info(entity.toString())) + .collect(Collectors.toList()); + } + + @GetMapping("/findByName") + public List findByName(@RequestParam("name") String name) { + log.info("find by name: {}", name); + return testEntityRepository.findByTestNameContaining(name); + } + + @DeleteMapping("/{id}") + public void deleteById(@PathVariable("id") int id) { + log.info("delete by id: {}", id); + testEntityRepository.deleteById(id); + } + + @PutMapping("/update") + public void update(@RequestBody TestEntity testUpdateEntity) { + log.info("update: {}", testUpdateEntity); + testEntityRepository.save(testUpdateEntity); + } + +} +``` + +#### 4.创建应用程序类,并运行: +```java +package com.oceanbase.samples; + +import com.oceanbase.samples.entity.TestEntity; +import com.oceanbase.samples.repository.TestEntityRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class HelloOceanBaseBootApplication { + + public static void main( String[] args ) { + SpringApplication.run(HelloOceanBaseBootApplication.class, args); + } + +} +``` + +修改代码中的连接信息,之后你就可以直接使用 run.sh 运行示例代码。 + +```bash +sh run.sh +``` diff --git a/java/oceanbase-spring-boot-mysql-jpa/README.md b/java/oceanbase-spring-boot-mysql-jpa/README.md new file mode 100644 index 0000000..78e8390 --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/README.md @@ -0,0 +1,281 @@ +# Guide to Connecting Spring Boot to OceanBase (Using Spring Data JPA) + +English | [简体中文](README-CN.md) + +This document introduces how to connect to the OceanBase database through Spring's official Spring Data JPA. +Since OceanBase supports MySQL mode and Oracle mode, you can use the MySQL driver to connect to OceanBase. + +## Quick Start + +### First, add the Spring Boot, Spring Data JPA, and MySQL driver dependencies to the pom.xml file, referring to the [OceanBase SpringBoot connection example](https://www.oceanbase.com/docs/community-observer-cn-10000000000900914). + +```xml + + 4.0.0 + + com.oceanbase.samples + oceanbase-spring-boot-mysql-jpa + 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + oceanbase-spring-boot + + + UTF-8 + 1.8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + spring-boot-starter-json + org.springframework.boot + + + + + mysql + mysql-connector-java + 8.0.25 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + junit + junit + 3.8.1 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.8.5 + + + +``` + +### Add the database connection information and other configurations in the application.yml file. + +```yaml +server: + port: 8081 +spring: + jpa: + database: mysql + show-sql: true + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://host:port/test?characterEncoding=UTF-8 + username: ***** + password: ***** +#spring.jpa.hibernate.ddl-auto=update +jackson: + serialization: + indent_output: true +``` + +### Test Cases: + +#### 1.Define a simple entity: +```java +package com.oceanbase.samples.entity; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.Objects; + +/* + * CREATE TABLE TEST(id integer, name varchar2(50)) + * + */ +@Entity +@Table( name = "test" ) +public class TestEntity implements Serializable { + + private static final long serialVersionUID = -6578740021873269176L; + + @Id + // @GeneratedValue(strategy=GenerationType.AUTO) //Oracle does not have an auto-increment strategy. Adding this annotation can automatically generate a sequence to provide an auto-increment primary key. If the database already has a relevant sequence, you can omit this annotation. + @Column(name = "id") + private Integer testId; + + @Column( name = "name" ) + private String TestName; + + + + public TestEntity(){ + + } + + public TestEntity(String bauer) { + this.TestName = bauer; + } + + + public Integer getTestId() { + return testId; + } + + public void setTestId(Integer testId) { + this.testId = testId; + } + + public String getTestName() { + return TestName; + } + + public void setTestName(String testName) { + TestName = testName; + } + + @Override + public String toString() { + return "TestEntity{" + + "testId=" + testId + + ", TestName='" + TestName + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestEntity that = (TestEntity) o; + return Objects.equals(testId, that.testId) && Objects.equals(TestName, that.TestName); + } + + @Override + public int hashCode() { + return Objects.hash(testId, TestName); + } +} +``` +#### 2.Create a simple query: +```java +package com.oceanbase.samples.repository; + +import com.oceanbase.samples.entity.TestEntity; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface TestEntityRepository extends CrudRepository { + List findByTestName(String lastName); + + List findByTestNameContaining(String testName); + + TestEntity findById(int id); +} +``` + +#### 3.Create test cases in the controller for CRUD operations: +```java +package com.oceanbase.samples.controller; + +import com.oceanbase.samples.entity.TestEntity; +import com.oceanbase.samples.repository.TestEntityRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@RestController +@RequestMapping("/") +public class TestController { + private static final Logger log = LoggerFactory.getLogger(TestController.class); + + @Autowired + private TestEntityRepository testEntityRepository; + + @PostMapping("/save") + public Boolean save(@RequestBody TestEntity testSaveEntity) { + TestEntity resultEntity = testEntityRepository.save(testSaveEntity); + log.info("save result: {}", resultEntity); + return resultEntity != null; + } + + @GetMapping("/{id}") + public TestEntity findById(@PathVariable("id") int id) { + log.info("find by id: {}", id); + TestEntity resultEntity = testEntityRepository.findById(id); + log.info("find result: {}", resultEntity); + return resultEntity; + } + + @GetMapping("/findAll") + public List findAll() { + log.info("find all"); + return StreamSupport.stream(testEntityRepository.findAll().spliterator(), false) + .filter(Objects::nonNull) + .peek(entity -> log.info(entity.toString())) + .collect(Collectors.toList()); + } + + @GetMapping("/findByName") + public List findByName(@RequestParam("name") String name) { + log.info("find by name: {}", name); + return testEntityRepository.findByTestNameContaining(name); + } + + @DeleteMapping("/{id}") + public void deleteById(@PathVariable("id") int id) { + log.info("delete by id: {}", id); + testEntityRepository.deleteById(id); + } + + @PutMapping("/update") + public void update(@RequestBody TestEntity testUpdateEntity) { + log.info("update: {}", testUpdateEntity); + testEntityRepository.save(testUpdateEntity); + } + +} +``` + +#### 4.Create the application class and run it: +```java +package com.oceanbase.samples; + +import com.oceanbase.samples.entity.TestEntity; +import com.oceanbase.samples.repository.TestEntityRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class HelloOceanBaseBootApplication { + + public static void main( String[] args ) { + SpringApplication.run(HelloOceanBaseBootApplication.class, args); + } + +} +``` + +Modify the connection info in code, and use `run.sh` to run the example code. + +```bash +sh run.sh +``` diff --git a/java/oceanbase-spring-boot/pom.xml b/java/oceanbase-spring-boot-mysql-jpa/pom.xml similarity index 62% rename from java/oceanbase-spring-boot/pom.xml rename to java/oceanbase-spring-boot-mysql-jpa/pom.xml index 32cf04f..a37aaab 100644 --- a/java/oceanbase-spring-boot/pom.xml +++ b/java/oceanbase-spring-boot-mysql-jpa/pom.xml @@ -3,12 +3,12 @@ 4.0.0 com.oceanbase.samples - oceanbase-spring-boot + oceanbase-spring-boot-mysql-jpa 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent - 2.7.16 + 2.0.1.RELEASE oceanbase-spring-boot @@ -21,7 +21,13 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web + + + spring-boot-starter-json + org.springframework.boot + + mysql @@ -33,27 +39,15 @@ spring-boot-starter-data-jpa - org.springframework.boot - spring-boot-starter-test + junit + junit + 3.8.1 test + + com.fasterxml.jackson.core + jackson-databind + 2.8.5 + - - - - src/main/java - - **/*.properties - **/*.yml - - - - src/main/resources - - **/*.properties - **/*.yml - - - - diff --git a/java/oceanbase-spring-boot/run.sh b/java/oceanbase-spring-boot-mysql-jpa/run.sh similarity index 100% rename from java/oceanbase-spring-boot/run.sh rename to java/oceanbase-spring-boot-mysql-jpa/run.sh diff --git a/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java new file mode 100644 index 0000000..380395f --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java @@ -0,0 +1,19 @@ +package com.oceanbase.samples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HelloOceanBaseBootApplication { + + private static final Logger log = LoggerFactory.getLogger(HelloOceanBaseBootApplication.class); + + public static void main( String[] args ) + { + SpringApplication.run(HelloOceanBaseBootApplication.class, args); + } + + +} diff --git a/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/controller/TestController.java b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/controller/TestController.java new file mode 100644 index 0000000..a2fd68d --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/controller/TestController.java @@ -0,0 +1,67 @@ +package com.oceanbase.samples.controller; + +import com.oceanbase.samples.entity.TestEntity; +import com.oceanbase.samples.repository.TestEntityRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@RestController +@RequestMapping("/") +public class TestController { + private static final Logger log = LoggerFactory.getLogger(TestController.class); + + @Autowired + private TestEntityRepository testEntityRepository; + + @PostMapping("/save") + public Boolean save(@RequestBody TestEntity testSaveEntity) { + TestEntity resultEntity = testEntityRepository.save(testSaveEntity); + log.info("save result: {}", resultEntity); + return resultEntity != null; + } + + + + @GetMapping("/{id}") + public TestEntity findById(@PathVariable("id") int id) { + log.info("find by id: {}", id); + TestEntity resultEntity = testEntityRepository.findById(id); + log.info("find result: {}", resultEntity); + return resultEntity; + } + + @GetMapping("/findAll") + public List findAll() { + log.info("find all"); + return StreamSupport.stream(testEntityRepository.findAll().spliterator(), false) + .filter(Objects::nonNull) + .peek(entity -> log.info(entity.toString())) + .collect(Collectors.toList()); + } + + @GetMapping("/findByName") + public List findByName(@RequestParam("name") String name) { + log.info("find by name: {}", name); + return testEntityRepository.findByTestNameContaining(name); + } + + @DeleteMapping("/{id}") + public void deleteById(@PathVariable("id") int id) { + log.info("delete by id: {}", id); + testEntityRepository.deleteById(id); + } + + @PutMapping("/update") + public void update(@RequestBody TestEntity testUpdateEntity) { + log.info("update: {}", testUpdateEntity); + testEntityRepository.save(testUpdateEntity); + } + +} diff --git a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/entity/TestEntity.java similarity index 100% rename from java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java rename to java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/entity/TestEntity.java diff --git a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java similarity index 86% rename from java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java rename to java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java index 34cbd04..5b5d2a5 100644 --- a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java @@ -10,5 +10,7 @@ public interface TestEntityRepository extends CrudRepository { List findByTestName(String lastName); + List findByTestNameContaining(String testName); + TestEntity findById(int id); } diff --git a/java/oceanbase-spring-boot/src/main/resources/application.yml b/java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml similarity index 54% rename from java/oceanbase-spring-boot/src/main/resources/application.yml rename to java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml index 53a8319..b7e199f 100644 --- a/java/oceanbase-spring-boot/src/main/resources/application.yml +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml @@ -2,13 +2,13 @@ server: port: 8081 spring: jpa: - database: mysql + database: mysql # mysql, oracle show-sql: true datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://host:port/test?characterEncoding=UTF-8 - username: ***** - password: ***** + url: jdbc:mysql://host:port/dbname?characterEncoding=UTF-8 + username: ***** # your username + password: ***** # your password #spring.jpa.hibernate.ddl-auto=update jackson: serialization: diff --git a/java/oceanbase-spring-boot/README-CN.md b/java/oceanbase-spring-boot/README-CN.md deleted file mode 100644 index 4d17548..0000000 --- a/java/oceanbase-spring-boot/README-CN.md +++ /dev/null @@ -1,238 +0,0 @@ -# Spring Boot 连接 OceanBase 指南(使用 Spring Data JPA) - -[English](README.md) | 简体中文 - -本文介绍如何通过 Spring 官方 Spring Data JPA 连接 OceanBase 数据库。 -由于 OceanBase 支持 MySQL 模式与 Oracle 模式,因此可以使用 MySQL 驱动连接 OceanBase。 -## 快速开始 - -### 在 Maven 中首先加入 Spring Boot 与 Spring Data JPA 相关的驱动, 以及 MySQL 驱动。 - -```xml - - org.springframework.boot - spring-boot-starter-parent - 2.7.16 - - - - org.springframework.boot - spring-boot-starter - - - mysql - mysql-connector-java - 8.0.25 - - - org.springframework.boot - spring-boot-starter-data-jpa - - -``` - -### 在 Spring Boot 的配置文件中加入数据库连接信息。 - -```yaml -server: - port: 8081 -spring: - jpa: - database: mysql - show-sql: true - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://host:port/test?characterEncoding=UTF-8 - username: ***** - password: ***** -#spring.jpa.hibernate.ddl-auto=update -jackson: - serialization: - indent_output: true -``` -### 紧接着参考[Spring Data JPA 快速入门示例](https://spring.io/guides/gs/accessing-data-jpa) 编写 demo: - -#### 1.定义简单实体: -```java -package com.oceanbase.samples.entity; - -import javax.persistence.*; -import java.io.Serializable; -import java.util.Objects; - -/* - * CREATE TABLE TEST(id integer, name varchar2(50)) - * - */ -@Entity -@Table( name = "test" ) -public class TestEntity implements Serializable { - - private static final long serialVersionUID = -6578740021873269176L; - - @Id - // @GeneratedValue(strategy=GenerationType.AUTO) //oracle 没有自增策略,添加该注解可以自动生成一个序列,提供自增主键,若数据库已有相关序列,可以忽 //略该注解。 - @Column(name = "id") - private Integer testId; - - @Column( name = "name" ) - private String TestName; - - - - public TestEntity(){ - - } - - public TestEntity(String bauer) { - this.TestName = bauer; - } - - - public Integer getTestId() { - return testId; - } - - public void setTestId(Integer testId) { - this.testId = testId; - } - - public String getTestName() { - return TestName; - } - - public void setTestName(String testName) { - TestName = testName; - } - - @Override - public String toString() { - return "TestEntity{" + - "testId=" + testId + - ", TestName='" + TestName + '\'' + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TestEntity that = (TestEntity) o; - return Objects.equals(testId, that.testId) && Objects.equals(TestName, that.TestName); - } - - @Override - public int hashCode() { - return Objects.hash(testId, TestName); - } -} - -``` -#### 2.创建简单查询: -```java -package com.oceanbase.samples.repository; - -import com.oceanbase.samples.entity.TestEntity; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface TestEntityRepository extends CrudRepository { - List findByTestName(String lastName); - - TestEntity findById(int id); -} - -``` - -#### 3.创建应用程序类: -```java -package com.oceanbase.samples; - -import com.oceanbase.samples.entity.TestEntity; -import com.oceanbase.samples.repository.TestEntityRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class HelloOceanBaseBootApplication { - - public static void main( String[] args ) { - SpringApplication.run(HelloOceanBaseBootApplication.class, args); - } - -} - -``` - -#### 4.在 HelloOceanBaseBootApplication 创建测试: -```java -package com.oceanbase.samples; - -import com.oceanbase.samples.entity.TestEntity; -import com.oceanbase.samples.repository.TestEntityRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class HelloOceanBaseBootApplication { - - private static final Logger log = LoggerFactory.getLogger(HelloOceanBaseBootApplication.class); - - public static void main( String[] args ) - { - SpringApplication.run(HelloOceanBaseBootApplication.class, args); - } - - @Bean - public CommandLineRunner demo(TestEntityRepository repository) { - return (args) -> { - // save a few test entities - repository.save(new TestEntity("Hello OceanBase")); - repository.save(new TestEntity("OceanBase is a distributed database")); - - // fetch all test entities - log.info("Test entities found with findAll():"); - log.info("-------------------------------"); - for (TestEntity entity : repository.findAll()) { - log.info(entity.toString()); - } - log.info(""); - - // fetch an individual test entity by ID - TestEntity entity = repository.findById(1); - log.info("Test entity found with findById(1):"); - log.info("--------------------------------"); - log.info(entity.toString()); - log.info(""); - - // fetch test entities by test name - log.info("Test entity found with findByTestName('OceanBase'):"); - log.info("--------------------------------------------"); - repository.findByTestName("OceanBase").forEach(oceanBase -> { - log.info(oceanBase.toString()); - }); - log.info(""); - }; - } - -} - -``` - - -修改代码中的连接信息,之后你就可以直接使用 run.sh 运行示例代码。 - -```bash -sh run.sh -``` diff --git a/java/oceanbase-spring-boot/README.md b/java/oceanbase-spring-boot/README.md deleted file mode 100644 index ce1857e..0000000 --- a/java/oceanbase-spring-boot/README.md +++ /dev/null @@ -1,239 +0,0 @@ -# Guide to Connecting Spring Boot to OceanBase (Using Spring Data JPA) - -English | [简体中文](README-CN.md) - -This document introduces how to connect to the OceanBase database through Spring's official Spring Data JPA. -Since OceanBase supports MySQL mode and Oracle mode, you can use the MySQL driver to connect to OceanBase. - -## Quick Start - -### First, add the Spring Boot, Spring Data JPA, and MySQL driver dependencies to your Maven project. - -```xml - - org.springframework.boot - spring-boot-starter-parent - 2.7.16 - - - - org.springframework.boot - spring-boot-starter - - - mysql - mysql-connector-java - 8.0.25 - - - org.springframework.boot - spring-boot-starter-data-jpa - - -``` - -### Add the database connection information to the Spring Boot configuration file. - -```yaml -server: - port: 8081 -spring: - jpa: - database: mysql - show-sql: true - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://host:port/test?characterEncoding=UTF-8 - username: ***** - password: ***** -#spring.jpa.hibernate.ddl-auto=update -jackson: - serialization: - indent_output: true -``` - -### Next, refer to the [Spring Data JPA quick start example](https://spring.io/guides/gs/accessing-data-jpa) to write a demo: - -#### 1.Define a simple entity: -```java -package com.oceanbase.samples.entity; - -import javax.persistence.*; -import java.io.Serializable; -import java.util.Objects; - -/* - * CREATE TABLE TEST(id integer, name varchar2(50)) - * - */ -@Entity -@Table( name = "test" ) -public class TestEntity implements Serializable { - - private static final long serialVersionUID = -6578740021873269176L; - - @Id - // @GeneratedValue(strategy=GenerationType.AUTO) //oracle 没有自增策略,添加该注解可以自动生成一个序列,提供自增主键,若数据库已有相关序列,可以忽 //略该注解。 - @Column(name = "id") - private Integer testId; - - @Column( name = "name" ) - private String TestName; - - - - public TestEntity(){ - - } - - public TestEntity(String bauer) { - this.TestName = bauer; - } - - - public Integer getTestId() { - return testId; - } - - public void setTestId(Integer testId) { - this.testId = testId; - } - - public String getTestName() { - return TestName; - } - - public void setTestName(String testName) { - TestName = testName; - } - - @Override - public String toString() { - return "TestEntity{" + - "testId=" + testId + - ", TestName='" + TestName + '\'' + - '}'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TestEntity that = (TestEntity) o; - return Objects.equals(testId, that.testId) && Objects.equals(TestName, that.TestName); - } - - @Override - public int hashCode() { - return Objects.hash(testId, TestName); - } -} - -``` -#### 2.Create a simple query: -```java -package com.oceanbase.samples.repository; - -import com.oceanbase.samples.entity.TestEntity; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface TestEntityRepository extends CrudRepository { - List findByTestName(String lastName); - - TestEntity findById(int id); -} - -``` - -#### 3.Create an application class: -```java -package com.oceanbase.samples; - -import com.oceanbase.samples.entity.TestEntity; -import com.oceanbase.samples.repository.TestEntityRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class HelloOceanBaseBootApplication { - - public static void main( String[] args ) { - SpringApplication.run(HelloOceanBaseBootApplication.class, args); - } - -} - -``` - -#### 4.Create a test in HelloOceanBaseBootApplication: -```java -package com.oceanbase.samples; - -import com.oceanbase.samples.entity.TestEntity; -import com.oceanbase.samples.repository.TestEntityRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class HelloOceanBaseBootApplication { - - private static final Logger log = LoggerFactory.getLogger(HelloOceanBaseBootApplication.class); - - public static void main( String[] args ) - { - SpringApplication.run(HelloOceanBaseBootApplication.class, args); - } - - @Bean - public CommandLineRunner demo(TestEntityRepository repository) { - return (args) -> { - // save a few test entities - repository.save(new TestEntity("Hello OceanBase")); - repository.save(new TestEntity("OceanBase is a distributed database")); - - // fetch all test entities - log.info("Test entities found with findAll():"); - log.info("-------------------------------"); - for (TestEntity entity : repository.findAll()) { - log.info(entity.toString()); - } - log.info(""); - - // fetch an individual test entity by ID - TestEntity entity = repository.findById(1); - log.info("Test entity found with findById(1):"); - log.info("--------------------------------"); - log.info(entity.toString()); - log.info(""); - - // fetch test entities by test name - log.info("Test entity found with findByTestName('OceanBase'):"); - log.info("--------------------------------------------"); - repository.findByTestName("OceanBase").forEach(oceanBase -> { - log.info(oceanBase.toString()); - }); - log.info(""); - }; - } - -} - -``` - -Modify the connection info in code, and use `run.sh` to run the example code. - -```bash -sh run.sh -``` diff --git a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java deleted file mode 100644 index 3409c1b..0000000 --- a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.oceanbase.samples; - -import com.oceanbase.samples.entity.TestEntity; -import com.oceanbase.samples.repository.TestEntityRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class HelloOceanBaseBootApplication { - - private static final Logger log = LoggerFactory.getLogger(HelloOceanBaseBootApplication.class); - - public static void main( String[] args ) - { - SpringApplication.run(HelloOceanBaseBootApplication.class, args); - } - - @Bean - public CommandLineRunner demo(TestEntityRepository repository) { - return (args) -> { - // save a few test entities - repository.save(new TestEntity("Hello OceanBase")); - repository.save(new TestEntity("OceanBase is a distributed database")); - - // fetch all test entities - log.info("Test entities found with findAll():"); - log.info("-------------------------------"); - for (TestEntity entity : repository.findAll()) { - log.info(entity.toString()); - } - log.info(""); - - // fetch an individual test entity by ID - TestEntity entity = repository.findById(1); - log.info("Test entity found with findById(1):"); - log.info("--------------------------------"); - log.info(entity.toString()); - log.info(""); - - // fetch test entities by test name - log.info("Test entity found with findByTestName('OceanBase'):"); - log.info("--------------------------------------------"); - repository.findByTestName("OceanBase").forEach(oceanBase -> { - log.info(oceanBase.toString()); - }); - log.info(""); - }; - } - -}