SpringBoot整合JDBC的實(shí)現(xiàn)
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關(guān)系的都在Spring Data家族中,所以我們看看springboot中如何使用JDBC來(lái)實(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>編寫(xiě)配置文件
這里我們需要把數(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編寫(xiě)測(cè)試類(lèi)
@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)行開(kāi)發(fā),比如上面測(cè)試中的 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'; } /** * 可以通過(guò)占位符實(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 類(lèi)型也是一個(gè)object,所以這樣傳參也是可以的 jdbcTemplate.update(sql, id); return 'delete success'; }}
上面的案例只是展示基本的操作,但是真實(shí)項(xiàng)目中是不會(huì)這樣寫(xiě)的,一般還是整合MyBatis或者JPA來(lái)實(shí)現(xiàn)操作數(shù)據(jù)源。
到此這篇關(guān)于SpringBoot整合JDBC的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot整合JDBC內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類(lèi)的方法步驟4. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理5. Django視圖類(lèi)型總結(jié)6. vue cli4下環(huán)境變量和模式示例詳解7. Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示?8. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)9. Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼10. 關(guān)于JavaScript對(duì)象類(lèi)型之Array及Object
