SpringBoot整合JDBC的實(shí)現(xià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)@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ā),比如上面測(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'; } /** * 可以通過占位符實(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送3. python 如何停止一個(gè)死循環(huán)的線程4. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值5. php5.6不能擴(kuò)展redis.so的解決方法6. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總7. 關(guān)于HTML5的img標(biāo)簽8. Python編寫nmap掃描工具9. python 爬取嗶哩嗶哩up主信息和投稿視頻10. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密
