基于SpringBoot实现操作GaussDB(DWS)的项目实战

基于SpringBoot实现操作GaussDB(DWS)的项目实战数据仓库服务GaussDB 是一种基于华为云基础架构和平台的在线数据处理数据库,提供即开即用、可扩展且完全托管的分析型数据库服务。

大家好,欢迎来到IT知识分享网。

本文分享自华为云社区《基于SpringBoot实现操作GaussDB(DWS)的项目实战【玩转PB级数仓GaussDB(DWS)】-云社区-华为云》,作者:清雨小竹。

GaussDB(DWS)

数据仓库服务GaussDB(DWS) 是一种基于华为云基础架构和平台的在线数据处理数据库,提供即开即用、可扩展且完全托管的分析型数据库服务。GaussDB(DWS)是基于华为融合数据仓库GaussDB产品的云原生服务 ,兼容标准ANSI SQL 99和SQL 2003,同时兼容PostgreSQL/Oracle数据库生态,为各行业PB级海量大数据分析提供有竞争力的解决方案。

GaussDB(DWS) 基于Shared-nothing分布式架构,具备MPP (Massively Parallel Processing)大规模并行处理引擎,由众多拥有独立且互不共享的CPU、内存、存储等系统资源的逻辑节点组成。在这样的系统架构中,业务数据被分散存储在多个节点上,数据分析任务被推送到数据所在位置就近执行,并行地完成大规模的数据处理工作,实现对数据处理的快速响应。

基于SpringBoot实现操作GaussDB(DWS)的项目实战

Spring Boot

Spring Boot是一个构建在Spring框架顶部的项目。它提供了一种简便,快捷的方式来设置,配置和运行基于Web的简单应用程序。它是一个Spring模块,提供了 RAD(快速应用程序开发)功能。它用于创建独立的基于Spring的应用程序,因为它需要最少的Spring配置,因此可以运行。简而言之,Spring Boot是 Spring Framework 和 嵌入式服务器的组合。在Spring Boot不需要XML配置(部署描述符)。它使用约定优于配置软件设计范例,这意味着可以减少开发人员的工作量。我们可以使用Spring STS IDE 或 Spring Initializr 进行开发Spring Boot Java应用程序。

基于SpringBoot实现操作GaussDB(DWS)的项目实战

Mybatis plus(MP)

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集,MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

华为云官方文档给出了使用JDBC连接GaussDB(DWS)并实现增删改查基本操作的教程和代码示例。https://support.huaweicloud.com/devg-dws/dws_04_0085.html,在java开发中springboot作为一个常用的开发框架在很多项目中使用,下面就使用springboot结合mybatis plus在项目中实现对GaussDB(DWS)的增删改查操作。

一、新建springboot项目

1.打开idea基于向导新建springboot项目。

基于SpringBoot实现操作GaussDB(DWS)的项目实战

2.添加依赖JDBC API和SpringWeb

基于SpringBoot实现操作GaussDB(DWS)的项目实战

3.项目新建完成后打开新建libs文件夹,把jdbc驱动复制到libs目录下。https://support.huaweicloud.com/mgtg-dws/dws_01_0032.html

  • gsjdbc4.jar:与PostgreSQL保持兼容,其中类名、类结构与PostgreSQL驱动完全一致,曾经运行于PostgreSQL的应用程序可以直接移植到当前系统中使用。
  • gsjdbc200.jar:如果同一JVM进程内需要同时访问PostgreSQL及GaussDB(DWS) 请使用该驱动包。该包主类名为“com.huawei.gauss200.jdbc.Driver”(即将“org.postgresql”替换为“com.huawei.gauss200.jdbc”) ,数据库连接的URL前缀为“jdbc:gaussdb”,其余与gsjdbc4.jar相同。
基于SpringBoot实现操作GaussDB(DWS)的项目实战

4.jar包上鼠标点击右键,点击Add as Library。

基于SpringBoot实现操作GaussDB(DWS)的项目实战

5.打开build.gradle,添加mybatis plus依赖,由于GaussDB DWS兼容PostgreSQL所以runtimeOnly可以使用org.postgresql:postgresql

dependencies { //mybatis-plus implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.2' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test' }

6.打开application.properties配置数据库源信息。

spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://xx.xx.xx.xx:8000/database?currentSchema=traffic_data spring.datasource.username=dbadmin spring.datasource.password=xxxxxx

二、配置mybatis plus

7.新增数据表

CREATE TABLE "traffic_data"."customer" ( "id" int4, "c_customer_sk" int4, "c_customer_name" varchar(32) );

8.新增包名com.zz.testdws.mapper和com.zz.testdws.entity

基于SpringBoot实现操作GaussDB(DWS)的项目实战

9.新建实体类对象customer.java和Mappder对象CustomerMapper.java文件

package com.zz.testdws.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; /** * <p> * * </p> * * @author zzzili * @since 2023-02-16 */ public class customer { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Integer id; private Integer cCustomerSk; private String cCustomerName; @Override public String toString() { return "customer{" + "id=" + id + ", cCustomerSk=" + cCustomerSk + ", cCustomerName='" + cCustomerName + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getcCustomerSk() { return cCustomerSk; } public void setcCustomerSk(Integer cCustomerSk) { this.cCustomerSk = cCustomerSk; } public String getcCustomerName() { return cCustomerName; } public void setcCustomerName(String cCustomerName) { this.cCustomerName = cCustomerName; } } 
package com.zz.testdws.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zz.testdws.entity.customer; /** * <p> * Mapper 接口 * </p> * * @author zzzili * @since 2023-02-15 */ public interface CustomerMapper extends BaseMapper<customer> { } 

10.打开TestDwsSpringBootApplication.java文件,添加mapper扫描器注解@MapperScan(“com.zz.testdws.mapper”)

package com.zz.testdws; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.zz.testdws.mapper") public class TestDwsSpringBootApplication { public static void main(String[] args) { SpringApplication.run(TestDwsSpringBootApplication.class, args); } } 

三、测试数据

11.打开TestDwsSpringBootApplicationTests.java文件编写代码,测试用sql执行增删改查数据。

 @Autowired DataSource dataSource; @Test void testDoSQL() throws SQLException { System.out.println(dataSource.getClass()); //获取连接 Connection con = dataSource.getConnection(); //调用Connection的createStatement方法创建语句对象 Statement stmt = con.createStatement(); //调用Statement的executeUpdate方法执行SQL语句 //int rc = stmt.executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));"); //System.out.println("rc = " + rc); //int rc = stmt.executeUpdate("INSERT INTO customer_t1(c_customer_sk,c_customer_name) values('1001','zhangsan');"); //System.out.println("insert rc = " + rc); //查询数据 ResultSet rs= stmt.executeQuery("select * from customer_t1"); //遍历数据 while(rs.next()){ String sk = rs.getString("c_customer_sk"); String name = rs.getString("c_customer_name"); System.out.println("sk:"+sk+" 姓名:"+name); } con.close(); }
基于SpringBoot实现操作GaussDB(DWS)的项目实战

12.编写代码测试使用mybatis plus实现增删改查。

 @Autowired CustomerMapper customerMapper; @Test void testMybatis(){ //增加 customer cus=new customer(); cus.setcCustomerName("zzzili"); cus.setcCustomerSk(); cus.setId(8); customerMapper.insert(cus); //改 cus.setcCustomerSk(66666); customerMapper.updateById(cus); //查 List<customer> list = customerMapper.selectList(null); System.out.println("list size="+list.size()); for(customer node :list){ System.out.println(node); } //删除 customerMapper.deleteById(1); }
基于SpringBoot实现操作GaussDB(DWS)的项目实战

基于SpringBoot实现操作GaussDB(DWS)的项目实战

总结:通过以上实验实现了在springboot框架中利用mybatis ORM框架对GaussDB(DWS)的增删改查(ARUD)操作,在项目开发中更具有实用性。本示例项目已上传至附件。

附件:测试项目代码.rar9.50MB→鍗庝负浜戝崥瀹澶ф暟鎹崥瀹AI鍗氬_浜戣绠楀崥瀹寮€鍙戣€呭崥瀹鎶€鏈崥瀹�-鍗庝负浜�

点击下方,第一时间了解华为云新鲜技术~

华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云

#华为云开发者联盟#

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/50563.html

(0)
上一篇 2024-08-22 19:45
下一篇 2024-08-26 16:33

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信