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

導(dǎo)入JDBC依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>導(dǎo)入數(shù)據(jù)庫驅(qū)動
我這里使用的MySQL數(shù)據(jù)庫,就導(dǎo)入MySQL數(shù)據(jù)庫驅(qū)動
<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表結(jié)構(gòu)

測試類代碼
查詢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);
}運行結(jié)果

查詢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);
}
}運行結(jié)果

查詢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ù)");
}
}
運行結(jié)果


修改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ù)");
}
}
運行結(jié)果


刪除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ù)");
}
}
運行結(jié)果


總結(jié)
以上就是SpringBoot整合JdbcTemplate基本內(nèi)容,其它內(nèi)容以后慢慢研究。
到此這篇關(guān)于SpringBoot整合JdbcTemplate的文章就介紹到這了,更多相關(guān)SpringBoot整合JdbcTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot用JdbcTemplates操作Mysql實例代碼詳解
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- Spring Boot 整合持久層之JdbcTemplate
- Spring操作JdbcTemplate數(shù)據(jù)庫的方法學(xué)習(xí)
- Spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
- Spring JdbcTemplate實現(xiàn)添加與查詢方法詳解
相關(guān)文章
詳解Java使用Pipeline對Redis批量讀寫(hmset&hgetall)
本篇文章主要介紹了Java使用Pipeline對Redis批量讀寫(hmset&hgetall),具有一定的參考價值,有興趣的可以了解一下。2016-12-12
JDK14的新特性NullPointerExceptions的使用
這篇文章主要介紹了JDK14的新特性NullPointerExceptions的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
關(guān)于Intellij idea 報錯:Error : java 不支持發(fā)行版本5的問題
這篇文章主要介紹了關(guān)于Intellij idea 報錯:Error : java 不支持發(fā)行版本5的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
基于Java SSM框架實現(xiàn)簡易的評教系統(tǒng)
這篇文章主要介紹了通過Java SSM框架實現(xiàn)一個簡易的評教系統(tǒng)的示例代碼,文中的代碼講解詳細,感興趣的小伙伴可以了解一下2022-02-02

