springboot整合JdbcTemplate的方法是什么

springboot整合JdbcTemplate的方法是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、成都小程序開發(fā)、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動互聯(lián)開發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十年以來,已經(jīng)為數(shù)千家高空作業(yè)車租賃各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的數(shù)千家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。

一、前言

Spring Boot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。本文主要介紹的是關(guān)于springboot整合JdbcTemplate。

在此記錄下,分享給大家。

二、springboot整合JdbcTemplate

1、pom文件 依賴引入

<parent>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-parent</artifactId>  <version>2.1.8.RELEASE</version> </parent> <dependencies>  <!-- SpringBoot 整合 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>   <version>5.1.38</version>  </dependency>  <!-- SpringBoot 測試 -->  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-test</artifactId>   <scope>test</scope>  </dependency>  <!-- SpringBoot web組件 -->  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId>  </dependency> </dependencies>

2、 application.yml 新增配置

spring: datasource: url: jdbc:mysql://localhost:3306/yys_springboot_jdbc username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver

3、UserController.java

/** * 用戶管理 *  Controller * @author yys */@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @RequestMapping("/add") public String addUser(String userName, Integer age) {  return userService.addUser(userName, age) ? "success" : "fail"; }}

4、UserService.java

/** * 用戶管理 *  Service * @author yys */@Servicepublic class UserService { @Autowired private JdbcTemplate jdbcTemplate; public boolean addUser(String userName, Integer age) {  return jdbcTemplate.update("insert into yys_user values(null, ?, ?, 1, now(), now());", userName, age) > 0 ? true : false; }}

5、啟動類

@SpringBootApplicationpublic class YysApp { public static void main(String[] args) {  SpringApplication.run(YysApp.class, args); }}

6、初始化sql文件

CREATE TABLE `yys_user` ( `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'ID,自增列', `user_name` varchar(32) NOT NULL COMMENT '用戶名', `age` int(11) NOT NULL COMMENT '用戶年齡', `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '狀態(tài):-1-刪除;1-正常;', `create_time` datetime NOT NULL COMMENT '創(chuàng)建時間', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

7、測試

http://localhost:8080/user/add?userName=yys&age=18

a、頁面結(jié)果 - 如下圖所示 :

b、數(shù)據(jù)庫結(jié)果 - 如下圖所示 :

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)站名稱:springboot整合JdbcTemplate的方法是什么
新聞來源:http://muchs.cn/article22/jcjscc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)網(wǎng)站維護、定制網(wǎng)站、營銷型網(wǎng)站建設(shè)云服務(wù)器、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站