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); ? ? } }
輸出預期結果
記錄總數(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>
調整配置文件
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內容請搜索腳本之家以前的文章或繼續(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入門案例(代碼詳解)
相關文章
解決springboot 多線程使用MultipartFile讀取excel文件內容報錯問題
這篇文章主要介紹了解決springboot 多線程使用MultipartFile讀取excel文件內容報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Intellij IDEA下Spring Boot熱切換配置
這篇文章主要介紹了Intellij IDEA下Spring Boot熱切換配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Spring-boot JMS 發(fā)送消息慢的解決方法
這篇文章主要為大家詳細介紹了Spring-boot JMS 發(fā)送消息慢的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08