SpringBoot整合JDBC的實現(xiàn)
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關(guān)系的都在Spring Data家族中,所以我們看看springboot中如何使用JDBC來實現(xiàn)對數(shù)據(jù)庫的增刪改查操作。
簡單使用引入依賴這里我們只引入基本的依賴就好,創(chuàng)建一個springboot項目(這里版本是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 ## 這里如果不配置時區(qū)可能會報錯,所以配置時區(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()); }}實現(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'; } /** * 可以通過占位符實現(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 類型也是一個object,所以這樣傳參也是可以的 jdbcTemplate.update(sql, id); return 'delete success'; }}
上面的案例只是展示基本的操作,但是真實項目中是不會這樣寫的,一般還是整合MyBatis或者JPA來實現(xiàn)操作數(shù)據(jù)源。
到此這篇關(guān)于SpringBoot整合JDBC的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP laravel實現(xiàn)導(dǎo)出PDF功能2. vscode運行php報錯php?not?found解決辦法3. Python使用Selenium自動進(jìn)行百度搜索的實現(xiàn)4. Python基于requests庫爬取網(wǎng)站信息5. 一文帶你徹底理解Java序列化和反序列化6. 微信小程序?qū)崿F(xiàn)商品分類頁過程結(jié)束7. Java commons-httpclient如果實現(xiàn)get及post請求8. JS中6個對象數(shù)組去重的方法9. 資深程序員:給Python軟件開發(fā)測試的25個忠告!10. python中文本字符處理的簡單方法記錄

網(wǎng)公網(wǎng)安備