SpringBoot整合JdbcTemplate的示例代碼
前言
Spring對數(shù)據(jù)庫的操作在jdbc上面做了更深層次的封裝,而JdbcTemplate便是Spring提供的一個操作數(shù)據(jù)庫的便捷工具。我們可以借助JdbcTemplate來執(zhí)行所有數(shù)據(jù)庫操作,例如插入,更新,刪除和從數(shù)據(jù)庫中檢索數(shù)據(jù),并且有效避免直接使用jdbc帶來的繁瑣編碼
初始化SpringBoot項目
使用IDEA創(chuàng)建項目
點擊File–>New–>Project
點擊spring initializr,注意自己的SDK版本,再點擊next
填寫自己的group、artifact,注意Java version的版本和前面的SDK版本一致,點擊next
選擇自己要添加的依賴,項目創(chuàng)建的時候會自動加入到maven中,然后
點擊next
填寫自己的項目名,點擊finish
導入JDBC依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency>
導入數(shù)據(jù)庫驅動
我這里使用的MySQL數(shù)據(jù)庫,就導入MySQL數(shù)據(jù)庫驅動
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency>
修改配置文件
我這里使用的是yml格式配置文件
spring: datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8 username: root password: admin driver-class-name: com.mysql.jdbc.Driver
數(shù)據(jù)庫sys_user表結構
測試類代碼
查詢sys_user表數(shù)據(jù)量
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //獲取sys_user表數(shù)據(jù)量 Long aLong = jdbcTemplate.queryForObject("select count(*) from sys_user", Long.class); log.info("記錄總數(shù):{}", aLong); }
運行結果
查詢sys_user表一條數(shù)據(jù)
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //獲取sys_user表一條數(shù)據(jù) Map<String, Object> map = jdbcTemplate.queryForMap("select * from sys_user where id = 1"); log.info("map數(shù)據(jù)為:" + map); } }
運行結果
查詢sys_user表所有數(shù)據(jù)
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //獲取sys_user表所有數(shù)據(jù) List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from sys_user"); log.info("list數(shù)據(jù)為:" + list); } }
新增sys_user表一條數(shù)據(jù)
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //新增一條數(shù)據(jù) int i = jdbcTemplate.update("insert into sys_user(user_name, phone) values(?,?)", new String[]{"小王", "13100720084"}); log.info("新增了" + i + "條數(shù)據(jù)"); } }
運行結果
修改sys_user表一條數(shù)據(jù)
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //修改一條數(shù)據(jù) int i2 = jdbcTemplate.update("update sys_user set user_name = ? where id = 1", new String[]{"小張"}); log.info("修改了" + i2 + "條數(shù)據(jù)"); } }
運行結果
刪除sys_user表一條數(shù)據(jù)
package com.gavin.boot; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.Map; @Slf4j @SpringBootTest class BootJdbcApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { //刪除一條數(shù)據(jù) int i3 = jdbcTemplate.update("delete from sys_user where id = ?", 1); log.info("刪除了" + i3 + "條數(shù)據(jù)"); } }
運行結果
總結
以上就是SpringBoot整合JdbcTemplate基本內容,其它內容以后慢慢研究。
到此這篇關于SpringBoot整合JdbcTemplate的文章就介紹到這了,更多相關SpringBoot整合JdbcTemplate內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot用JdbcTemplates操作Mysql實例代碼詳解
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- Spring Boot 整合持久層之JdbcTemplate
- Spring操作JdbcTemplate數(shù)據(jù)庫的方法學習
- Spring學習JdbcTemplate數(shù)據(jù)庫事務參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫事務管理完全注解方式
- Spring JdbcTemplate實現(xiàn)添加與查詢方法詳解
相關文章
SpringBoot通過JSON傳遞請求參數(shù)的實例詳解
這篇文章主要介紹了SpringBoot通過JSON傳遞請求參數(shù),示例介紹SpringMVC如何通過JSON格式傳遞入?yún)?,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11基于SpringBoot和Vue3的博客平臺文章詳情與評論功能實現(xiàn)
在前面的教程中,我們已經(jīng)實現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能以及文章列表與分頁功能。本教程將引導您實現(xiàn)博客平臺的文章詳情與評論功能,需要的朋友可以參考一下2023-04-04Java Validation Api實現(xiàn)原理解析
這篇文章主要介紹了Java Validation Api實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09