SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
Spring
對數(shù)據(jù)庫的操作在jdbc上s面做了深層次的封裝,使用spring的注入功能,可以把DataSource
注冊到JdbcTemplate
之中。
JdbcTemplate
在Spring-jdbc
包下面,還需要Spring-tx
包支持,里面包含事務(wù)和異??刂?
一、建一個rumenz_springboot庫
創(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的依賴
<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í)體類
@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 類描述 ?* @author: 入門小站 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)類
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 類描述 ?* @author: 入門小站 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]="入門小站"+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測試
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 類描述 ?* @author: 入門小站 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("入門小站").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(); ? ? } ? ? //查詢數(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("入門小站-修改").domain("https://tooltt.com").build(); ? ? ? ? Integer reint = userService.update(user); ? ? ? ? return reint.toString(); ? ? } }
八、總結(jié)
常用CURD操作大致使用以下三個方法:
- 1.execute方法,用于直接執(zhí)行SQL語句
- 2.update方法,用戶新增修改刪除操作
- 3.query方法,用于查詢方法
到此這篇關(guān)于SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法的文章就介紹到這了,更多相關(guān)SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)樹形結(jié)構(gòu)管理的組合設(shè)計模式
Java組合模式是一種結(jié)構(gòu)型設(shè)計模式,它允許將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得用戶可以使用統(tǒng)一的方式處理單個對象和對象組合,從而簡化了系統(tǒng)的設(shè)計和維護(hù)2023-04-04J2ee 高并發(fā)情況下監(jiān)聽器實(shí)例詳解
這篇文章主要介紹了J2ee 高并發(fā)情況下監(jiān)聽器實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02如何使用?Spring?Boot?搭建?WebSocket?服務(wù)器實(shí)現(xiàn)多客戶端連接
本文介紹如何使用SpringBoot快速搭建WebSocket服務(wù)器,實(shí)現(xiàn)多客戶端連接和消息廣播,WebSocket協(xié)議提供全雙工通信,SpringBoot通過@ServerEndpoint簡化配置,支持實(shí)時消息推送,適用于聊天室或通知系統(tǒng)等應(yīng)用場景2024-11-11IDEA提示內(nèi)存不足 low memory的完美解決方法(親測好用)
這篇文章主要介紹了IDEA提示內(nèi)存不足 low memory的完美解決方法(親測好用),這里以IDEA2022版本為例,在IDE中 幫助(help)–>change memory setting(改變內(nèi)存設(shè)置),具體設(shè)置辦法文中給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01Spring Cloud 專題之Sleuth 服務(wù)跟蹤實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Cloud 專題之Sleuth 服務(wù)跟蹤,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08java數(shù)據(jù)庫批量插入數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了java數(shù)據(jù)庫批量插入數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05java.io.EOFException產(chǎn)生原因及解決方法(附代碼)
java.io.EOFException表示在讀取數(shù)據(jù)時突然遇到了文件或流的末尾,也就是說客戶端或服務(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ū)別
相信大家在日常開發(fā)中這三種方法用到的應(yīng)該很多,本文主要介紹了Java中toString()、String.valueOf、(String)強(qiáng)轉(zhuǎn)區(qū)別,感興趣的可以了解一下2021-09-09