SpringBoot集成瀚高数据库(PostgreSQL)

1. 前言

在现代微服务架构中,Spring Boot因其简洁的配置和快速开发能力而广受欢迎。瀚高数据库作为国产数据库的佼佼者,越来越多地被应用于企业级项目中。本文将指导您如何在Spring Boot项目中集成瀚高数据库,实现高效的数据访问与管理。

2. 添加依赖

<dependency>
  <groupId>com.highgo</groupId>
  <artifactId>HgdbJdbc</artifactId>
  <version>6.2.2</version>
</dependency>

或可以直接添加 PostgreSQL依赖

<!--postgresql-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

2. 配置文件

如果添加的是highgo的依赖,则写为:

spring:
  datasource:
    #驱动类名称
    driver-class-name: com.highgo.jdbc.Driver
    #数据库连接的url
    url: jdbc:highgo://ip:端口号/数据库名?currentSchema=模式名,public&stringtype=unspecified
    #连接数据库的用户名
    username: 账号
    #连接数据库的密码
    password: 密码

 如果添加的是PostgreSQL的依赖,则写为:

spring:
  datasource:
    #驱动类名称
    driver-class-name: org.postgresql.Driver
    #数据库连接的url
    url: jdbc:postgresql://ip:端口号/数据库名?currentSchema=模式名,public&stringtype=unspecified
    #连接数据库的用户名
    username: 账号
    #连接数据库的密码
    password: 密码

例如, 瀚高数据库的ip为localhost,端口为5866,默认数据库为highgo,模式名为test,账号和密码皆为postgres,那么可写为:

spring:
  datasource:
    driver-class-name: com.highgo.jdbc.Driver
    url: jdbc:highgo://localhost:5866/highgo?currentSchema=test,public&stringtype=unspecified
    username: postgres
    password: postgres

或写为:

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5866/highgo?currentSchema=test,public&stringtype=unspecified
    username: postgres
    password: postgres

说明:

  1. 为什么在 currentSchema里加入public数据库,详见解决瀚高数据库(PostgreSQL)中com.highgo.jdbc.util.PSQLException: ERROR: function XXX does not exist-CSDN博客
  2. 为什么在url后拼接&stringtype=unspecified,详见MySQL数据库切换瀚高数据库(PostgreSQL)导致SQL适配问题:BadSqlGrammarException_pgsql 查看版本是highgo-CSDN博客

3. 编写代码测试

1. 创建数据表

DROP TABLE IF EXISTS "test"."t_point";
CREATE TABLE "test"."t_point" (
  "int_id" int8,
  "longitude" varchar(255) COLLATE "pg_catalog"."default",
  "latitude" varchar(255) COLLATE "pg_catalog"."default"
)
;
 
INSERT INTO "test"."t_point" VALUES (1,  '120.80826', '115.07296');
INSERT INTO "test"."t_point" VALUES (2,  '10.12345', '10.56789');
INSERT INTO "test"."t_point" VALUES (3,  '785.45680', '456.75465');

2. 编写Controller层

@RestController
@RequiredArgsConstructor
public class PositionController {
    private final PositionService positionService;
 
    @GetMapping("/getAllPosition")
    public List<Position> getAllPosition() {
        return positionService.getAllPosition();
    }
}

3. 编写Service层

public interface PositionService extends IService<Position> {
    List<Position> getAllPosition();
}

4. 编写Mapper层

@Service
@RequiredArgsConstructor
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
    private final PositionMapper positionMapper;
 
    @Override
    public List<Position> getAllPosition() {
        return positionMapper.getAllPosition();
    }
}
<?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.zjp.demo.mapper.PositionMapper">
    <select id="getAllPosition" resultType="com.zjp.demo.pojo.Position">
        SELECT
            *
        FROM
            t_fl_fire_rescue_station
        WHERE
            longitude IS NOT NULL
          AND latitude IS NOT NULL
        ORDER BY
            round(
                    st_distancesphere (
                            geometry ( POINT ( 0:: DOUBLE, 0:: DOUBLE ) ),
                            geometry ( POINT ( longitude :: DOUBLE, latitude :: DOUBLE ) )
                    ),
                    2
            );
    </select>
</mapper>

 5. 测试代码

1. 启动项目,浏览器访问http://localhost:8080/getAllPosition,显示查询结果,配置成功!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值