SpringBoot整合JDBC的實(shí)現(xiàn)
簡介
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關(guān)系的都在Spring Data家族中,所以我們看看springboot中如何使用JDBC來實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查操作。
簡單使用
引入依賴
這里我們只引入基本的依賴就好,創(chuàng)建一個(gè)springboot項(xiàng)目(這里版本是2.1.6),然后添加以下依賴:
<dependencies>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql驅(qū)動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtimen</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
編寫配置文件
這里我們需要把數(shù)據(jù)庫的基本連接信息配置好
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver ## 這里如果不配置時(shí)區(qū)可能會報(bào)錯(cuò),所以配置時(shí)區(qū):serverTimezone=UT url: jdbc:mysql://localhost:3306/study_springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: root
編寫測試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
@Autowired
private DataSource dataSource;
@Test
public void load(){
// 打印出:class com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
}
}
實(shí)現(xiàn)增刪改查
spring boot中有很多的xxxTemplate,也就是給我們默認(rèn)配置了 很多的模板,方便我們進(jìn)行開發(fā),比如上面測試中的 JdbcTemplate,spring boot已經(jīng)給我們封裝好方法了,我們只要調(diào)用就好,下面是增刪改查的案例:
@RestController
public class TestController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/userList")
public List<Map<String, Object>> getUserList(){
String sql = "select * from study_springboot.user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@GetMapping("/addUser")
public String addUser(){
String sql = "insert into study_springboot.user(id, name, password) values('1', 'zhangsan', 'qqqq')";
jdbcTemplate.update(sql);
return "add success";
}
/**
* 可以通過占位符實(shí)現(xiàn)入?yún)?
* @param id
* @return
*/
@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id){
String sql = "update study_springboot.user set name =?, password = ? where id = "+id;
// 封裝占位符
Object[] objects = new Object[2];
objects[0] = "李四";
objects[1] = "pppppp";
jdbcTemplate.update(sql, objects);
return "update success";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id){
String sql = "delete from study_springboot.user where id = ?";
// int 類型也是一個(gè)object,所以這樣傳參也是可以的
jdbcTemplate.update(sql, id);
return "delete success";
}
}
上面的案例只是展示基本的操作,但是真實(shí)項(xiàng)目中是不會這樣寫的,一般還是整合MyBatis或者JPA來實(shí)現(xiàn)操作數(shù)據(jù)源。
到此這篇關(guān)于SpringBoot整合JDBC的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- 關(guān)于Spring Boot對jdbc的支持問題
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫分表功能
- Springboot jdbctemplate整合實(shí)現(xiàn)步驟解析
- Spring JdbcTemplate整合使用方法及原理詳解
- Spring jdbc具名參數(shù)使用方法詳解
- 詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式
- Spring JDBC的使用詳解
相關(guān)文章
Spring Data JPA 整合QueryDSL的使用案例
QueryDSL 是一個(gè)用于構(gòu)建類型安全的 SQL 查詢的 Java 庫,它的主要目標(biāo)是簡化在 Java 中構(gòu)建和執(zhí)行 SQL 查詢的過程,同時(shí)提供類型安全性和更好的編碼體驗(yàn),對Spring Data JPA 整合QueryDSL使用案例感興趣的朋友跟隨小編一起看看吧2023-08-08
springboot+EHcache 實(shí)現(xiàn)文章瀏覽量的緩存和超時(shí)更新
這篇文章主要介紹了springboot+EHcache 實(shí)現(xiàn)文章瀏覽量的緩存和超時(shí)更新,問題描述和解決思路給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-04-04
Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能
在早期的網(wǎng)絡(luò)編程中,Socket是很常見的實(shí)現(xiàn)技術(shù)之一,比如早期的聊天室,就是基于這種技術(shù)進(jìn)行實(shí)現(xiàn)的,另外現(xiàn)在有些消息推送,也可以基于Socket實(shí)現(xiàn),本文小編給大家介紹了Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能的示例,需要的朋友可以參考下2023-11-11
SpringBoot使用TraceId進(jìn)行日志追蹤的實(shí)現(xiàn)
本文主要介紹了SpringBoot使用TraceId進(jìn)行日志追蹤的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題
這篇文章主要介紹了解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java:DocumentBuilderFactory調(diào)用XML的方法實(shí)例
Java:DocumentBuilderFactory調(diào)用XML的方法實(shí)例,需要的朋友可以參考一下2013-04-04
基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別
這篇文章主要介紹了基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring?Cloud?整合?nacos實(shí)現(xiàn)動態(tài)配置中心的詳細(xì)步驟
這篇文章主要介紹了Spring?Cloud?整合?nacos?實(shí)現(xiàn)動態(tài)配置中心,整合步驟是通過添加依賴新建nacos配置,本文分步驟通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10

