diff --git a/java/oceanbase-spring-boot/README-CN.md b/java/oceanbase-spring-boot/README-CN.md new file mode 100644 index 0000000..4d17548 --- /dev/null +++ b/java/oceanbase-spring-boot/README-CN.md @@ -0,0 +1,238 @@ +# 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 new file mode 100644 index 0000000..ce1857e --- /dev/null +++ b/java/oceanbase-spring-boot/README.md @@ -0,0 +1,239 @@ +# 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/pom.xml b/java/oceanbase-spring-boot/pom.xml new file mode 100644 index 0000000..32cf04f --- /dev/null +++ b/java/oceanbase-spring-boot/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + + com.oceanbase.samples + oceanbase-spring-boot + 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.7.16 + + oceanbase-spring-boot + + + UTF-8 + 1.8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + mysql + mysql-connector-java + 8.0.25 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + src/main/java + + **/*.properties + **/*.yml + + + + src/main/resources + + **/*.properties + **/*.yml + + + + + diff --git a/java/oceanbase-spring-boot/run.sh b/java/oceanbase-spring-boot/run.sh new file mode 100644 index 0000000..c8e48d8 --- /dev/null +++ b/java/oceanbase-spring-boot/run.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +mvn clean install exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass=com.oceanbase.samples.HelloOceanBaseBootApplication 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 new file mode 100644 index 0000000..3409c1b --- /dev/null +++ b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java @@ -0,0 +1,54 @@ +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(""); + }; + } + +} diff --git a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java new file mode 100644 index 0000000..454f890 --- /dev/null +++ b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java @@ -0,0 +1,73 @@ +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.IDENTITY) + // @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); + } +} diff --git a/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java new file mode 100644 index 0000000..34cbd04 --- /dev/null +++ b/java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java @@ -0,0 +1,14 @@ +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); +} diff --git a/java/oceanbase-spring-boot/src/main/resources/application.yml b/java/oceanbase-spring-boot/src/main/resources/application.yml new file mode 100644 index 0000000..53a8319 --- /dev/null +++ b/java/oceanbase-spring-boot/src/main/resources/application.yml @@ -0,0 +1,16 @@ +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 +