摘要:本文就使用springboot結合mybatis plus在專案中實作對GaussDB(DWS)的增刪改查操作,
本文分享自華為云社區《基于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、記憶體、存盤等系統資源的邏輯節點組成,在這樣的系統架構中,業務資料被分散存盤在多個節點上,資料分析任務被推送到資料所在位置就近執行,并行地完成大規模的資料處理作業,實作對資料處理的快速回應,

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應用程式,

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專案,

2.添加依賴JDBC API和SpringWeb

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相同,、

4.jar包上滑鼠點擊右鍵,點擊Add as Library,

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

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(); }

12.撰寫代碼測驗使用mybatis plus實作增刪改查,
@Autowired CustomerMapper customerMapper; @Test void testMybatis(){ //增加 customer cus=new customer(); cus.setcCustomerName("zzzili"); cus.setcCustomerSk(123456); 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框架中利用mybatis ORM框架對GaussDB(DWS)的增刪改查(ARUD)操作,在專案開發中更具有實用性,本示例專案已上傳至附件,
附件:測驗專案代碼.rar9.50MB
點擊關注,第一時間了解華為云新鮮技術~
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/544904.html
標籤:其他
上一篇:搜索EE場景排序鏈路升級
