5分钟快速学会spring boot整合JdbcTemplate的方法
一、前言
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:
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、测试
a、页面结果 - 所示 :
?
b、数据库结果 - 所示 :
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对无名的支持。
同类资源
- Java基于springboot教务管理系统
Java基于springboot教务管理系统本文件感兴趣的可以参考一下,帮助学校管理教务系统,用一个帐号解决学校教务...
- Java EE企业级应用开发教程Spring+Spring MVC+MyBatis实验报告
JavaEE企业级应用开发教程Spring+SpringMVC+MyBatis实验报告本文件感兴趣的可以参考一下。...
- springBootNoMaven实现定时任务功能
springBootNoMaven本文件感兴趣的可以参考一下,不使用maven环境,创建springboot项目,实现了定时任务功能简单...
- agilebpm-base-spring-boot.rar
agilebpm-base-spring-boot.rar本文件感兴趣的可以参考一下,基于activiti实现的审批工作流web端项目。...
- 数据库课设论文加源码mysql Java spring
数据库课设论文加源码mysqlJavaspring绿色版,数据库课程设计舍友信息管理系统,mysql,javaspring整篇论文。...
- 基于SpringMVC,Spring,Mybatis,BootStrap教务管理系统
基于SpringMVC,Spring,Mybatis,BootStrap教务管理系统绿色版,基础信息管理、系统权限管理、版本管理、子系统...
- SpringBoot健身房管理系统
SpringBoot健身房管理系统绿色版,springboot+MySQL+thymeleaf。...
- 使用springboot2.x整合shiro包含sql数据库
使用springboot2.x整合shiro包含sql数据库例子源代码。...