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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot3集成PostgreSQL的詳細過程
- mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實現(xiàn)
- SpringBoot集成PostgreSQL并設置最大連接數(shù)
- SpringBoot項目配置postgresql數(shù)據(jù)庫完整步驟(配置多數(shù)據(jù)源)
- springboot+springJdbc+postgresql 實現(xiàn)多數(shù)據(jù)源的配置
- SpringBoot連接使用PostgreSql數(shù)據(jù)庫的方法
- Springboot中MyBatisplus使用IPage和Page分頁的實例代碼
- SpringBoot+MybatisPlus+代碼生成器整合示例
- springboot集成mybatisplus實例詳解
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
相關文章
Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解
本文主要介紹了一個基于spring?boot+maven+opencv實現(xiàn)的圖像識別及訓練項目,可以實現(xiàn)車牌識別功能,感興趣的可以跟隨小編一起試一試2022-01-01
javaweb Servlet開發(fā)總結(jié)(一)
Servlet是sun公司提供的一門用于開發(fā)動態(tài)web資源的技術。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下2016-05-05
使用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中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

