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

总结

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

(0)

相关推荐

  • 基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)

    1.pom添加依赖 <!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <

  • SpringBoot使用JdbcTemplate操作数据库

    前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTemplate. 新建项目 新建一个项目.pom文件中加入Jdbc依赖,完整pom如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM

  • SpringBoot JdbcTemplate批量操作的示例代码

    前言 在我们做后端服务Dao层开发,特别是大数据批量插入的时候,这时候普通的ORM框架(Mybatis.hibernate.JPA)就无法满足程序对性能的要求了.当然我们又不可能使用原生的JDBC进行操作,那样尽管效率会高,但是复杂度会上升. 综合考虑我们使用Spring中的JdbcTemplate和具名参数namedParameterJdbcTemplate来进行批量操作. 改造前 在开始讲解之前,我们首先来看下之前的JPA是如何批量操作的. 实体类User: public class App

  • springBoot使用JdbcTemplate代码实例

    springBoot使用JdbcTemplate 如果是通过spring自动注入的jdbcTemplate,配好application.properties在其他类中就能在其他类中直接使用. 如果通过new JdbcTemplate()出来的就需要自己配置DataSource. 自动注入如下 application.properties文件 spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC spri

  • SpringBoot用JdbcTemplates访问Mysql实例代码

    本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: -- create table `account` DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL,

  • 详解spring boot中使用JdbcTemplate

    本文将介绍如何将spring boot 与 JdbcTemplate一起工作. Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTemplate 是在JDBC API基础上提供了更抽象的封装,并提供了基于方法注解的事务管理能力. 通过使用SpringBoot自动配置功能并代替我们自动配置beans. 数据源配置 在maven中,我们需要增加spring-boot-starter-jdbc

  • springboot使用JdbcTemplate完成对数据库的增删改查功能

    首先新建一个简单的数据表,通过操作这个数据表来进行演示 DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `detail` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE

  • Spring boot 使用JdbcTemplate访问数据库

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物, 自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 Spring Framework 对数据库的操作在 JDBC 上面做了深层次的封装,通过 依赖注入 功能,可以将 DataSource 注册到 JdbcTemplate 之中,使我们可以轻易的完成对象关系映射,并有助于规避常见的错误,在 SpringBoot

  • Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    之前介绍了一些Web层的例子,包括构建RESTful API.使用Thymeleaf模板引擎渲染Web视图,但是这些内容还不足以构建一个动态的应用.通常我们做App也好,做Web应用也好,都需要内容,而内容通常存储于各种类型的数据库,服务端在接收到访问请求之后需要访问数据库获取并处理成展现给用户使用的数据形式. 本文介绍在Spring Boot基础下配置数据源和通过 JdbcTemplate 编写数据访问的示例. 数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同

  • Spring Boot中使用jdbctemplate 操作MYSQL数据库实例

    最近在学习使用Spring Boot连接数据库,今天学习了使用jdbctemplate 操作MYSQL数据库,下面就留个笔记 不废话,先来代码 pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org

随机推荐