SpringBoot集成瀚高数据库(PostgreSQL)

打印 上一主题 下一主题

主题 946|帖子 946|积分 2838

1. 媒介

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

  1. <dependency>
  2.   <groupId>com.highgo</groupId>
  3.   <artifactId>HgdbJdbc</artifactId>
  4.   <version>6.2.2</version>
  5. </dependency>
复制代码
或可以直接添加 PostgreSQL依赖
  1. <!--postgresql-->
  2. <dependency>
  3.     <groupId>org.postgresql</groupId>
  4.     <artifactId>postgresql</artifactId>
  5.     <scope>runtime</scope>
  6. </dependency>
复制代码
2. 设置文件

如果添加的是highgo的依赖,则写为:
  1. spring:
  2.   datasource:
  3.     #驱动类名称
  4.     driver-class-name: com.highgo.jdbc.Driver
  5.     #数据库连接的url
  6.     url: jdbc:highgo://ip:端口号/数据库名?currentSchema=模式名,public&stringtype=unspecified
  7.     #连接数据库的用户名
  8.     username: 账号
  9.     #连接数据库的密码
  10.     password: 密码
复制代码
 如果添加的是PostgreSQL的依赖,则写为:
  1. spring:
  2.   datasource:
  3.     #驱动类名称
  4.     driver-class-name: org.postgresql.Driver
  5.     #数据库连接的url
  6.     url: jdbc:postgresql://ip:端口号/数据库名?currentSchema=模式名,public&stringtype=unspecified
  7.     #连接数据库的用户名
  8.     username: 账号
  9.     #连接数据库的密码
  10.     password: 密码
复制代码
例如, 瀚高数据库的ip为localhost,端口为5866,默认数据库为highgo,模式名为test,账号和密码皆为postgres,那么可写为:
  1. spring:
  2.   datasource:
  3.     driver-class-name: com.highgo.jdbc.Driver
  4.     url: jdbc:highgo://localhost:5866/highgo?currentSchema=test,public&stringtype=unspecified
  5.     username: postgres
  6.     password: postgres
复制代码
或写为:
  1. spring:
  2.   datasource:
  3.     driver-class-name: org.postgresql.Driver
  4.     url: jdbc:postgresql://localhost:5866/highgo?currentSchema=test,public&stringtype=unspecified
  5.     username: postgres
  6.     password: postgres
复制代码
  说明:
  

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

1. 创建数据表
  1. DROP TABLE IF EXISTS "test"."t_point";
  2. CREATE TABLE "test"."t_point" (
  3.   "int_id" int8,
  4.   "longitude" varchar(255) COLLATE "pg_catalog"."default",
  5.   "latitude" varchar(255) COLLATE "pg_catalog"."default"
  6. )
  7. ;
  8. INSERT INTO "test"."t_point" VALUES (1,  '120.80826', '115.07296');
  9. INSERT INTO "test"."t_point" VALUES (2,  '10.12345', '10.56789');
  10. INSERT INTO "test"."t_point" VALUES (3,  '785.45680', '456.75465');
复制代码
2. 编写Controller层
  1. @RestController
  2. @RequiredArgsConstructor
  3. public class PositionController {
  4.     private final PositionService positionService;
  5.     @GetMapping("/getAllPosition")
  6.     public List<Position> getAllPosition() {
  7.         return positionService.getAllPosition();
  8.     }
  9. }
复制代码
3. 编写Service层
  1. public interface PositionService extends IService<Position> {
  2.     List<Position> getAllPosition();
  3. }
复制代码
4. 编写Mapper层
  1. @Service
  2. @RequiredArgsConstructor
  3. public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
  4.     private final PositionMapper positionMapper;
  5.     @Override
  6.     public List<Position> getAllPosition() {
  7.         return positionMapper.getAllPosition();
  8.     }
  9. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.zjp.demo.mapper.PositionMapper">
  4.     <select id="getAllPosition" resultType="com.zjp.demo.pojo.Position">
  5.         SELECT
  6.             *
  7.         FROM
  8.             t_fl_fire_rescue_station
  9.         WHERE
  10.             longitude IS NOT NULL
  11.           AND latitude IS NOT NULL
  12.         ORDER BY
  13.             round(
  14.                     st_distancesphere (
  15.                             geometry ( POINT ( 0:: DOUBLE, 0:: DOUBLE ) ),
  16.                             geometry ( POINT ( longitude :: DOUBLE, latitude :: DOUBLE ) )
  17.                     ),
  18.                     2
  19.             );
  20.     </select>
  21. </mapper>
复制代码
 5. 测试代码
1. 启动项目,浏览器访问http://localhost:8080/getAllPosition,表现查询效果,设置成功!!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表