springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能
首先新建一個(gè)簡(jiǎn)單的數(shù)據(jù)表,通過操作這個(gè)數(shù)據(jù)表來進(jìn)行演示
DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `detail` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
引入JdbcTemplate的maven依賴及連接類
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
在application.properties文件配置mysql的驅(qū)動(dòng)類,數(shù)據(jù)庫地址,數(shù)據(jù)庫賬號(hào)、密碼信息,application.properties新建在src/main/resource文件夾下
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 server.port=8080 server.session.timeout=10 server.tomcat.uri-encoding=UTF-8
新建一個(gè)實(shí)體類,屬性對(duì)應(yīng)sql字段
package org.amuxia.start;
public class Items {
private Integer id;
private String title;
private String name;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Items() {
super();
// TODO Auto-generated constructor stub
}
public Items(Integer id, String title, String name, String detail) {
super();
this.id = id;
this.title = title;
this.name = name;
this.detail = detail;
}
@Override
public String toString() {
return "Items [id=" + id + ", title=" + title + ", name=" + name + ", detail=" + detail + "]";
}
} 新增操作
/**
* 新增數(shù)據(jù)
* @param items
* @return
*/
@RequestMapping("/add")
public @ResponseBody String addItems(Items items) {
String sql = "insert into items (id,title,name,detail) value (?,?,?,?)";
Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章新增成功";
}
return "新增出現(xiàn)錯(cuò)誤";
} 我們做一個(gè)測(cè)試。在postman測(cè)試工具中輸入http://localhost:8080/items/add

我們可以看到,新增已經(jīng)成功了,確實(shí)很方便,也沒有繁瑣的配置信息。
其余刪除,更新操作與新增代碼不變,只是sql的變化,這里不做演示。
全部查詢操作
/**
* @return
* 查詢?nèi)啃畔?
*/
@RequestMapping("/list")
public List<Map<String, Object>> itemsList() {
String sql = "select * from items";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
} 我們做一個(gè)測(cè)試。在postman測(cè)試工具中輸入http://localhost:8080/items/list
我們看到,包括剛才新增的數(shù)據(jù),都已經(jīng)被查出來了。

這里為了學(xué)習(xí)一下springboot的JdbcTemplate操作,所有增刪改查代碼都寫在ItemsController類中,也方便演示,這里把代碼貼出來,需要的可以運(yùn)行一下
package org.amuxia.start;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@ComponentScan
@RestController
@RequestMapping("/items")
public class ItemsController {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* @return
* 查詢?nèi)啃畔?
*/
@RequestMapping("/list")
public List<Map<String, Object>> itemsList() {
String sql = "select * from items";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
/**
* @param id
* @return
* 根據(jù)ID查詢單條信息
*/
@RequestMapping("/detail/{id}")
public Map<String, Object> detail(@PathVariable int id) {
Map<String, Object> map = null;
List<Map<String, Object>> list = itemsList();
map = list.get(id);
return map;
}
/**
* 新增數(shù)據(jù)
* @param items
* @return
*/
@RequestMapping("/add")
public @ResponseBody String addItems(Items items) {
String sql = "insert into items (id,title,name,detail) value (?,?,?,?)";
Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章新增成功";
}
return "新增出現(xiàn)錯(cuò)誤";
}
/**
* @param items
* @return
* 刪除數(shù)據(jù)
*/
@RequestMapping("/del")
public @ResponseBody String delItems(Items items) {
String sql = "delete from items where id = ?";
Object args[] = {items.getId()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章刪除成功";
}
return "刪除出現(xiàn)錯(cuò)誤";
}
/**
* @param items
* @return
* 更新操作
*/
@RequestMapping("/upd")
public @ResponseBody String updItems(Items items) {
String sql = "update items set title = ?,detail = ? where id = ?";
Object args[] = {items.getTitle(),items.getDetail(),items.getId()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章修改成功";
}
return "修改出現(xiàn)錯(cuò)誤";
}
} 這里解釋一個(gè)注解
@ComponentScan:
@ComponentScan告訴Spring 哪個(gè)注解標(biāo)識(shí)的類會(huì)被spring自動(dòng)掃描并且裝入bean容器。如果你有個(gè)類用@Controller注解標(biāo)識(shí)了,那么,如果不加上@ComponentScan自動(dòng)掃描該controller,那么該Controller就不會(huì)被spring掃描到,更不會(huì)裝入spring容器中,Controller就不會(huì)起作用。
啟動(dòng)類代碼
package org.amuxia.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class App
{
public static void main( String[] args )
{
System.out.println( "start....." );
SpringApplication.run(ItemsController.class, args);
}
} 總結(jié)
以上所述是小編給大家介紹的springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
- SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測(cè)好用)
- SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
- SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡(jiǎn)單使用方法
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實(shí)例
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- 如何在SpringBoot項(xiàng)目中使用Oracle11g數(shù)據(jù)庫
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
相關(guān)文章
java中字符進(jìn)行全角半角轉(zhuǎn)換示例代碼
全角:指一個(gè)字符占用兩個(gè)標(biāo)準(zhǔn)字符位置,而半角:指一字符占用一個(gè)標(biāo)準(zhǔn)的字符位置,在日常開發(fā)中經(jīng)常會(huì)遇到全角半角轉(zhuǎn)換的要求,下面這篇文章主要給大家介紹了關(guān)于java中字符進(jìn)行全角半角轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-08-08
全面了解OAuth?2.0四種授權(quán)方式金三銀四無懼面試
這篇文章主要介紹了全面了解OAuth?2.0四種授權(quán)方式金三銀四無懼面試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例
本篇文章主要介紹了Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
全網(wǎng)最深分析SpringBoot MVC自動(dòng)配置失效的原因
這篇文章主要介紹了全網(wǎng)最深分析SpringBoot MVC自動(dòng)配置失效的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Idea進(jìn)行pull的時(shí)候Your local changes would be
這篇文章主要介紹了Idea進(jìn)行pull的時(shí)候Your local changes would be overwritten by merge.具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot使用Prometheus實(shí)現(xiàn)監(jiān)控
在當(dāng)今的軟件開發(fā)世界中,監(jiān)控是至關(guān)重要的一部分,本文主要介紹了如何在Spring Boot應(yīng)用程序中使用Prometheus進(jìn)行監(jiān)控,以幫助大家更好地理解和管理您的應(yīng)用程序,有需要的可以參考下2023-10-10
Spring整合Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)器的示例代碼
本篇文章主要介紹了Spring整合Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)器的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01

