亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能

 更新時(shí)間:2022年05月20日 10:36:42   作者:阿木俠  
這篇文章主要介紹了springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫的增刪改查功能,需要的朋友可以參考下

首先新建一個(gè)簡單的數(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è)測試。在postman測試工具中輸入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è)測試。在postman測試工具中輸入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)站的支持!

相關(guān)文章

  • Deepin系統(tǒng)安裝eclipse2021-03及CDT插件的安裝教程

    Deepin系統(tǒng)安裝eclipse2021-03及CDT插件的安裝教程

    本教程教大家deepin20.1操作系統(tǒng)上安裝eclipse_2021-03版的詳細(xì)步驟及CDT插件的安裝方法,通過圖文展示的非常明了,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-06-06
  • request如何獲取body的json數(shù)據(jù)

    request如何獲取body的json數(shù)據(jù)

    這篇文章主要介紹了request如何獲取body的json數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Hibernate 與 Mybatis 的共存問題,打破你的認(rèn)知!(兩個(gè)ORM框架)

    Hibernate 與 Mybatis 的共存問題,打破你的認(rèn)知!(兩個(gè)ORM框架)

    這篇文章主要介紹了Hibernate 與 Mybatis 如何共存?本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 解析Java的Spring框架的基本結(jié)構(gòu)

    解析Java的Spring框架的基本結(jié)構(gòu)

    這篇文章主要介紹了Java的Spring框架的基本結(jié)構(gòu),作者從Spring的設(shè)計(jì)角度觸發(fā)解析其基礎(chǔ)的架構(gòu)內(nèi)容,需要的朋友可以參考下
    2016-03-03
  • IntelliJ IDEA 2020.2 配置大全詳細(xì)圖文教程(更新中)

    IntelliJ IDEA 2020.2 配置大全詳細(xì)圖文教程(更新中)

    這篇文章主要介紹了IntelliJ IDEA 2020.2 配置大全(更新中),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 詳解Spring Aop實(shí)例之xml配置

    詳解Spring Aop實(shí)例之xml配置

    本篇文章主要介紹了詳解Spring Aop實(shí)例之xml配置,使用xml可以對(duì)aop進(jìn)行集中配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼

    java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼

    這篇文章主要介紹了java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • idea2020安裝MybatisCodeHelper插件的圖文教程

    idea2020安裝MybatisCodeHelper插件的圖文教程

    這篇文章主要介紹了idea2020安裝MybatisCodeHelper插件的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Spring Boot集成教程之異步調(diào)用Async

    Spring Boot集成教程之異步調(diào)用Async

    在項(xiàng)目中,當(dāng)訪問其他人的接口較慢或者做耗時(shí)任務(wù)時(shí),不想程序一直卡在耗時(shí)任務(wù)上,想程序能夠并行執(zhí)行,我們可以使用多線程來并行的處理任務(wù),也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來一起看看吧。
    2018-03-03
  • Java多線程實(shí)現(xiàn)快速切分文件的程序

    Java多線程實(shí)現(xiàn)快速切分文件的程序

    這篇文章主要為大家詳細(xì)介紹了Java多線程實(shí)現(xiàn)快速切分文件的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評(píng)論