SpringBoot整合JDBC的實(shí)現(xiàn)
簡(jiǎn)介
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關(guān)系的都在Spring Data家族中,所以我們看看springboot中如何使用JDBC來實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查操作。
簡(jiǎn)單使用
引入依賴
這里我們只引入基本的依賴就好,創(chuàng)建一個(gè)springboot項(xiàng)目(這里版本是2.1.6),然后添加以下依賴:
<dependencies> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mysql驅(qū)動(dòng)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtimen</scope> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
編寫配置文件
這里我們需要把數(shù)據(jù)庫(kù)的基本連接信息配置好
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver ## 這里如果不配置時(shí)區(qū)可能會(huì)報(bào)錯(cuò),所以配置時(shí)區(qū):serverTimezone=UT url: jdbc:mysql://localhost:3306/study_springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: root
編寫測(cè)試類
@RunWith(SpringRunner.class) @SpringBootTest public class BaseTest { @Autowired private DataSource dataSource; @Test public void load(){ // 打印出:class com.zaxxer.hikari.HikariDataSource System.out.println(dataSource.getClass()); } }
實(shí)現(xiàn)增刪改查
spring boot中有很多的xxxTemplate,也就是給我們默認(rèn)配置了 很多的模板,方便我們進(jìn)行開發(fā),比如上面測(cè)試中的 JdbcTemplate,spring boot已經(jīng)給我們封裝好方法了,我們只要調(diào)用就好,下面是增刪改查的案例:
@RestController public class TestController { @Autowired private JdbcTemplate jdbcTemplate; @GetMapping("/userList") public List<Map<String, Object>> getUserList(){ String sql = "select * from study_springboot.user"; List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); return maps; } @GetMapping("/addUser") public String addUser(){ String sql = "insert into study_springboot.user(id, name, password) values('1', 'zhangsan', 'qqqq')"; jdbcTemplate.update(sql); return "add success"; } /** * 可以通過占位符實(shí)現(xiàn)入?yún)? * @param id * @return */ @GetMapping("/updateUser/{id}") public String updateUser(@PathVariable("id") int id){ String sql = "update study_springboot.user set name =?, password = ? where id = "+id; // 封裝占位符 Object[] objects = new Object[2]; objects[0] = "李四"; objects[1] = "pppppp"; jdbcTemplate.update(sql, objects); return "update success"; } @GetMapping("/deleteUser/{id}") public String deleteUser(@PathVariable("id") int id){ String sql = "delete from study_springboot.user where id = ?"; // int 類型也是一個(gè)object,所以這樣傳參也是可以的 jdbcTemplate.update(sql, id); return "delete success"; } }
上面的案例只是展示基本的操作,但是真實(shí)項(xiàng)目中是不會(huì)這樣寫的,一般還是整合MyBatis或者JPA來實(shí)現(xiàn)操作數(shù)據(jù)源。
到此這篇關(guān)于SpringBoot整合JDBC的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- 關(guān)于Spring Boot對(duì)jdbc的支持問題
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫(kù)分表功能
- Springboot jdbctemplate整合實(shí)現(xiàn)步驟解析
- Spring JdbcTemplate整合使用方法及原理詳解
- Spring jdbc具名參數(shù)使用方法詳解
- 詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫(kù)的幾種方式
- Spring JDBC的使用詳解
相關(guān)文章
Java獲取時(shí)間打印到控制臺(tái)代碼實(shí)例
這篇文章主要介紹了Java獲取時(shí)間打印到控制臺(tái)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot項(xiàng)目如何設(shè)置權(quán)限攔截器和過濾器
這篇文章主要介紹了使用lombok時(shí)如何自定義get、set方法問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java中生產(chǎn)者消費(fèi)者問題總結(jié)
這篇文章主要介紹了Java中生產(chǎn)者消費(fèi)者問題總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java編程中實(shí)現(xiàn)歸并排序算法的實(shí)例教程
這篇文章主要介紹了Java編程中實(shí)現(xiàn)歸并排序算法的實(shí)例教程,包括自底向上的歸并排序的實(shí)現(xiàn)方法介紹,需要的朋友可以參考下2016-05-05Spring Security基于JWT登錄認(rèn)證的項(xiàng)目實(shí)踐
JWT被用來在身份提供者和服務(wù)提供者間傳遞被認(rèn)證的用戶身份信息,本文主要介紹了Spring Security基于JWT登錄認(rèn)證的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringBoot將logback替換成log4j2的操作步驟
文章介紹了如何在SpringBoot項(xiàng)目中將默認(rèn)的日志框架logback替換為log4j2,以利用log4j2的高性能異步日志記錄特性,特別是通過Disruptor實(shí)現(xiàn)的無(wú)鎖化隊(duì)列,提高了日志處理速度,同時(shí),文章提供了詳細(xì)的配置步驟,需要的朋友可以參考下2024-10-10Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請(qǐng)求參數(shù)注解的操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2024-04-04詳解如何使用tldb數(shù)據(jù)庫(kù)的java客戶端
這篇文章主要為大家介紹了如何使用tldb數(shù)據(jù)庫(kù)的java客戶端過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09