锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. SpringBoot整合PostgreSQL教程

SpringBoot整合PostgreSQL教程

0
  • 软件开发
  • 发布于 2024-08-17
  • 0 次阅读
黄健
黄健

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

主要描述如何优雅的整合 postgresql。本文略去如何安装 pgsql 的过程,详情可参考其他文章。

文章目录

  • postgresql 简介
  • 整合 postgresql
  • 整合 mybatis
  • 整合 mybatis-plus

postgresql 简介

与 mysql 一样也是开源的关系型数据库,同时还支持 NoSql 的文档型存储。在某些方面标榜比 mysql 表现更加出众,现在就让我们来了解下如何使用 postgresql。

整合 postgresql

  • 引入依赖
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>3.0.4</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.26</version>
</dependency>
  • 配置文件
spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    username: postgres
    password: root
    url: jdbc:postgresql://127.0.0.1:5432/postgres
  • 测试用例
    此处我们预先新建一个 test 表,查询该表的记录数,当前表中有一条记录。
@Slf4j
@SpringBootTest
class MypgApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Test
    void contextLoads() {
        Long count = jdbcTemplate.queryForObject("select count(*) from test", Long.class);
        log.info("记录总数:{}",count);
    }

}
  • 输出预期结果
记录总数:1

整合 mybatis

以上我们直接使用 jdbc 进行数据操作,接下来我们使用 mybatis 进行改造。

  • 引入 maven 依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.0</version>
</dependency>
  • 调整配置文件
mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 新建 domain/service/mapper/mapper.xml
@Data
public class Test {

    private Long id;
}
public interface TestMapper {

    Long queryForMybatis(Test testDO);

}
<?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.vainycos.dao.TestMapper">

    <select id="queryForMybatis" parameterType="com.vainycos.domain.Test" resultType="java.lang.Long">
        select count(*) from test
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
        </where>
    </select>
</mapper>
@Service
public class TestService {

    @Resource
    private TestMapper testMapper;

    public Long queryForMybatis(Test testDO){
        return testMapper.queryForMybatis(testDO);
    }

}
  • mapper 文件路径指向
@MapperScan("com.vainycos.dao")
@SpringBootApplication
public class MypgApplication {

    public static void main(String[] args) {
        SpringApplication.run(MypgApplication.class, args);
    }

}

整合 mybatis-plus

  • 引入 mybatis-plus 的 maven 依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>
  • 改造 mapper 接口
public interface TestMapper extends BaseMapper<Test> {

    Long queryForMybatis(Test testDO);

}
  • service 使用 mybatis-plus 写法实现
public Long queryForMybatisPlus(Test testDO){
    Integer resultCount = testMapper.selectCount(new LambdaQueryWrapper<Test>()
            .eq(testDO.getId() != null, Test::getId, testDO.getId())
    );
    return resultCount.longValue();
}
  • controller 测试
@GetMapping("/queryForMybatisPlus")
public Long queryForMybatisPlus(Test testDO){
    return testService.queryForMybatisPlus(testDO);
}

参考资料:

  • SpringBoot 整合 pgSQL
  • SpringBoot 集成 MyBatis-yml 自动化配置原理详解
  • Spring Boot 整合 MyBatis-Plus
标签: #Spring Boot 173 #软件开发 1171 #JAVA 991
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.