5分钟快速学会spring boot整合JdbcTemplate的方法
作者:猿医生 时间:2022-04-01 15:56:11
一、前言
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。本文主要介绍的是关于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 驱动 -->
<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
*/
@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、启动类
@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 '状态:-1-删除;1-正常;',
`create_time` datetime NOT NULL COMMENT '创建时间',
`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、页面结果 - 如下图所示 :
b、数据库结果 - 如下图所示 :
来源:https://segmentfault.com/a/1190000021333213
标签:springboot,整合,jdbctemplate
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter状态管理Bloc使用示例详解
2023-08-24 09:09:10
![](https://img.aspxhome.com/file/2023/0/98690_0s.jpg)
Java中计算时间差的方法
2023-11-15 10:35:44
Java 实现判定顺序表中是否包含某个元素(思路详解)
2023-09-11 01:03:04
![](https://img.aspxhome.com/file/2023/4/58474_0s.png)
java文件操作之Path,Paths,Files
2023-11-19 15:34:34
java中JVM中如何存取数据和相关信息详解
2023-08-10 03:49:46
![](https://img.aspxhome.com/file/2023/8/58048_0s.jpg)
Java回调函数实例代码详解
2023-11-23 18:22:26
JavaWeb后台购物车类实现代码详解
2023-11-09 23:11:26
java读取文件内容的三种方法代码片断分享(java文件操作)
2023-11-21 06:53:20
java设计模式--三种工厂模式详解
2023-11-29 09:54:06
![](https://img.aspxhome.com/file/2023/7/60337_0s.png)
Spring Boot使用Allatori代码混淆的方法
2023-11-24 16:34:55
![](https://img.aspxhome.com/file/2023/0/59080_0s.jpg)
java文件上传下载代码实例
2023-11-10 05:06:14
java数据结构与算法之noDups去除重复项算法示例
2023-06-19 08:50:34
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
Android+SQLite数据库实现的生词记事本功能实例
2023-06-18 10:41:35
SpringBoot如何使用Fastjson解析Json数据
2023-11-25 11:55:58
![](https://img.aspxhome.com/file/2023/1/60041_0s.png)
基于Java8实现提高Excel读写效率
2023-11-25 10:01:37
Android中TextView动态设置缩进距离的方法
2023-08-07 09:52:00
![](https://img.aspxhome.com/file/2023/4/86754_0s.png)
Spring Boot启动过程全面解析(三)
2023-09-13 13:16:39
Java经典面试题最全汇总208道(六)
2023-11-23 17:52:45
![](https://img.aspxhome.com/file/2023/4/59064_0s.jpg)
Springboot导出文件,前端下载文件方式
2023-07-21 11:27:05
![](https://img.aspxhome.com/file/2023/4/60774_0s.jpg)