SpringBoot整合華為GaussDB企業(yè)版的實(shí)現(xiàn)
非常好!下面是一個(gè)基于 Spring Boot + MyBatis-Plus 3.5.7 + PageHelper 分頁 + 華為 GaussDB(Zenith) 的完整示例項(xiàng)目結(jié)構(gòu),包含:
- Maven 依賴
application.yml- MyBatis-Plus 和 PageHelper 分頁共存配置
- 完整的實(shí)體、Mapper、Service、Controller
- 分頁接口示例
? 項(xiàng)目結(jié)構(gòu)
src
└── main
├── java
│ └── com.example.gaussdemo
│ ├── GaussDemoApplication.java
│ ├── config
│ │ ├── MybatisPlusConfig.java
│ │ └── PageHelperConfig.java
│ ├── controller
│ │ └── UserController.java
│ ├── entity
│ │ └── User.java
│ ├── mapper
│ │ └── UserMapper.java
│ └── service
│ └── UserService.java
├── resources
│ ├── application.yml
│ └── mapper
│ └── UserMapper.xml
? Maven 依賴pom.xml
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<!-- PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
</dependency>
<!-- Druid 數(shù)據(jù)庫(kù)連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- 華為 GaussDB JDBC 驅(qū)動(dòng) -->
<dependency>
<groupId>com.huawei.gauss</groupId>
<artifactId>zenith-driver</artifactId>
<version>21.0.0</version>
</dependency>
</dependencies>
?application.yml
spring:
datasource:
driver-class-name: com.huawei.gauss.jdbc.ZenithDriver
url: jdbc:zenith:@127.0.0.1:1888
username: gauss_user
password: gauss_password
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.gaussdemo.entity
? MyBatis-Plus 分頁配置MybatisPlusConfig.java
package com.example.gaussdemo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.example.gaussdemo.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
? PageHelper 分頁配置PageHelperConfig.java
package com.example.gaussdemo.config;
import com.github.pagehelper.PageInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class PageHelperConfig {
@Bean
public PageInterceptor pageInterceptor() {
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "postgresql");
properties.setProperty("reasonable", "true");
interceptor.setProperties(properties);
return interceptor;
}
}
?? GaussDB 使用 postgresql 方言
? 實(shí)體類User.java
package com.example.gaussdemo.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
? Mapper 接口UserMapper.java
package com.example.gaussdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.gaussdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User> {
// PageHelper 分頁示例
@Select("SELECT id, name, age FROM demo_schema.users")
List<User> selectAllUsers();
}
? Mapper XML(MyBatis-Plus 不需要也可以,PageHelper 也支持注解)
如果用 XML,路徑 resources/mapper/UserMapper.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.example.gaussdemo.mapper.UserMapper">
</mapper>
? Service 層UserService.java
package com.example.gaussdemo.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.gaussdemo.entity.User;
import com.example.gaussdemo.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserMapper userMapper;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
// MyBatis-Plus 分頁
public Page<User> getUsersByPlus(int pageNum, int pageSize) {
return userMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<>());
}
// PageHelper 分頁
public PageInfo<User> getUsersByPageHelper(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> list = userMapper.selectAllUsers();
return new PageInfo<>(list);
}
}
? Controller 層UserController.java
package com.example.gaussdemo.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.gaussdemo.entity.User;
import com.example.gaussdemo.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
// MyBatis-Plus 分頁接口
@GetMapping("/plus")
public Page<User> getUsersByPlus(@RequestParam int pageNum, @RequestParam int pageSize) {
return userService.getUsersByPlus(pageNum, pageSize);
}
// PageHelper 分頁接口
@GetMapping("/pagehelper")
public PageInfo<User> getUsersByPageHelper(@RequestParam int pageNum, @RequestParam int pageSize) {
return userService.getUsersByPageHelper(pageNum, pageSize);
}
}
? 數(shù)據(jù)庫(kù)建表示例
CREATE SCHEMA demo_schema;
CREATE TABLE demo_schema.users (
id BIGINT PRIMARY KEY,
name VARCHAR(100),
age INT
);
INSERT INTO demo_schema.users (id, name, age) VALUES
(1, 'Alice', 30),
(2, 'Bob', 25),
(3, 'Charlie', 28),
(4, 'David', 35);
? 啟動(dòng)訪問測(cè)試
? MyBatis-Plus 分頁接口:
http://localhost:8080/api/users/plus?pageNum=1&pageSize=2
? PageHelper 分頁接口:
http://localhost:8080/api/users/pagehelper?pageNum=1&pageSize=2
?? ?? 總結(jié)
| 分頁插件 | 優(yōu)點(diǎn) | 缺點(diǎn) |
|---|---|---|
| MyBatis-Plus | 集成度高,原生支持,語法優(yōu)雅 | 只能用于 MyBatis-Plus API |
| PageHelper | 通用分頁工具,支持 MyBatis 和注解 | 依賴 ThreadLocal,容易誤用 |
? PageHelper 的application.yml配置示例
pagehelper: helperDialect: postgresql # GaussDB 企業(yè)版使用 postgresql 方言 reasonable: true # 分頁合理化:頁碼<=0 時(shí)查詢第一頁,頁碼>=總頁數(shù)時(shí)查詢最后一頁 supportMethodsArguments: true # 支持通過 Mapper 接口參數(shù)傳遞分頁參數(shù) params: count=countSql # 查詢總數(shù)的參數(shù) rowBoundsWithCount: true # 使用 RowBounds 分頁時(shí)同時(shí)進(jìn)行 count 查詢
?? 各參數(shù)含義
| 配置項(xiàng) | 默認(rèn)值 | 說明 |
|---|---|---|
| helperDialect | mysql | 數(shù)據(jù)庫(kù)方言(MySQL、MariaDB、Oracle、PostgreSQL、SQLServer、H2、sqlite、GaussDB 用 postgresql) |
| reasonable | false | 合理化分頁,頁碼<=0 查詢第一頁,頁碼>=總頁數(shù)查詢最后一頁 |
| supportMethodsArguments | false | 支持通過方法參數(shù)傳遞 pageNum 和 pageSize |
| params | count 查詢的映射參數(shù),比如 count=countSql | |
| rowBoundsWithCount | false | 是否支持 RowBounds 進(jìn)行 count 查詢 |
?? 完整application.yml示例(包含數(shù)據(jù)源)
spring:
datasource:
driver-class-name: com.huawei.gauss.jdbc.ZenithDriver
url: jdbc:zenith:@127.0.0.1:1888
username: gauss_user
password: gauss_password
type: com.alibaba.druid.pool.DruidDataSource
pagehelper:
helperDialect: postgresql
reasonable: true
supportMethodsArguments: true
params: count=countSql
rowBoundsWithCount: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.gaussdemo.entity
? 使用示例(無需額外配置)
PageHelper.startPage(1, 5); List<User> users = userMapper.selectAllUsers(); PageInfo<User> pageInfo = new PageInfo<>(users);
?? 結(jié)論
- ? 配置簡(jiǎn)單,推薦使用 YAML 進(jìn)行集中配置。
- ? GaussDB 企業(yè)版直接使用
helperDialect: postgresql。 - ? 對(duì)于 Spring Boot,PageHelper 會(huì)自動(dòng)根據(jù)
application.yml完成初始化,無需額外@Bean。
到此這篇關(guān)于SpringBoot整合華為GaussDB企業(yè)版的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合華為GaussDB內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫(kù)分表功能
- springboot Mongodb的集成與使用實(shí)例詳解
- SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫(kù)分表方式
- SpringBoot?集成MongoDB實(shí)現(xiàn)文件上傳功能
- SpringBoot集成Sharding Jdbc使用復(fù)合分片的實(shí)踐
- SpringBoot集成Access?DB實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入和解析
- SpringBoot集成MongoDB的實(shí)現(xiàn)
- Spring Boot集成mongodb數(shù)據(jù)庫(kù)過程解析
相關(guān)文章
Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法
今天小編就為大家分享一篇關(guān)于Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
Java發(fā)送報(bào)文與接收?qǐng)?bào)文的實(shí)例代碼
這篇文章主要介紹了Java發(fā)送報(bào)文與接收?qǐng)?bào)文,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Spring Boot 多環(huán)境配置Maven Profile vs 啟
本文介紹了在Java項(xiàng)目開發(fā)和部署中使用多環(huán)境配置的方法,比較了兩種主要方式:MavenProfile方式和啟動(dòng)參數(shù)方式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-12-12
linux配置jdk環(huán)境變量簡(jiǎn)單教程
這篇文章主要為大家詳細(xì)介紹了linux配置jdk環(huán)境變量簡(jiǎn)單教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Spring?Security?自定義授權(quán)服務(wù)器實(shí)踐記錄
授權(quán)服務(wù)器(Authorization Server)目前并沒有集成在Spring Security項(xiàng)目中,而是作為獨(dú)立項(xiàng)目存在于Spring生態(tài)中,這篇文章主要介紹了Spring?Security?自定義授權(quán)服務(wù)器實(shí)踐,需要的朋友可以參考下2022-08-08
mybatis update set 多個(gè)字段實(shí)例
這篇文章主要介紹了mybatis update set 多個(gè)字段實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
springcloud引入spring-cloud-starter-openfeign失敗的解決
這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java?實(shí)現(xiàn)訂單未支付超時(shí)自動(dòng)取消功能(京東商城為例)
本文以京東網(wǎng)上商城為例,給大家介紹商品在下單后沒有支付的情況下,超時(shí)自動(dòng)取消功能,超過24小時(shí),就會(huì)自動(dòng)取消訂單,下面使用 Java 定時(shí)器實(shí)現(xiàn)超時(shí)取消訂單功能,感興趣的朋友一起看看吧2022-01-01

