欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合PostgreSQL的示例代碼

 更新時(shí)間:2023年07月07日 08:35:35   作者:Vainycos  
本文主要介紹了SpringBoot整合PostgreSQL的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

postgresql簡介

與mysql一樣也是開源的關(guān)系型數(shù)據(jù)庫,同時(shí)還支持NoSql的文檔型存儲。在某些方面標(biāo)榜比mysql表現(xiàn)更加出眾,現(xiàn)在就讓我們來了解下如何使用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

測試用例

此處我們預(yù)先新建一個(gè)test表,查詢該表的記錄數(shù),當(dāng)前表中有一條記錄。

@Slf4j
@SpringBootTest
class MypgApplicationTests {
? ? @Autowired
? ? JdbcTemplate jdbcTemplate;
? ? @Test
? ? void contextLoads() {
? ? ? ? Long count = jdbcTemplate.queryForObject("select count(*) from test", Long.class);
? ? ? ? log.info("記錄總數(shù):{}",count);
? ? }
}

輸出預(yù)期結(jié)果
記錄總數(shù):1

整合mybatis

以上我們直接使用jdbc進(jìn)行數(shù)據(jù)操作,接下來我們使用mybatis進(jìn)行改造。

引入maven依賴

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.0</version>
</dependency>

調(diào)整配置文件

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寫法實(shí)現(xiàn)

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自動(dòng)化配置原理詳解
Spring Boot 整合 MyBatis-Plus

到此這篇關(guān)于SpringBoot整合PostgreSQL的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot整合PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例

    javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例

    這篇文章主要介紹了javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例,小編覺得還是挺不錯(cuò)的,需要的朋友可以參考。
    2017-10-10
  • 解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題

    解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題

    這篇文章主要介紹了解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 圖文詳解Java中class的初始化順序

    圖文詳解Java中class的初始化順序

    網(wǎng)上有很多關(guān)于Java中class的初始化順序文章,但是本文通過圖文更加詳細(xì)的介紹了Java中class的初始化順序,并對class的裝載順序進(jìn)行了講解,下面一起來看看。
    2016-08-08
  • Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊詳解

    Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • JAVA面向?qū)ο笤O(shè)計(jì)寵物類方式

    JAVA面向?qū)ο笤O(shè)計(jì)寵物類方式

    本指南涉及JAVA面向?qū)ο蟮膶櫸镱愒O(shè)計(jì),包括寵物類的父類及其子類小貓類和小狗類,用戶可以選擇養(yǎng)貓或養(yǎng)狗,給寵物起名字,實(shí)現(xiàn)喂食互動(dòng),同時(shí)寵物具有飽食度和快樂度屬性,適合初學(xué)者學(xué)習(xí)面向?qū)ο笤O(shè)計(jì)
    2024-10-10
  • Tomcat內(nèi)存溢出分析及解決方法

    Tomcat內(nèi)存溢出分析及解決方法

    堆是給開發(fā)人員用的上面說的就是,是在JVM啟動(dòng)時(shí)創(chuàng)建;非堆是留給JVM自己用的,用來存放類的信息的,本文將詳細(xì)介紹Tomcat內(nèi)存溢出,需要了解更多的朋友可以參考下
    2012-11-11
  • Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解

    Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Intellij IDEA下Spring Boot熱切換配置

    Intellij IDEA下Spring Boot熱切換配置

    這篇文章主要介紹了Intellij IDEA下Spring Boot熱切換配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Spring-boot JMS 發(fā)送消息慢的解決方法

    Spring-boot JMS 發(fā)送消息慢的解決方法

    這篇文章主要為大家詳細(xì)介紹了Spring-boot JMS 發(fā)送消息慢的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • springboot啟動(dòng)過程中常用的回調(diào)示例詳解

    springboot啟動(dòng)過程中常用的回調(diào)示例詳解

    springboot提供非常豐富回調(diào)接口,利用這些接口可以做非常多的事情,本文通過實(shí)例代碼給大家介紹springboot啟動(dòng)過程中常用的回調(diào)知識感興趣的朋友跟隨小編一起看看吧
    2022-01-01

最新評論