• 作者:老汪软件技巧
  • 发表时间:2024-09-21 07:28
  • 浏览量:

基于梧桐数据库的 Spring Boot 应用开发实战一、项目背景

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

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

二、环境配置三、技术栈介绍梧桐数据库

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

Spring Boot

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

四、开发步骤4.1 测试数据准备

-- 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 本地库:

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 依赖加入以下内容:

        
		<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>2.7.18version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <version>2.7.18version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
		
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.3.1version>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>dynamic-datasource-spring-boot-starterartifactId>
            <version>3.5.2version>
        dependency>
        
        <dependency>
            <groupId>com.wutonggroupId>
            <artifactId>wutongartifactId>
            <version>6.0version>
        dependency>

4.3 配置数据库连接

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

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

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

4.4 设计实体类

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

// 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 接口来实现数据的增删改查操作。

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

@DS("wutong_magma")
public interface TestInfoMapper extends BaseMapper {}

"1.0" encoding="UTF-8"?>
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 编写服务层

public interface TestInfoService extends IService {
}

@Service
public class TestInfoServiceImpl extends ServiceImpl
    implements TestInfoService{
}

4.7 编写控制器

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

@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {
    
    @Autowired
    private TestInfoService testInfoService;
    @PostMapping(value = "/get")
    public AjaxResult getModel(@RequestBody TestReq req){
        QueryWrapper 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 实现梧桐数据库的数据实时查询。


上一条查看详情 +反向传播:神经网络训练的基石
下一条 查看详情 +没有了