From 29683fff01866a4a11c34e5b5ffaf2336538aeeb Mon Sep 17 00:00:00 2001 From: BrucePang Date: Sat, 27 Apr 2024 01:34:44 +0800 Subject: [PATCH 1/3] feat: Add a demo of SpringBoot #10 --- java/oceanbase-spring-boot/README-CN.md | 238 +++++++++++++++++ java/oceanbase-spring-boot/README.md | 239 ++++++++++++++++++ java/oceanbase-spring-boot/pom.xml | 59 +++++ java/oceanbase-spring-boot/run.sh | 2 + .../HelloOceanBaseBootApplication.java | 54 ++++ .../oceanbase/samples/entity/TestEntity.java | 73 ++++++ .../repository/TestEntityRepository.java | 14 + .../src/main/resources/application.yml | 16 ++ 8 files changed, 695 insertions(+) create mode 100644 java/oceanbase-spring-boot/README-CN.md create mode 100644 java/oceanbase-spring-boot/README.md create mode 100644 java/oceanbase-spring-boot/pom.xml create mode 100644 java/oceanbase-spring-boot/run.sh create mode 100644 java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java create mode 100644 java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/entity/TestEntity.java create mode 100644 java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java create mode 100644 java/oceanbase-spring-boot/src/main/resources/application.yml 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 + From ffe2ec60193dadbe4edec9c9876a6b6b29438f91 Mon Sep 17 00:00:00 2001 From: BrucePang Date: Sat, 27 Apr 2024 02:53:56 +0800 Subject: [PATCH 2/3] feat: Add a demo of SpringBoot #10, Refer to `Other Information` to provide demo --- .../README-CN.md | 279 +++++++++++++++++ .../oceanbase-spring-boot-mysql-jpa/README.md | 281 ++++++++++++++++++ .../pom.xml | 40 ++- .../run.sh | 0 .../HelloOceanBaseBootApplication.java | 19 ++ .../samples/controller/TestController.java | 67 +++++ .../oceanbase/samples/entity/TestEntity.java | 0 .../repository/TestEntityRepository.java | 2 + .../src/main/resources/application.yml | 8 +- java/oceanbase-spring-boot/README-CN.md | 238 --------------- java/oceanbase-spring-boot/README.md | 239 --------------- .../HelloOceanBaseBootApplication.java | 54 ---- 12 files changed, 669 insertions(+), 558 deletions(-) create mode 100644 java/oceanbase-spring-boot-mysql-jpa/README-CN.md create mode 100644 java/oceanbase-spring-boot-mysql-jpa/README.md rename java/{oceanbase-spring-boot => oceanbase-spring-boot-mysql-jpa}/pom.xml (62%) rename java/{oceanbase-spring-boot => oceanbase-spring-boot-mysql-jpa}/run.sh (100%) create mode 100644 java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java create mode 100644 java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/controller/TestController.java rename java/{oceanbase-spring-boot => oceanbase-spring-boot-mysql-jpa}/src/main/java/com/oceanbase/samples/entity/TestEntity.java (100%) rename java/{oceanbase-spring-boot => oceanbase-spring-boot-mysql-jpa}/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java (86%) rename java/{oceanbase-spring-boot => oceanbase-spring-boot-mysql-jpa}/src/main/resources/application.yml (54%) delete mode 100644 java/oceanbase-spring-boot/README-CN.md delete mode 100644 java/oceanbase-spring-boot/README.md delete mode 100644 java/oceanbase-spring-boot/src/main/java/com/oceanbase/samples/HelloOceanBaseBootApplication.java 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(""); - }; - } - -} From 010163f0c8805802f8c3227a020c43a03615582f Mon Sep 17 00:00:00 2001 From: BrucePang Date: Sat, 27 Apr 2024 13:54:06 +0800 Subject: [PATCH 3/3] feat: Add a demo of MyBatis working with OceanBase. --- java/oceanbase-mybatis/README-CN.md | 245 ++++++++++++++++++ java/oceanbase-mybatis/README.md | 245 ++++++++++++++++++ java/oceanbase-mybatis/pom.xml | 39 +++ java/oceanbase-mybatis/run.sh | 2 + .../samples/OceanBaseMyBatisTest.java | 93 +++++++ .../com/oceanbase/samples/entity/User.java | 49 ++++ .../oceanbase/samples/mapper/UserMapper.java | 4 + .../src/main/resources/UserMapper.xml | 26 ++ .../src/main/resources/mybatis-config.xml | 22 ++ 9 files changed, 725 insertions(+) create mode 100644 java/oceanbase-mybatis/README-CN.md create mode 100644 java/oceanbase-mybatis/README.md create mode 100644 java/oceanbase-mybatis/pom.xml create mode 100644 java/oceanbase-mybatis/run.sh create mode 100644 java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/OceanBaseMyBatisTest.java create mode 100644 java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/entity/User.java create mode 100644 java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/mapper/UserMapper.java create mode 100644 java/oceanbase-mybatis/src/main/resources/UserMapper.xml create mode 100644 java/oceanbase-mybatis/src/main/resources/mybatis-config.xml diff --git a/java/oceanbase-mybatis/README-CN.md b/java/oceanbase-mybatis/README-CN.md new file mode 100644 index 0000000..71bc501 --- /dev/null +++ b/java/oceanbase-mybatis/README-CN.md @@ -0,0 +1,245 @@ +# Spring Boot 连接 OceanBase 指南(使用 Spring Data JPA) + +[English](README.md) | 简体中文 + +本文介绍如何通过 MyBatis 连接 OceanBase 数据库。 +由于 OceanBase 支持 MySQL 模式与 Oracle 模式,因此可以使用 MySQL 驱动连接 OceanBase。 +## 快速开始 + +1.在 pom.xml 中首先加入 MySQL 以及 MyBatis 驱动,pom.xml 参考[OceanBase MyBatis 连接示例](https://www.oceanbase.com/docs/community-observer-cn-10000000000900919) 示例。 + +```xml + + + mysql + mysql-connector-java + 8.0.25 + + + org.mybatis + mybatis + 3.5.4 + + +``` + + 2.在 src/main/resources 文件夹新建 mybatis-config.xml ,并修改数据库连接信息,指定 mapper.xml 路径。 + + mybatis-config.xml +```xml + + + + + + + + + + + + + + + + + + + + +``` +3.新建 实体类,以及对应的 Mapper.xml 文件。 本次示例中,我们创建一个 User 实体类。 +```java +package com.oceanbase.samples.entity; + +import java.util.Objects; + +public class User { + private Integer id; + private String name; + + public User() { + } + + public User(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(name, user.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} +``` +4.新建 UserMapper 接口,用于与 UserMapper关联相应的增删改查操作 +```java +package com.oceanbase.samples.mapper; + +public interface UserMapper { +} +``` + +5.新建 UserMapper.xml ,并绑定 UserMapper 接口, 添加相应的增删改查操作。 +```xml + + + + + + + + + + delete from user where id = #{id}; + + + + insert into user (name) values (#{name}); + + + + update user set name = #{name} where id = #{id}; + + +``` + +6.测试用例,用于测试 UserMapper.xml 中的增删改查操作。 +```java +package com.oceanbase.samples; + + +import com.oceanbase.samples.entity.User; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + + +public class OceanBaseMyBatisTest +{ + + public static void main( String[] args ) + { + SqlSession sqlSession = null; + try { + // Get SqlSessionFactoryBuilder + SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); + // Load mybatis-config.xml as InputStream + InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); + // Get SqlSessionFactory + SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); + // Get SqlSession + sqlSession = sqlSessionFactory.openSession(); + // Execute SQL + // insertTest(sqlSession); + // updateTest(sqlSession); + // selectTest(sqlSession); + // selectWithPagination(sqlSession, 0, 10); + deleteTest(sqlSession); + // Commit Transaction + sqlSession.commit(); + } catch (IOException e) { + // Rollback Transaction + if (sqlSession != null) { + sqlSession.rollback(); + } + e.printStackTrace(); + } finally { + // Close SqlSession + if (sqlSession != null) { + sqlSession.close(); + } + } + } + + public static void insertTest(SqlSession sqlSession) { + // Insert data + User user = new User(); + user.setName("Tom"); + int count = sqlSession.insert("com.oceanbase.samples.mapper.UserMapper.insert", user); + System.out.println("Insert count: " + count); + + } + + public static void updateTest(SqlSession sqlSession) { + // Update data + User user = new User(); + user.setId(1); + user.setName("Jerry"); + int count = sqlSession.update("com.oceanbase.samples.mapper.UserMapper.update", user); + System.out.println("Update count: " + count); + } + + public static void selectTest(SqlSession sqlSession) { + // Select data + List user = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectUser", 1); + user.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + + public static void selectWithPagination(SqlSession sqlSession, int offset, int pageSize) { + Map params = new HashMap<>(); + params.put("offset", offset); + params.put("pageSize", pageSize); + List users = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectWithPagination", params); + users.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + public static void deleteTest(SqlSession sqlSession) { + // Delete data + int count = sqlSession.delete("com.oceanbase.samples.mapper.UserMapper.delete", 3); + System.out.println("Delete count: " + count); + } +} +``` + + +修改代码中的连接信息,之后你就可以直接使用 run.sh 运行示例代码。 + +```bash +sh run.sh +``` diff --git a/java/oceanbase-mybatis/README.md b/java/oceanbase-mybatis/README.md new file mode 100644 index 0000000..0ff667d --- /dev/null +++ b/java/oceanbase-mybatis/README.md @@ -0,0 +1,245 @@ +# Guide to Connect Spring Boot to OceanBase (Using Spring Data JPA) + +English | [简体中文](README-CN.md) + +This article introduces how to connect to the OceanBase database through MyBatis. +Since OceanBase supports MySQL mode and Oracle mode, the MySQL driver can be used to connect to OceanBase. +## Quick Start + +1. First, add the MySQL and MyBatis drivers to the pom.xml file. Refer to the [OceanBase MyBatis Connection Example](https://www.oceanbase.com/docs/community-observer-cn-10000000000900919) for the pom.xml example. + +```xml + + + mysql + mysql-connector-java + 8.0.25 + + + org.mybatis + mybatis + 3.5.4 + + +``` + +2. Create a new mybatis-config.xml file in the src/main/resources folder, modify the database connection information, and specify the path for the mapper.xml. + + mybatis-config.xml +```xml + + + + + + + + + + + + + + + + + + + + +``` +3. Create a new entity class and the corresponding Mapper.xml file. In this example, we create a User entity class. +```java +package com.oceanbase.samples.entity; + +import java.util.Objects; + +public class User { + private Integer id; + private String name; + + public User() { + } + + public User(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(name, user.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} +``` +4. Create a new UserMapper interface, which will be associated with the UserMapper for the corresponding CRUD operations. +```java +package com.oceanbase.samples.mapper; + +public interface UserMapper { +} +``` + +5. Create a new UserMapper.xml file and bind it to the UserMapper interface, and add the corresponding CRUD operations. +```xml + + + + + + + + + + delete from user where id = #{id}; + + + + insert into user (name) values (#{name}); + + + + update user set name = #{name} where id = #{id}; + + +``` + +6. Test case, used to test the CRUD operations in UserMapper.xml. +```java +package com.oceanbase.samples; + + +import com.oceanbase.samples.entity.User; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + + +public class OceanBaseMyBatisTest +{ + + public static void main( String[] args ) + { + SqlSession sqlSession = null; + try { + // Get SqlSessionFactoryBuilder + SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); + // Load mybatis-config.xml as InputStream + InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); + // Get SqlSessionFactory + SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); + // Get SqlSession + sqlSession = sqlSessionFactory.openSession(); + // Execute SQL + // insertTest(sqlSession); + // updateTest(sqlSession); + // selectTest(sqlSession); + // selectWithPagination(sqlSession, 0, 10); + deleteTest(sqlSession); + // Commit Transaction + sqlSession.commit(); + } catch (IOException e) { + // Rollback Transaction + if (sqlSession != null) { + sqlSession.rollback(); + } + e.printStackTrace(); + } finally { + // Close SqlSession + if (sqlSession != null) { + sqlSession.close(); + } + } + } + + public static void insertTest(SqlSession sqlSession) { + // Insert data + User user = new User(); + user.setName("Tom"); + int count = sqlSession.insert("com.oceanbase.samples.mapper.UserMapper.insert", user); + System.out.println("Insert count: " + count); + + } + + public static void updateTest(SqlSession sqlSession) { + // Update data + User user = new User(); + user.setId(1); + user.setName("Jerry"); + int count = sqlSession.update("com.oceanbase.samples.mapper.UserMapper.update", user); + System.out.println("Update count: " + count); + } + + public static void selectTest(SqlSession sqlSession) { + // Select data + List user = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectUser", 1); + user.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + + public static void selectWithPagination(SqlSession sqlSession, int offset, int pageSize) { + Map params = new HashMap<>(); + params.put("offset", offset); + params.put("pageSize", pageSize); + List users = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectWithPagination", params); + users.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + public static void deleteTest(SqlSession sqlSession) { + // Delete data + int count = sqlSession.delete("com.oceanbase.samples.mapper.UserMapper.delete", 3); + System.out.println("Delete count: " + count); + } +} +``` + + +修改代码中的连接信息,之后你就可以直接使用 run.sh 运行示例代码。 + +```bash +sh run.sh +``` diff --git a/java/oceanbase-mybatis/pom.xml b/java/oceanbase-mybatis/pom.xml new file mode 100644 index 0000000..0a8b55f --- /dev/null +++ b/java/oceanbase-mybatis/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + com.oceanbase.samples + oceanbase-mybatis + 1.0-SNAPSHOT + + oceanbase-mysql + + + UTF-8 + 1.8 + 1.8 + + + + + mysql + mysql-connector-java + 8.0.25 + + + org.mybatis + mybatis + 3.5.4 + + + + + + src/main/resources + + **/**.xml + + + + + diff --git a/java/oceanbase-mybatis/run.sh b/java/oceanbase-mybatis/run.sh new file mode 100644 index 0000000..e8e0fc8 --- /dev/null +++ b/java/oceanbase-mybatis/run.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +mvn clean install exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass=com.oceanbase.samples.OceanBaseMyBatisTest diff --git a/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/OceanBaseMyBatisTest.java b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/OceanBaseMyBatisTest.java new file mode 100644 index 0000000..b1decb9 --- /dev/null +++ b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/OceanBaseMyBatisTest.java @@ -0,0 +1,93 @@ +package com.oceanbase.samples; + + +import com.oceanbase.samples.entity.User; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + + +public class OceanBaseMyBatisTest +{ + + public static void main( String[] args ) + { + SqlSession sqlSession = null; + try { + // Get SqlSessionFactoryBuilder + SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); + // Load mybatis-config.xml as InputStream + InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); + // Get SqlSessionFactory + SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); + // Get SqlSession + sqlSession = sqlSessionFactory.openSession(); + // Execute SQL + // insertTest(sqlSession); + // updateTest(sqlSession); + // selectTest(sqlSession); + // selectWithPagination(sqlSession, 0, 10); + deleteTest(sqlSession); + // Commit Transaction + sqlSession.commit(); + } catch (IOException e) { + // Rollback Transaction + if (sqlSession != null) { + sqlSession.rollback(); + } + e.printStackTrace(); + } finally { + // Close SqlSession + if (sqlSession != null) { + sqlSession.close(); + } + } + } + + public static void insertTest(SqlSession sqlSession) { + // Insert data + User user = new User(); + user.setName("Tom"); + int count = sqlSession.insert("com.oceanbase.samples.mapper.UserMapper.insert", user); + System.out.println("Insert count: " + count); + + } + + public static void updateTest(SqlSession sqlSession) { + // Update data + User user = new User(); + user.setId(1); + user.setName("Jerry"); + int count = sqlSession.update("com.oceanbase.samples.mapper.UserMapper.update", user); + System.out.println("Update count: " + count); + } + + public static void selectTest(SqlSession sqlSession) { + // Select data + List user = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectUser", 1); + user.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + + public static void selectWithPagination(SqlSession sqlSession, int offset, int pageSize) { + Map params = new HashMap<>(); + params.put("offset", offset); + params.put("pageSize", pageSize); + List users = sqlSession.selectList("com.oceanbase.samples.mapper.UserMapper.selectWithPagination", params); + users.stream().filter(Objects::nonNull).forEach(System.out::println); + } + + public static void deleteTest(SqlSession sqlSession) { + // Delete data + int count = sqlSession.delete("com.oceanbase.samples.mapper.UserMapper.delete", 3); + System.out.println("Delete count: " + count); + } +} diff --git a/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/entity/User.java b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/entity/User.java new file mode 100644 index 0000000..b0e6615 --- /dev/null +++ b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/entity/User.java @@ -0,0 +1,49 @@ +package com.oceanbase.samples.entity; + +import java.util.Objects; + +public class User { + private Integer id; + private String name; + + public User() { + } + + public User(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(name, user.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/mapper/UserMapper.java b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/mapper/UserMapper.java new file mode 100644 index 0000000..a8ac176 --- /dev/null +++ b/java/oceanbase-mybatis/src/main/java/com/oceanbase/samples/mapper/UserMapper.java @@ -0,0 +1,4 @@ +package com.oceanbase.samples.mapper; + +public interface UserMapper { +} diff --git a/java/oceanbase-mybatis/src/main/resources/UserMapper.xml b/java/oceanbase-mybatis/src/main/resources/UserMapper.xml new file mode 100644 index 0000000..88bc128 --- /dev/null +++ b/java/oceanbase-mybatis/src/main/resources/UserMapper.xml @@ -0,0 +1,26 @@ + + + + + + + + + + delete from user where id = #{id}; + + + + insert into user (name) values (#{name}); + + + + update user set name = #{name} where id = #{id}; + + diff --git a/java/oceanbase-mybatis/src/main/resources/mybatis-config.xml b/java/oceanbase-mybatis/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..73a487f --- /dev/null +++ b/java/oceanbase-mybatis/src/main/resources/mybatis-config.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + +