From f962966b5e5726c8e44c022b03a3532d01e5ab65 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 8 May 2024 23:20:17 +0800 Subject: [PATCH] [improve] add readme and readme-cn --- java/mybatisplus-java/README-CN.md | 106 ++++++++++++++++++++++++++++ java/mybatisplus-java/README.md | 108 +++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 java/mybatisplus-java/README-CN.md create mode 100644 java/mybatisplus-java/README.md diff --git a/java/mybatisplus-java/README-CN.md b/java/mybatisplus-java/README-CN.md new file mode 100644 index 0000000..6ff1956 --- /dev/null +++ b/java/mybatisplus-java/README-CN.md @@ -0,0 +1,106 @@ +# MybatisPlus Java + +[English](README.md) | 简体中文 + +本文介绍了如何通过 `mybatisplus-java` 启动和测试 OceanBase Docker +容器,更多详细信息可以参见 https://github.com/baomidou/mybatis-plus +以及 https://java.testcontainers.org/modules/databases/oceanbase 。 + +## 快速开始 + +将 OceanBase 驱动、TestContainers OceanBase、MybatisPlusStarter、SpringBootStarter Test 模块添加到 POM。 + +```xml + + + + com.oceanbase + oceanbase-client + 2.4.9 + + + org.testcontainers + oceanbase + 1.19.7 + test + + + com.baomidou + mybatis-plus-boot-starter + 3.5.6 + test + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-testcontainers + test + + + org.testcontainers + junit-jupiter + test + + +``` + +以 [MybatisPlusJavaApplicationTests.java](src/test/java/com/oceanbase/samples/mybatisplusjava/MybatisPlusJavaApplicationTests.java) +代码为例。 + +以下代码不仅实现了`OceanBaseCEContainer`的生命周期管理。 它将在执行任何测试用例之前启动容器实例,并在执行所有测试用例后停止容器,而且还在期间使用 +ScriptUtils.executeSqlScript 执行数据库初始化 SQL。 + +```java + +@SpringBootTest +@Testcontainers +class MybatisPlusJavaApplicationTests { + + @Container + public static OceanBaseCEContainer oceanBaseContainer = new OceanBaseCEContainer(DockerImageName.parse("oceanbase/oceanbase-ce:latest")) + .withEnv("MODE", "slim") + .withEnv("FASTBOOT", "true"); + @Autowired + private PersonMapper personMapper; + + @DynamicPropertySource + static void oceanBaseProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", oceanBaseContainer::getJdbcUrl); + registry.add("spring.datasource.username", oceanBaseContainer::getUsername); + registry.add("spring.datasource.password", oceanBaseContainer::getPassword); + registry.add("spring.datasource.driver-class-name", oceanBaseContainer::getDriverClassName); + } + + @BeforeAll + static void setup(@Autowired DataSource dataSource) throws Exception { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + assertNotNull(jdbcTemplate.getDataSource()); + ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), new ClassPathResource("init.sql")); + } +} +``` + +您可以在测试用例中使用 MybatisPlus 操作 OceanBase 实例,如下所示: + +```java + +@Test +void testSelectList() { + List persons = personMapper.selectList(null); + assertFalse(persons.isEmpty()); +} +``` diff --git a/java/mybatisplus-java/README.md b/java/mybatisplus-java/README.md new file mode 100644 index 0000000..6f8cef9 --- /dev/null +++ b/java/mybatisplus-java/README.md @@ -0,0 +1,108 @@ +# MybatisPlus Java + +English | [简体中文](README-CN.md) + +This document introduces how to start and test the OceanBase Docker container using `mybatisplus-java`. For more +details, please see https://github.com/baomidou/mybatis-plus +and https://java.testcontainers.org/modules/databases/oceanbase. + +## Quick Start + +Add the OceanBase driver, TestContainers OceanBase, MybatisPlusStarter, and SpringBootStarter Test modules to your POM. + +```xml + + + + com.oceanbase + oceanbase-client + 2.4.9 + + + org.testcontainers + oceanbase + 1.19.7 + test + + + com.baomidou + mybatis-plus-boot-starter + 3.5.6 + test + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-testcontainers + test + + + org.testcontainers + junit-jupiter + test + + +``` + +Take the code +in [MybatisPlusJavaApplicationTests.java](src/test/java/com/oceanbase/samples/mybatisplusjava/MybatisPlusJavaApplicationTests.java) +as an example. + +The following code not only implements lifecycle management for the `OceanBaseCEContainer`. It will start the container +instance before executing any test cases and stop it after all test cases are completed, and also uses +ScriptUtils.executeSqlScript to perform database initialization SQL during the period. + +```java + +@SpringBootTest +@Testcontainers +class MybatisPlusJavaApplicationTests { + + @Container + public static OceanBaseCEContainer oceanBaseContainer = new OceanBaseCEContainer(DockerImageName.parse("oceanbase/oceanbase-ce:latest")) + .withEnv("MODE", "slim") + .withEnv("FASTBOOT", "true"); + @Autowired + private PersonMapper personMapper; + + @DynamicPropertySource + static void oceanBaseProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", oceanBaseContainer::getJdbcUrl); + registry.add("spring.datasource.username", oceanBaseContainer::getUsername); + registry.add("spring.datasource.password", oceanBaseContainer::getPassword); + registry.add("spring.datasource.driver-class-name", oceanBaseContainer::getDriverClassName); + } + + @BeforeAll + static void setup(@Autowired DataSource dataSource) throws Exception { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + assertNotNull(jdbcTemplate.getDataSource()); + ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), new ClassPathResource("init.sql")); + } +} +``` + +You can use MybatisPlus to operate the OceanBase instance in your test cases as follows: + +```java + +@Test +void testSelectList() { + List persons = personMapper.selectList(null); + assertFalse(persons.isEmpty()); +} +```