SpringBoot使用JdbcTemplate訪(fǎng)問(wèn)操作數(shù)據(jù)庫(kù)基本用法
Spring
對(duì)數(shù)據(jù)庫(kù)的操作在jdbc上s面做了深層次的封裝,使用spring的注入功能,可以把DataSource
注冊(cè)到JdbcTemplate
之中。
JdbcTemplate
在Spring-jdbc
包下面,還需要Spring-tx
包支持,里面包含事務(wù)和異??刂?
一、建一個(gè)rumenz_springboot庫(kù)
創(chuàng)建user表:
create table user( ? id int primary key auto_increment, ? name varchar(100) not null default '', ? domain varchar(100) not null default '' )engine=innodb default charset=utf8;
二、加入pom的依賴(lài)
<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>
三、SpringBoot配置文件
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/rumenz_springboot spring.datasource.username=root spring.datasource.password=root1234 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
四、創(chuàng)建User實(shí)體類(lèi)
@Builder @Data @AllArgsConstructor public class User ?implements RowMapper { ? ? private Integer id; ? ? private String name; ? ? private String domain; ? ? @Override ? ? public Object mapRow(ResultSet rs, int rowNum) throws SQLException { ? ? ? ? User user=new User(); ? ? ? ? user.setId(rs.getInt("id")); ? ? ? ? user.setName(rs.getString("name")); ? ? ? ? user.setDomain(rs.getString("domain")); ? ? ? ? return user; ? ? } }
五、Service接口
UserService.java
package com.rumenz.lession14.controller.service; import com.rumenz.lession14.controller.entity.User; import java.util.List; /** ?* @className: UserService ?* @description: TODO 類(lèi)描述 ?* @author: 入門(mén)小站 rumenz.com ?* @date: 2021/12/13 ?**/ public interface UserService { ? ? Integer save(User user); ? ? List<User> list(); ? ? Integer update(User user); ? ? Integer batchSave(); }
六、Service接口實(shí)現(xiàn)類(lèi)
UserServiceImpl.java
package com.rumenz.lession14.controller.service.Impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import com.rumenz.lession14.controller.entity.User; import com.rumenz.lession14.controller.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** ?* @className: UserServiceImpl ?* @description: TODO 類(lèi)描述 ?* @author: 入門(mén)小站 rumenz.com ?* @date: 2021/12/13 ?**/ @Service public class UserServiceImpl implements UserService { ? ? @Autowired ? ? private JdbcTemplate jdbcTemplate; ? ? @Override ? ? public Integer save(User user) { ? ? ? ? int reint = jdbcTemplate.update("insert into user(name,domain) values (?,?)", user.getName(), user.getDomain()); ? ? ? ? return reint; ? ? } ? ? @Override ? ? public Integer batchSave() { ? ? ? ? String sql="insert into user(name,domain) values(?,?)"; ? ? ? ? List<Object[]> par=new ArrayList<>(); ? ? ? ? for (int i = 0; i < 10; i++) { ? ? ? ? ? ? String[] s=new String[2]; ? ? ? ? ? ? s[0]="入門(mén)小站"+i; ? ? ? ? ? ? s[1]="https://rumenz.com/"+i; ? ? ? ? ? ? par.add(s); ? ? ? ? } ? ? ? ? int[] ints = jdbcTemplate.batchUpdate(sql, par); ? ? ? ? System.out.println(ints.toString()); ? ? ? ? return 0; ? ? } ? ? @Override ? ? public List<User> list() { ? ? ? ? //User實(shí)現(xiàn)RowMapper接口,實(shí)現(xiàn)接口里的mapRow方法。 ? ? ? ? List<User> list = jdbcTemplate.query("select * from user",new User()); ? ? ? ? return list; ? ? } ? ? @Override ? ? public Integer update(User user) { ? ? ? ? Integer reint=jdbcTemplate.update("update user set name=?,domain=? where id=?", user.getName(),user.getDomain(),user.getId()); ? ? ? ? // ? ? ? ? return reint; ? ? } }
七、Controller測(cè)試
RumenzController.java
package com.rumenz.lession14.controller; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.rumenz.lession14.controller.entity.User; import com.rumenz.lession14.controller.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** ?* @className: RumenzController ?* @description: TODO 類(lèi)描述 ?* @author: 入門(mén)小站 rumenz.com ?* @date: 2021/12/13 ?**/ @RestController @RequestMapping("/rumenz") public class RumenzController { ? ? @Autowired ? ? private UserService userService; ? ? //添加數(shù)據(jù) ? ? @GetMapping("/save") ? ? public String save(){ ? ? ? ? User user=User.builder().name("入門(mén)小站").domain("https://rumenz.com").build(); ? ? ? ? Integer reint = userService.save(user); ? ? ? ? return reint.toString(); ? ? } ? ? //批量添加數(shù)據(jù) ? ? @GetMapping("/batchSave") ? ? public String batchSave(){ ? ? ? ? Integer reint = userService.batchSave(); ? ? ? ? return reint.toString(); ? ? } ? ? //查詢(xún)數(shù)據(jù) ? ? @GetMapping("/list") ? ? public String list() throws JsonProcessingException { ? ? ? ? List<User> list = userService.list(); ? ? ? ? ObjectMapper objectMapper=new ObjectMapper(); ? ? ? ? String val = objectMapper.writeValueAsString(list); ? ? ? ? return val; ? ? } ? ? //更新數(shù)據(jù) ? ? @GetMapping("/update") ? ? public String update() throws JsonProcessingException { ? ? ? ? User user=User.builder().id(1).name("入門(mén)小站-修改").domain("https://tooltt.com").build(); ? ? ? ? Integer reint = userService.update(user); ? ? ? ? return reint.toString(); ? ? } }
八、總結(jié)
常用CURD操作大致使用以下三個(gè)方法:
- 1.execute方法,用于直接執(zhí)行SQL語(yǔ)句
- 2.update方法,用戶(hù)新增修改刪除操作
- 3.query方法,用于查詢(xún)方法
到此這篇關(guān)于SpringBoot使用JdbcTemplate訪(fǎng)問(wèn)操作數(shù)據(jù)庫(kù)基本用法的文章就介紹到這了,更多相關(guān)SpringBoot使用JdbcTemplate訪(fǎng)問(wèn)操作數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)
- SpringBoot中使用JdbcTemplate訪(fǎng)問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
- SpringBoot訪(fǎng)問(wèn)MongoDB數(shù)據(jù)庫(kù)的兩種方式
- SpringBoot整合數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)層的實(shí)戰(zhàn)
- SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)功能
- SpringBoot如何訪(fǎng)問(wèn)不同的數(shù)據(jù)庫(kù)的方法實(shí)現(xiàn)
相關(guān)文章
Java實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)管理的組合設(shè)計(jì)模式
Java組合模式是一種結(jié)構(gòu)型設(shè)計(jì)模式,它允許將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得用戶(hù)可以使用統(tǒng)一的方式處理單個(gè)對(duì)象和對(duì)象組合,從而簡(jiǎn)化了系統(tǒng)的設(shè)計(jì)和維護(hù)2023-04-04J2ee 高并發(fā)情況下監(jiān)聽(tīng)器實(shí)例詳解
這篇文章主要介紹了J2ee 高并發(fā)情況下監(jiān)聽(tīng)器實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02如何使用?Spring?Boot?搭建?WebSocket?服務(wù)器實(shí)現(xiàn)多客戶(hù)端連接
本文介紹如何使用SpringBoot快速搭建WebSocket服務(wù)器,實(shí)現(xiàn)多客戶(hù)端連接和消息廣播,WebSocket協(xié)議提供全雙工通信,SpringBoot通過(guò)@ServerEndpoint簡(jiǎn)化配置,支持實(shí)時(shí)消息推送,適用于聊天室或通知系統(tǒng)等應(yīng)用場(chǎng)景2024-11-11IDEA提示內(nèi)存不足 low memory的完美解決方法(親測(cè)好用)
這篇文章主要介紹了IDEA提示內(nèi)存不足 low memory的完美解決方法(親測(cè)好用),這里以IDEA2022版本為例,在IDE中 幫助(help)–>change memory setting(改變內(nèi)存設(shè)置),具體設(shè)置辦法文中給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01Spring Cloud 專(zhuān)題之Sleuth 服務(wù)跟蹤實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Cloud 專(zhuān)題之Sleuth 服務(wù)跟蹤,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05java.io.EOFException產(chǎn)生原因及解決方法(附代碼)
java.io.EOFException表示在讀取數(shù)據(jù)時(shí)突然遇到了文件或流的末尾,也就是說(shuō)客戶(hù)端或服務(wù)器已經(jīng)關(guān)閉了連接,但是你還在嘗試讀取數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java.io.EOFException產(chǎn)生原因及解決的相關(guān)資料,需要的朋友可以參考下2023-09-09Java中toString()、String.valueOf、(String)強(qiáng)轉(zhuǎn)區(qū)別
相信大家在日常開(kāi)發(fā)中這三種方法用到的應(yīng)該很多,本文主要介紹了Java中toString()、String.valueOf、(String)強(qiáng)轉(zhuǎn)區(qū)別,感興趣的可以了解一下2021-09-09java讀取某個(gè)文件夾下的所有文件實(shí)例代碼
這篇文章主要介紹了java讀取某個(gè)文件夾下的所有文件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03