对上篇mysql导入到OceanBase中的数据库进行代码测试,写了个demo包含测试方法,在原mysql库中成功执行,迁移到OceanBase时看是否能不修改业务代码而成功执行测试方法:
- 代码基于SpringBoot + MyBastis
- 测试增删改查、批量新增、多表联查、事务
只修改pom依赖和连接配置
<!-- OceanBase驱动包 -->
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.3</version>
</dependency>
# oceanbase连接信息
spring.datasource.driverClassName=com.oceanbase.jdbc.Driver
spring.datasource.url=jdbc:oceanbase://192.168.113.161:2883/employees?useSSL=false&useUnicode=true&characterEncoding=utf-8&useServerPrepStmts=true&rewriteBatchedStatements=true
spring.datasource.username=root@mq_t1#zycluster
spring.datasource.password=/aVi*H8(0%FS_YwZ-|dmo&[hjlT7pe@E
Java测试类代码
package com.zypcy.ob_demo;
import com.alibaba.fastjson2.JSON;
import com.zypcy.ob_demo.dto.EmployeesSalariesDto;
import com.zypcy.ob_demo.entity.Employees;
import com.zypcy.ob_demo.service.EmployeesService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@SpringBootTest
class ObDemoApplicationTests {
@Autowired
EmployeesService employeesService;
@Test
void contextLoads() {
}
public Employees initEmployees(int empNo) {
Employees employees = new Employees();
employees.setEmpNo(empNo);
employees.setBirthDate(new Date());
employees.setFirstName("zhu");
employees.setLastName("yu");
employees.setGender("M");
employees.setHireDate(new Date());
return employees;
}
@Test
public void testEmployees() {
//测试单条数据的插入、更新、删除、查询
Employees employees = initEmployees(500000);
//新增一条记录
boolean flag = employeesService.insert(employees);
Assertions.assertTrue(flag, "插入操作失败");
//查询新增但记录
Employees insertedEmployee = employeesService.selectByPrimaryKey(employees.getEmpNo());
System.out.println("插入后的记录: " + JSON.toJSONString(insertedEmployee));
//更新记录
employees.setLastName("yu2");
flag = employeesService.updateByPrimaryKey(employees);
Assertions.assertTrue(flag, "更新操作失败");
// 查询更新后的记录
Employees employees2 = employeesService.selectByPrimaryKey(employees.getEmpNo());
System.out.println("更新后的记录: " + JSON.toJSONString(employees2));
flag = employeesService.deleteByPrimaryKey(employees.getEmpNo());
Assertions.assertTrue(flag, "删除操作失败");
}
@Test
public void testBatchInsert() {
//测试批量插入,测试三次,看下耗时
int length = 200;
Long d1 = System.currentTimeMillis();
List<Employees> employeesList = new ArrayList<>(length);
for (int i = 1; i <= length; i++) {
Employees model = new Employees();
int empNo = 610000 + i;
model.setEmpNo(empNo);
model.setBirthDate(new Date());
model.setFirstName("zhu" + empNo);
model.setLastName("yu" + empNo);
model.setGender("M");
model.setHireDate(new Date());
employeesList.add(model);
}
boolean flag = employeesService.batchInsert(employeesList);
Assertions.assertTrue(flag);
Long d2 = System.currentTimeMillis();
System.out.println("批量插入耗时=" + (d2 - d1) + "ms");
}
@Test
public void selectEmployeesSalariesByEmpNos() {
//测试多表关联查询
List<EmployeesSalariesDto> list = employeesService.selectEmployeesSalariesByEmpNos(Arrays.asList(10001, 10002, 10003));
System.out.println("list.size=" + list.size());
Assertions.assertTrue(list.size() > 0);
}
@Test
public void testTransaction() {
//测试事务,往3张表分别插入一条记录,在第三张表插入时主动抛出异常,查看前两张表的数据是否回滚
Employees employees = initEmployees(650000);
boolean flag = employeesService.testTransaction(employees);
Assertions.assertTrue(flag);
}
}
以上代码先在mysql环境中全部执行一遍
然后连接OceanBase库,对每个(增、删、改、查、批量新增、多表关联查询、事务)方法进行测试,贴几张截图
下面这张图片时测试事物的,往3张表新增数据(插入一条编号为65000的员工),在第3张表新增前写一个错误来抛出异常,被@Transactional 捕获到而回滚,右边是数据库去查询编号=650000的员工是否存在,验证事务是否回滚了
经测试得出,在mysql中能执行的sql能照常在OceanBase中执行,只要修改OceanBase客户端和数据库连接字符串即可运行,无需修改业务sql代码
源码下载:https://download.csdn.net/download/zhuyu19911016520/88869926
用了Spring @Transactional(rollbackFor = Exception.class)声明式事务,需要依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>