springboot整合JdbcTemplate的方法是什么

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、数据库结果 - 如下图所示 :

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

网站名称:springboot整合JdbcTemplate的方法是什么
新闻来源:https://www.cdcxhl.com/article22/jcjscc.html

成都网站建设公司_创新互联,为您提供软件开发网站维护定制网站营销型网站建设云服务器静态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站