亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

5分鐘快速學會spring boot整合JdbcTemplate的方法

 更新時間:2019年12月19日 14:34:34   作者:猿醫(yī)生  
這篇文章主要給大家介紹了如何通過5分鐘快速學會spring boot整合JdbcTemplate的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot整合JdbcTemplate具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

一、前言

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應用開發(fā)領域(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 '狀態(tài):-1-刪除;1-正常;',
 `create_time` datetime NOT NULL COMMENT '創(chuàng)建時間',
 `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、數(shù)據(jù)庫結果 - 如下圖所示 :

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

最新評論