SpringBoot整合華為GaussDB企業(yè)版的實現(xiàn)
非常好!下面是一個基于 Spring Boot + MyBatis-Plus 3.5.7 + PageHelper 分頁 + 華為 GaussDB(Zenith) 的完整示例項目結(jié)構(gòu),包含:
- Maven 依賴
application.yml- MyBatis-Plus 和 PageHelper 分頁共存配置
- 完整的實體、Mapper、Service、Controller
- 分頁接口示例
? 項目結(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ù)庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- 華為 GaussDB JDBC 驅(qū)動 -->
<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);
}
}
? 數(shù)據(jù)庫建表示例
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
?? ?? 總結(jié)
| 分頁插件 | 優(yōu)點 | 缺點 |
|---|---|---|
| MyBatis-Plus | 集成度高,原生支持,語法優(yōu)雅 | 只能用于 MyBatis-Plus API |
| PageHelper | 通用分頁工具,支持 MyBatis 和注解 | 依賴 ThreadLocal,容易誤用 |
? PageHelper 的application.yml配置示例
pagehelper: helperDialect: postgresql # GaussDB 企業(yè)版使用 postgresql 方言 reasonable: true # 分頁合理化:頁碼<=0 時查詢第一頁,頁碼>=總頁數(shù)時查詢最后一頁 supportMethodsArguments: true # 支持通過 Mapper 接口參數(shù)傳遞分頁參數(shù) params: count=countSql # 查詢總數(shù)的參數(shù) rowBoundsWithCount: true # 使用 RowBounds 分頁時同時進行 count 查詢
?? 各參數(shù)含義
| 配置項 | 默認值 | 說明 |
|---|---|---|
| helperDialect | mysql | 數(shù)據(jù)庫方言(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 進行 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é)論
- ? 配置簡單,推薦使用 YAML 進行集中配置。
- ? GaussDB 企業(yè)版直接使用
helperDialect: postgresql。 - ? 對于 Spring Boot,PageHelper 會自動根據(jù)
application.yml完成初始化,無需額外@Bean。
到此這篇關(guān)于SpringBoot整合華為GaussDB企業(yè)版的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合華為GaussDB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實現(xiàn)分庫分表功能
- springboot Mongodb的集成與使用實例詳解
- SpringBoot集成Sharding-JDBC實現(xiàn)分庫分表方式
- SpringBoot?集成MongoDB實現(xiàn)文件上傳功能
- SpringBoot集成Sharding Jdbc使用復合分片的實踐
- SpringBoot集成Access?DB實現(xiàn)數(shù)據(jù)導入和解析
- SpringBoot集成MongoDB的實現(xiàn)
- Spring Boot集成mongodb數(shù)據(jù)庫過程解析
相關(guān)文章
java實現(xiàn)利用String類的簡單方法讀取xml文件中某個標簽中的內(nèi)容
下面小編就為大家?guī)硪黄猨ava實現(xiàn)利用String類的簡單方法讀取xml文件中某個標簽中的內(nèi)容。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
idea輸入sout無法自動補全System.out.println()的問題
這篇文章主要介紹了idea輸入sout無法自動補全System.out.println()的問題,本文給大家分享解決方案,供大家參考,需要的朋友可以參考下2020-07-07
@PathParam和@QueryParam區(qū)別簡析
這篇文章主要介紹了@PathParam和@QueryParam區(qū)別,分享了相關(guān)實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01

