如何5分钟内掌握Spring Boot集成JdbcTemplate技巧?

2026-05-28 06:101阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计769个文字,预计阅读时间需要4分钟。

如何5分钟内掌握Spring Boot集成JdbcTemplate技巧?

一、前言Spring Boot是由Pivotal团队提供的全新框架,旨在简化新Spring应用的初始搭建以及开发过程。其设计目的是简化Spring应用的初始搭建以及开发过程。

该框架使用了特定的方式来进行配置,从而使得开发者无需再定义样板化的配置文件。这样,开发人员就可以专注于业务逻辑,而无需花费大量时间在配置上。

一、前言

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。本文主要介绍的是关于springboot整合JdbcTemplate。

如何5分钟内掌握Spring Boot集成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、测试

localhost:8080/user/add?userName=yys&age=18

a、页面结果 - 如下图所示 :

b、数据库结果 - 如下图所示 :

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。

本文共计769个文字,预计阅读时间需要4分钟。

如何5分钟内掌握Spring Boot集成JdbcTemplate技巧?

一、前言Spring Boot是由Pivotal团队提供的全新框架,旨在简化新Spring应用的初始搭建以及开发过程。其设计目的是简化Spring应用的初始搭建以及开发过程。

该框架使用了特定的方式来进行配置,从而使得开发者无需再定义样板化的配置文件。这样,开发人员就可以专注于业务逻辑,而无需花费大量时间在配置上。

一、前言

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。本文主要介绍的是关于springboot整合JdbcTemplate。

如何5分钟内掌握Spring Boot集成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、测试

localhost:8080/user/add?userName=yys&age=18

a、页面结果 - 如下图所示 :

b、数据库结果 - 如下图所示 :

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。