前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >添加OceanBase-MybatisPlus案例

添加OceanBase-MybatisPlus案例

作者头像
阿超
发布2024-08-23 17:02:46
930
发布2024-08-23 17:02:46
举报
文章被收录于专栏:快乐阿超

多思不若养志,多言不若守静,多才不若蓄德。——曾国藩

我在 ob-samples 添加的 demo 合并了

https://github.com/oceanbase/ob-samples/pull/37

通过 mybatisplus-java 启动和测试 OceanBase Docker 容器,更多详细信息可以参见 https://github.com/baomidou/mybatis-plus 以及 https://java.testcontainers.org/modules/databases/oceanbase

快速开始

将 OceanBase 驱动、TestContainers OceanBase、MybatisPlusStarter、SpringBootStarter Test 模块添加到 POM。

代码语言:javascript
复制
<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 代码为例。

以下代码不仅实现了OceanBaseCEContainer的生命周期管理。 它将在执行任何测试用例之前启动容器实例,并在执行所有测试用例后停止容器,而且还在期间使用 ScriptUtils.executeSqlScript 执行数据库初始化 SQL。

代码语言:javascript
复制
@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 实例,如下所示:

代码语言:javascript
复制
@Test
void testSelectList() {
  List<Person> persons = personMapper.selectList(null);
  assertFalse(persons.isEmpty());
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 快速开始
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档