SpringBoot整合JDBC的實現(xiàn)
簡介
JDBC是最原基本的連接數(shù)據(jù)源的方式,在springboot中所有和數(shù)據(jù)源有關系的都在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) @SpringBootTest public 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,也就是給我們默認配置了 很多的模板,方便我們進行開發(fā),比如上面測試中的 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"; } /** * 可以通過占位符實現(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ù)源。
到此這篇關于SpringBoot整合JDBC的實現(xiàn)的文章就介紹到這了,更多相關SpringBoot整合JDBC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- 關于Spring Boot對jdbc的支持問題
- SpringBoot多數(shù)據(jù)源配置詳細教程(JdbcTemplate、mybatis)
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯誤
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實現(xiàn)分庫分表功能
- Springboot jdbctemplate整合實現(xiàn)步驟解析
- Spring JdbcTemplate整合使用方法及原理詳解
- Spring jdbc具名參數(shù)使用方法詳解
- 詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式
- Spring JDBC的使用詳解
相關文章
SpringBoot將logback替換成log4j2的操作步驟
文章介紹了如何在SpringBoot項目中將默認的日志框架logback替換為log4j2,以利用log4j2的高性能異步日志記錄特性,特別是通過Disruptor實現(xiàn)的無鎖化隊列,提高了日志處理速度,同時,文章提供了詳細的配置步驟,需要的朋友可以參考下2024-10-10Spring 處理 HTTP 請求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請求參數(shù)注解的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友參考下吧2024-04-04詳解如何使用tldb數(shù)據(jù)庫的java客戶端
這篇文章主要為大家介紹了如何使用tldb數(shù)據(jù)庫的java客戶端過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09