SpringBoot整合PostgreSQL的示例代碼
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)文章希望大家以后多多支持腳本之家!
- SpringBoot3集成PostgreSQL的詳細(xì)過程
- mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)
- SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
- SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫完整步驟(配置多數(shù)據(jù)源)
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- SpringBoot連接使用PostgreSql數(shù)據(jù)庫的方法
- Springboot中MyBatisplus使用IPage和Page分頁的實(shí)例代碼
- SpringBoot+MybatisPlus+代碼生成器整合示例
- springboot集成mybatisplus實(shí)例詳解
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
相關(guā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ò)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Spring 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-07Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Intellij IDEA下Spring Boot熱切換配置
這篇文章主要介紹了Intellij IDEA下Spring Boot熱切換配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Spring-boot JMS 發(fā)送消息慢的解決方法
這篇文章主要為大家詳細(xì)介紹了Spring-boot JMS 發(fā)送消息慢的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08springboot啟動(dòng)過程中常用的回調(diào)示例詳解
springboot提供非常豐富回調(diào)接口,利用這些接口可以做非常多的事情,本文通過實(shí)例代碼給大家介紹springboot啟動(dòng)過程中常用的回調(diào)知識感興趣的朋友跟隨小編一起看看吧2022-01-01