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
复制代码 说明:
- 为什么在 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. 创建数据表
- 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,表现查询效果,设置成功!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |