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 @@ + + + + + + + + + + + + + + + + + + + + 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-mysql-jpa/pom.xml b/java/oceanbase-spring-boot-mysql-jpa/pom.xml new file mode 100644 index 0000000..a37aaab --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/pom.xml @@ -0,0 +1,53 @@ + + 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 + + + diff --git a/java/oceanbase-spring-boot-mysql-jpa/run.sh b/java/oceanbase-spring-boot-mysql-jpa/run.sh new file mode 100644 index 0000000..c8e48d8 --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/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-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-mysql-jpa/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 new file mode 100644 index 0000000..454f890 --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/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-mysql-jpa/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 new file mode 100644 index 0000000..5b5d2a5 --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/java/com/oceanbase/samples/repository/TestEntityRepository.java @@ -0,0 +1,16 @@ +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); +} diff --git a/java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml b/java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml new file mode 100644 index 0000000..b7e199f --- /dev/null +++ b/java/oceanbase-spring-boot-mysql-jpa/src/main/resources/application.yml @@ -0,0 +1,16 @@ +server: + port: 8081 +spring: + jpa: + database: mysql # mysql, oracle + show-sql: true + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://host:port/dbname?characterEncoding=UTF-8 + username: ***** # your username + password: ***** # your password +#spring.jpa.hibernate.ddl-auto=update +jackson: + serialization: + indent_output: true +