springboot使用JdbcTemplate完成對數(shù)據(jù)庫的增刪改查功能
首先新建一個簡單的數(shù)據(jù)表,通過操作這個數(shù)據(jù)表來進行演示
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ū)動類,數(shù)據(jù)庫地址,數(shù)據(jù)庫賬號、密碼信息,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
新建一個實體類,屬性對應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)錯誤";
} 我們做一個測試。在postman測試工具中輸入http://localhost:8080/items/add

我們可以看到,新增已經(jīng)成功了,確實很方便,也沒有繁瑣的配置信息。
其余刪除,更新操作與新增代碼不變,只是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;
} 我們做一個測試。在postman測試工具中輸入http://localhost:8080/items/list
我們看到,包括剛才新增的數(shù)據(jù),都已經(jīng)被查出來了。

這里為了學習一下springboot的JdbcTemplate操作,所有增刪改查代碼都寫在ItemsController類中,也方便演示,這里把代碼貼出來,需要的可以運行一下
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)錯誤";
}
/**
* @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)錯誤";
}
/**
* @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)錯誤";
}
} 這里解釋一個注解
@ComponentScan:
@ComponentScan告訴Spring 哪個注解標識的類會被spring自動掃描并且裝入bean容器。如果你有個類用@Controller注解標識了,那么,如果不加上@ComponentScan自動掃描該controller,那么該Controller就不會被spring掃描到,更不會裝入spring容器中,Controller就不會起作用。
啟動類代碼
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完成對數(shù)據(jù)庫的增刪改查功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
- SpringBoot2使用JTA組件實現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務管理(親測好用)
- SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
- SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實例
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- 如何在SpringBoot項目中使用Oracle11g數(shù)據(jù)庫
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
相關(guān)文章
全面了解OAuth?2.0四種授權(quán)方式金三銀四無懼面試
這篇文章主要介紹了全面了解OAuth?2.0四種授權(quán)方式金三銀四無懼面試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Spring Security Oauth2.0 實現(xiàn)短信驗證碼登錄示例
本篇文章主要介紹了Spring Security Oauth2.0 實現(xiàn)短信驗證碼登錄示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
全網(wǎng)最深分析SpringBoot MVC自動配置失效的原因
這篇文章主要介紹了全網(wǎng)最深分析SpringBoot MVC自動配置失效的原因,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Idea進行pull的時候Your local changes would be
這篇文章主要介紹了Idea進行pull的時候Your local changes would be overwritten by merge.具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot使用Prometheus實現(xiàn)監(jiān)控
在當今的軟件開發(fā)世界中,監(jiān)控是至關(guān)重要的一部分,本文主要介紹了如何在Spring Boot應用程序中使用Prometheus進行監(jiān)控,以幫助大家更好地理解和管理您的應用程序,有需要的可以參考下2023-10-10
Spring整合Quartz實現(xiàn)動態(tài)定時器的示例代碼
本篇文章主要介紹了Spring整合Quartz實現(xiàn)動態(tài)定時器的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01

