SpringBoot整合JDBC的實(shí)現(xiàn)
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關(guān)系的都在Spring Data家族中,所以我們看看springboot中如何使用JDBC來實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查操作。
簡單使用引入依賴這里我們只引入基本的依賴就好,創(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ū)動--> <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ù)庫的基本連接信息配置好
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver ## 這里如果不配置時(shí)區(qū)可能會報(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編寫測試類
@RunWith(SpringRunner.class)@SpringBootTestpublic 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ā),比如上面測試中的 JdbcTemplate,spring boot已經(jīng)給我們封裝好方法了,我們只要調(diào)用就好,下面是增刪改查的案例:
@RestControllerpublic 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)目中是不會這樣寫的,一般還是整合MyBatis或者JPA來實(shí)現(xiàn)操作數(shù)據(jù)源。
到此這篇關(guān)于SpringBoot整合JDBC的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)2. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)3. jsp 實(shí)現(xiàn)的簡易mvc模式示例4. jscript與vbscript 操作XML元素屬性的代碼5. JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實(shí)例6. 基于PHP做個(gè)圖片防盜鏈7. XML在語音合成中的應(yīng)用8. Jsp servlet驗(yàn)證碼工具類分享9. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
