最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合PostgreSQL的示例代碼

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

postgresql簡介

與mysql一樣也是開源的關系型數(shù)據(jù)庫,同時還支持NoSql的文檔型存儲。在某些方面標榜比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

測試用例

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

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

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

整合mybatis

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

引入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寫法實現(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自動化配置原理詳解
Spring Boot 整合 MyBatis-Plus

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

相關文章

  • Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解

    Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解

    本文主要介紹了一個基于spring?boot+maven+opencv實現(xiàn)的圖像識別及訓練項目,可以實現(xiàn)車牌識別功能,感興趣的可以跟隨小編一起試一試
    2022-01-01
  • Java?for循環(huán)倒序輸出的操作代碼

    Java?for循環(huán)倒序輸出的操作代碼

    在Java中,要實現(xiàn)一個for循環(huán)的倒序輸出,通常我們會使用數(shù)組或集合(如ArrayList)作為數(shù)據(jù)源,然后通過倒序遍歷這個數(shù)組或集合來實現(xiàn),這篇文章主要介紹了Java?for循環(huán)倒序輸出,需要的朋友可以參考下
    2024-07-07
  • Java日常練習題,每天進步一點點(17)

    Java日常練習題,每天進步一點點(17)

    下面小編就為大家?guī)硪黄狫ava基礎的幾道練習題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • Spring boot actuator端點啟用和暴露操作

    Spring boot actuator端點啟用和暴露操作

    這篇文章主要介紹了Spring boot actuator端點啟用和暴露操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • javaweb Servlet開發(fā)總結(jié)(一)

    javaweb Servlet開發(fā)總結(jié)(一)

    Servlet是sun公司提供的一門用于開發(fā)動態(tài)web資源的技術。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下
    2016-05-05
  • java教程之二個arraylist排序的示例分享

    java教程之二個arraylist排序的示例分享

    常常遇到數(shù)組排序的問題,下面提供二個java的arraylist排序示例,需要的朋友可以參考下
    2014-03-03
  • 使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認的編碼為ISO-8859-1,遇到這個問題,該如何對字符串進行兩個編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧
    2021-12-12
  • 一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)

    一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)

    這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 學習Java之IO流的基礎概念詳解

    學習Java之IO流的基礎概念詳解

    這篇文章主要給大家介紹了Java中的IO流,我們首先要搞清楚一件事,就是為什么需要IO流這個東西,但在正式學習IO流的使用之前,小編有必要帶大家先了解一下IO流的基本概念,需要的朋友可以參考下
    2023-09-09
  • java實現(xiàn)二維碼掃碼授權登陸

    java實現(xiàn)二維碼掃碼授權登陸

    這篇文章主要為大家詳細介紹了java實現(xiàn)二維碼掃碼授權登陸,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10

最新評論

华坪县| 福海县| 杂多县| 侯马市| 余干县| 会泽县| 乃东县| 沈阳市| 安吉县| 盐边县| 资中县| 临潭县| 于田县| 封丘县| 伊川县| 广平县| 凤冈县| 柘城县| 望都县| 安阳县| 库伦旗| 丽江市| 大丰市| 安西县| 太和县| 广东省| 沛县| 娱乐| 南平市| 沂水县| 峨眉山市| 凉城县| 即墨市| 聂拉木县| 霸州市| 青田县| 吴川市| 贞丰县| 博客| 霍州市| 融水|