5分鐘快速學(xué)會(huì)spring boot整合JdbcTemplate的方法
一、前言
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開發(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ū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- SpringBoot 測(cè)試 --> <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
*/
@Service
public 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、啟動(dòng)類
@SpringBootApplication
public 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)建時(shí)間', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
7、測(cè)試
http://localhost:8080/user/add?userName=yys&age=18
a、頁(yè)面結(jié)果 - 如下圖所示 :
b、數(shù)據(jù)庫(kù)結(jié)果 - 如下圖所示 :

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- Springboot集成jdbcTemplate過程解析
- SpringBoot JdbcTemplate批量操作的示例代碼
- springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫(kù)的增刪改查功能
- 基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)
- SpringBoot用JdbcTemplates訪問Mysql實(shí)例代碼
- 詳解spring boot中使用JdbcTemplate
- Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫(kù)實(shí)例
- SpringBoot jdbctemplate使用方法解析
相關(guān)文章
Spring Cloud升級(jí)最新Finchley版本的所有坑
這篇文章主要介紹了Spring Cloud升級(jí)最新Finchley版本的所有坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-08-08
Java 如何使用Feign發(fā)送HTTP請(qǐng)求
這篇文章主要介紹了Java 如何使用Feign發(fā)送HTTP請(qǐng)求,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-11-11
idea?compile項(xiàng)目正常啟動(dòng)項(xiàng)目的時(shí)候build失敗報(bào)“找不到符號(hào)”等問題及解決方案
這篇文章主要介紹了idea?compile項(xiàng)目正常,啟動(dòng)項(xiàng)目的時(shí)候build失敗,報(bào)“找不到符號(hào)”等問題,這種問題屬于lombok編譯失敗導(dǎo)致,可能原因是依賴jar包沒有更新到最新版本,需要的朋友可以參考下2023-10-10
Spring 應(yīng)用中集成 Apache Shiro的方法
這篇文章主要介紹了Spring 應(yīng)用中集成 Apache Shiro的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-05-05
Java服務(wù)器主機(jī)信息監(jiān)控工具類的示例代碼
這篇文章主要介紹了Java服務(wù)器主機(jī)信息監(jiān)控工具類的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
教你怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧
本篇文章為大家詳細(xì)介紹了怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧,文中有非常詳細(xì)的代碼示例及注釋,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05

