温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot整合JdbcTemplate的方法是什么

发布时间:2021-10-15 18:02:56 来源:亿速云 阅读:82 作者:柒染 栏目:编程语言

springboot整合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: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 '状态:-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、数据库结果 - 如下图所示 :

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI