詳解SpringBoot封裝使用JDBC
Spring Boot中可以在配置文件中直接進(jìn)行數(shù)據(jù)庫配置,
spring.datasource.username= root spring.datasource.password= 123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
SpringBoot可以直接生成數(shù)據(jù)庫對(duì)象
默認(rèn)數(shù)據(jù)源為Hikari
jdbc連接
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @SpringBootTest class DataSpringbootApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println("默認(rèn)數(shù)據(jù)源"); System.out.println(dataSource.getClass()); System.out.println("獲得數(shù)據(jù)庫連接"); Connection connection = dataSource.getConnection(); System.out.println(connection); System.out.println("關(guān)閉數(shù)據(jù)源"); connection.close(); } }
springboot中有很多template已經(jīng)寫好可以直接拿來用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; //查詢數(shù)據(jù)庫所有信息 @GetMapping("/userList") public List<Map<String,Object>> userList(){ String sql = "select * from user"; List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql); return mapList; } @GetMapping("/addUser") public String addUser(){ String sql = "insert into mybatis.user(id,name,pwd) values (4,'hhh','451651')"; jdbcTemplate.update(sql); return "update-ok"; } @GetMapping("/deleteUser/{id}") public String deleteUser(@PathVariable("id") int id){ String sql = "delete from mybatis.user where id = ?"; jdbcTemplate.update(sql,id); return "delete-ok"; } }
到此這篇關(guān)于SpringBoot封裝JDBC使用的文章就介紹到這了,更多相關(guān)SpringBoot封裝JDBC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用springboot不自動(dòng)初始化數(shù)據(jù)庫連接池
這篇文章主要介紹了使用springboot不自動(dòng)初始化數(shù)據(jù)庫連接池,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java之Error與Exception的區(qū)別案例詳解
這篇文章主要介紹了Java之Error與Exception的區(qū)別案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09springboot2.x只需兩步快速整合log4j2的方法
這篇文章主要介紹了springboot2.x只需兩步快速整合log4j2的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Java多線程Queue、BlockingQueue和使用BlockingQueue實(shí)現(xiàn)生產(chǎn)消費(fèi)者模型方法解析
這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實(shí)現(xiàn)生產(chǎn)消費(fèi)者模型方法解析,涉及queue,BlockingQueue等有關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以參考。2017-11-11SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收
這篇文章主要介紹了SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Java實(shí)現(xiàn)人臉識(shí)別登錄、注冊(cè)等功能(最新完整版)
這段時(shí)間由于學(xué)校實(shí)行靜態(tài)化管理,寢室門和校門都是用了人臉識(shí)別的裝置,本系列項(xiàng)目從設(shè)計(jì)到實(shí)現(xiàn)源碼全部開源免費(fèi)學(xué)習(xí)使用,對(duì)Java實(shí)現(xiàn)人臉識(shí)別登錄、注冊(cè)功能感興趣的朋友一起看看吧2022-05-05一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景
這篇文章主要為大家介紹了一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03