Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add mybatis-plus samples #37

Merged
merged 11 commits into from
May 11, 2024
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
language: 'python'
- name: 'mysql-connector-python'
language: 'python'
- name: 'mybatisplus-java'
language: 'java'
with_oceanbase_container: false
uses: ./.github/workflows/basic-ci.yml
with:
module: ${{ matrix.module.name }}
Expand Down
108 changes: 108 additions & 0 deletions java/mybatisplus-java/README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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

<dependencies>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oceanbase</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
</dependencies>
```

以 [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<Person> persons = personMapper.selectList(null);
assertFalse(persons.isEmpty());
}
```
110 changes: 110 additions & 0 deletions java/mybatisplus-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# 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

<dependencies>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oceanbase</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
</dependencies>
```

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<Person> persons = personMapper.selectList(null);
assertFalse(persons.isEmpty());
}
```
80 changes: 80 additions & 0 deletions java/mybatisplus-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/>
</parent>
<groupId>com.oceanbase.samples</groupId>
<artifactId>mybatisplus-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplus-java</name>
<description>mybatisplus-java</description>
<dependencies>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oceanbase</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
2 changes: 2 additions & 0 deletions java/mybatisplus-java/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
mvn test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.oceanbase.samples.mybatisplusjava;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MybatisPlusJavaApplication {

public static void main(String[] args) {
SpringApplication.run(MybatisPlusJavaApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.oceanbase.samples.mybatisplusjava;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.oceanbase.OceanBaseCEContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@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"));
}

@Test
void testSelectList() {
List<Person> persons = personMapper.selectList(null);
assertFalse(persons.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.oceanbase.samples.mybatisplusjava;

import lombok.Data;

@Data
public class Person {
private Integer id;
private String name;
private Integer age;
}
Loading
Loading