基于梧桐数据库的 Spring Boot 应用开发实战

基于梧桐数据库的 Spring Boot 应用开发实战

一、项目背景

随着业务规模的不断扩大,数据量的增长对数据库的性能提出了更高的要求。为了满足高并发、大数据量的需求,我们决定采用梧桐数据库,并结合 Spring Boot 框架快速构建一个稳定高效的应用系统。

本文编写关于如何在 Spring Boot 环境下连接梧桐数据的指引文档。

二、环境配置

  • Java版本:OpenJDK 1.8
  • Spring Boot版本:2.7.18
  • 梧桐数据库版本:WuTongDB 1.x
  • 构建工具:Maven

三、技术栈介绍

梧桐数据库

梧桐数据库是由中移动信息技术有限公司(中国移动集团大数据中心)打造的新一代云原生分布式数据库,能够同时支持公有云与私有云。该产品采用存储和计算分离的架构,具有 MPP 的所有优点,服务层、计算层、存储层均可弹性扩展,支持混合工作负载并具备高扩展性。

Spring Boot

Spring Boot 是一个简化新 Spring 应用的初始搭建以及开发过程的框架。它默认配置了许多框架的使用方式,从而能够让我们不用花费太多时间在解决各种框架集成的难题上。

四、开发步骤

4.1 测试数据准备

postgresql 复制代码
-- DDL,schema_name按实际填写
CREATE TABLE schema_name.test_table(
  table_id text, 
  type text,
  end_time text,
  start_time text  )
FORMAT 'MAGMAAP' TABLESPACE magma_default01;

-- INSERT
INSERT INTO schema_name.test_table (table_id, type, end_time, start_time)
VALUES ('test_id_001', 'A', '2024-07-30 12:00:00', '2024-07-30 09:00:00');
INSERT INTO schema_name.test_table (table_id, type, end_time, start_time)
VALUES ('test_id_002', 'B', '2024-07-30 13:00:00', '2024-07-30 10:00:00');
INSERT INTO schema_name.test_table (table_id, type, end_time, start_time)
VALUES ('test_id_003', 'C', '2024-07-30 14:00:00', '2024-07-30 11:00:00');

4.2 创建 Spring Boot 项目

通过 Spring Initializr 创建一个基本的 Spring Boot 项目,并添加必要的依赖,如 Spring Web , 持久层框架 MyBatis-Plus 和用于连接梧桐数据库的驱动。

从梧桐侧获取驱动jar包:wutong-database-6.0.0.0-SNAPSHOT.jar,并放置于 E:\db 目录下。将 jar 注册到 maven 本地库:

ini 复制代码
mvn install:install-file -Dfile=E:\db\wutong-database-6.0.0.0-SNAPSHOT.jar -DgroupId=com.wutong -DartifactId=wutong -Dversion=6.0 -Dpackaging=jar

驱动注册完成后,我们在 maven 依赖加入以下内容:

xml 复制代码
        <!-- Spring Boot相关依赖 -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.18</version>
            <scope>test</scope>
        </dependency>
        <!-- bootstrap.yml支持依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
		<!-- MyBatis-Plus 相关依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <!-- 数据源切换工具 dynamic-datasource -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- 梧桐数据库连接驱动 -->
        <dependency>
            <groupId>com.wutong</groupId>
            <artifactId>wutong</artifactId>
            <version>6.0</version>
        </dependency>

4.3 配置数据库连接

bootstrap.yml 文件中配置数据库连接信息,例如:

yaml 复制代码
spring:
    datasource:
        dynamic:
            strict: false
            primary: wutong_ydy_magma
            datasource:
                wutong_magma:
                	# ip,db_name,schema_name按实际填写
                    url: jdbc:wutongdb://ip:5432/db_name?currentSchema=schema_name
                    # username按实际填写
                    username: username
                    # password按实际填写
                    password: password
                    driver-class_name: org.wutong.Driver

4.4 设计实体类

根据业务需求设计实体类,并使用 Mybatis-plus 注解来映射表结构。

java 复制代码
// schema_name按实际替换
@TableName(value ="schema_name.test_table")
public class TestInfo implements Serializable {
    private String tableId;

    private String type;

    private String endTime;

    private String startTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public String getTableId() {
        return tableId;
    }


    public void setTableId(String tableId) {
        this.tableId = tableId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        TestInfo other = (TestInfo) that;
        return (this.getTableId() == null ? other.getTableId() == null : this.getTableId().equals(other.getTableId()))
            && (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType()))
            && (this.getEndTime() == null ? other.getEndTime() == null : this.getEndTime().equals(other.getEndTime()))
            && (this.getStartTime() == null ? other.getStartTime() == null : this.getStartTime().equals(other.getStartTime()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getTableId() == null) ? 0 : getTableId().hashCode());
        result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
        result = prime * result + ((getEndTime() == null) ? 0 : getEndTime().hashCode());
        result = prime * result + ((getStartTime() == null) ? 0 : getStartTime().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", tableId=").append(tableId);
        sb.append(", type=").append(type);
        sb.append(", endTime=").append(endTime);
        sb.append(", startTime=").append(startTime);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

4.5 实现数据访问层

使用 MyBatis Plus 提供的 BaseMapper 接口来实现数据的增删改查操作。

文件内相关路径已做脱敏处理,注意替换。

java 复制代码
@DS("wutong_magma")
public interface TestInfoMapper extends BaseMapper<TestInfo> {}
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 替换成文件实际路径 -->
<mapper namespace="com.**.commons.wutong.mapper.demo.TestInfoMapper">
	<!-- 替换成文件实际路径 -->
    <resultMap id="BaseResultMap" type="com.**.commons.wutong.domain.demo.TestInfo">
            <result property="tableId" column="table_id" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="endTime" column="end_time" jdbcType="VARCHAR"/>
            <result property="startTime" column="start_time" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        table_id,type,end_time,
        start_time
    </sql>
</mapper>

4.6 编写服务层

java 复制代码
public interface TestInfoService extends IService<TestInfo> {
}
java 复制代码
@Service
public class TestInfoServiceImpl extends ServiceImpl<TestInfoMapper, TestInfo>
    implements TestInfoService{
}

4.7 编写控制器

创建 RESTful API,定义 HTTP 请求路径、方法等,处理前端传来的请求并返回相应的响应。

java 复制代码
@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {
    
    @Autowired
    private TestInfoService testInfoService;

    @PostMapping(value = "/get")
    public AjaxResult getModel(@RequestBody TestReq req){
        QueryWrapper<TestInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TestInfo::getTableId, req.getId())
                .eq(TestInfo::getEndTime, req.getTime());
        TestInfo one = testInfoService.getOne(queryWrapper,false);
        if (one!=null){
            TestModelResp testModelResp = new TestModelResp();
            testModelResp.setId(one.getTableId());
            testModelResp.setType(one.getType());
            return AjaxResult.success(testModelResp);
        }
        return AjaxResult.error();
    }

}

4.8 部署应用并测试

将应用部署后,请求接口即可获取结果。

五、总结

通过上述步骤,我们成功地构建了一个基于梧桐数据库的 Spring Boot 应用,并通过 API 实现梧桐数据库的数据实时查询。

相关推荐
大熊程序猿17 分钟前
python 读取excel数据存储到mysql
数据库·python·mysql
落落落sss18 分钟前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
jnrjian19 分钟前
Oracle 启动动态采样 自适应执行计划
数据库·oracle
lamb张40 分钟前
MySQL锁
数据库·mysql
ForRunner1231 小时前
使用 Python 高分解决 reCAPTCHA v3 的指南
数据库·python·microsoft
躺平的花卷2 小时前
Python爬虫案例六:抓取某个地区某月份天气数据并保存到mysql数据库中
数据库·爬虫·python·mysql
Flying_Fish_roe2 小时前
linux-安全管理-防火墙与网络安全
linux·数据库·oracle
A_cot3 小时前
Redis 的三个并发问题及解决方案(面试题)
java·开发语言·数据库·redis·mybatis
2401_847056553 小时前
Altium Designer脚本工具定制
网络·数据库
神仙别闹3 小时前
基于Python+SQLite的课程管理系统
数据库·sqlite