SpringBoot整合华为GaussDB企业版的实现
作者:ls65535
本文详细介绍了Spring Boot整合华为GaussDB企业版的完整实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
非常好!下面是一个基于 Spring Boot + MyBatis-Plus 3.5.7 + PageHelper 分页 + 华为 GaussDB(Zenith) 的完整示例项目结构,包含:
- Maven 依赖
 application.yml- MyBatis-Plus 和 PageHelper 分页共存配置
 - 完整的实体、Mapper、Service、Controller
 - 分页接口示例
 
✅ 项目结构
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 数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.20</version>
    </dependency>
    <!-- 华为 GaussDB JDBC 驱动 -->
    <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 方言
✅ 实体类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);
    }
}
✅ 数据库建表示例
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);
✅ 启动访问测试
➤ MyBatis-Plus 分页接口:
http://localhost:8080/api/users/plus?pageNum=1&pageSize=2
➤ PageHelper 分页接口:
http://localhost:8080/api/users/pagehelper?pageNum=1&pageSize=2
🚀 🔥 总结
| 分页插件 | 优点 | 缺点 | 
|---|---|---|
| MyBatis-Plus | 集成度高,原生支持,语法优雅 | 只能用于 MyBatis-Plus API | 
| PageHelper | 通用分页工具,支持 MyBatis 和注解 | 依赖 ThreadLocal,容易误用 | 
✅ PageHelper 的application.yml配置示例
pagehelper: helperDialect: postgresql # GaussDB 企业版使用 postgresql 方言 reasonable: true # 分页合理化:页码<=0 时查询第一页,页码>=总页数时查询最后一页 supportMethodsArguments: true # 支持通过 Mapper 接口参数传递分页参数 params: count=countSql # 查询总数的参数 rowBoundsWithCount: true # 使用 RowBounds 分页时同时进行 count 查询
🔧 各参数含义
| 配置项 | 默认值 | 说明 | 
|---|---|---|
| helperDialect | mysql | 数据库方言(MySQL、MariaDB、Oracle、PostgreSQL、SQLServer、H2、sqlite、GaussDB 用 postgresql) | 
| reasonable | false | 合理化分页,页码<=0 查询第一页,页码>=总页数查询最后一页 | 
| supportMethodsArguments | false | 支持通过方法参数传递 pageNum 和 pageSize | 
| params | count 查询的映射参数,比如 count=countSql | |
| rowBoundsWithCount | false | 是否支持 RowBounds 进行 count 查询 | 
🚀 完整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
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);
🔥 结论
- ✅ 配置简单,推荐使用 YAML 进行集中配置。
 - ✅ GaussDB 企业版直接使用 
helperDialect: postgresql。 - ✅ 对于 Spring Boot,PageHelper 会自动根据 
application.yml完成初始化,无需额外@Bean。 
到此这篇关于SpringBoot整合华为GaussDB企业版的实现的文章就介绍到这了,更多相关SpringBoot整合华为GaussDB内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
