最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 2.0快速構(gòu)建服務(wù)組件全步驟

 更新時間:2019年04月11日 11:41:45   作者:羅摩爾  
這篇文章主要給大家介紹了關(guān)于Spring Boot 2.0快速構(gòu)建服務(wù)組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot 2.0具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

所謂的服務(wù)組件(Service Component)— 就是用于處理系統(tǒng)業(yè)務(wù)邏輯的類,如果按照系統(tǒng)分層設(shè)計理論來劃分,服務(wù)組件是位于業(yè)務(wù)層當(dāng)中的類。在Spring Boot中,服務(wù)組件是一個被**@Service**注解進(jìn)行注釋的類,這些類用于編寫系統(tǒng)的業(yè)務(wù)代碼。在本章節(jié)中,將講解如何創(chuàng)建并使用服務(wù)組件。

在開始正文之前,先來看兩段示例代碼。使用服務(wù)組件之前,我們需要定義服務(wù)組件接口類,用于索引服務(wù)組件提供的服務(wù),代碼如下所示:

public interface UserService{
 // TODO ...
}

然后,需要使用**@Service**注解對服務(wù)組件接口實現(xiàn)類進(jìn)行注釋,演示代碼如下:

@Service(value="userService")
public class UserServiceImpl implements UserService{
 //TODO ...
}

最后,使用**@Autowired**注解來自動引用服務(wù)組件,代碼如下:

@Controller
public class DemoController{
 @Autowired
 UserService userService;
 //TODO ...
}

在本次講解中,我們依然以對用戶的增、刪、改、查為案例,將控制器中的業(yè)務(wù)方法遷移到服務(wù)組件中。

1. 創(chuàng)建服務(wù)接口

創(chuàng)建一個包含添加用戶、更新用戶、刪除用戶和查詢用戶的服務(wù)接口類 — 用戶服務(wù)組件接口類。詳細(xì)代碼如下:

package com.ramostear.application.service;

import com.ramostear.application.model.User;

import java.util.Collection;

/**
 * Created by ramostear on 2019/3/11 0011.
 */
public interface UserService {

 /**
 * create user
 * @param user
 */
 void create(User user);

 /**
 * update user info by ID
 * @param id
 * @param user
 */
 void update(long id,User user);

 /**
 * delete user by ID
 * @param id
 */
 void delete(long id);

 /**
 * query all user
 * @return
 */
 Collection<User> findAll();
}

2. 實現(xiàn)服務(wù)接口

創(chuàng)建一個接口實現(xiàn)類,用于實現(xiàn)其中的增、刪、改、查四個業(yè)務(wù)方法,并用**@Service**注解進(jìn)行標(biāo)注,具體代碼如下:

package com.ramostear.application.service.impl;

import com.ramostear.application.model.User;
import com.ramostear.application.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ramostear
 * @create-time 2019/3/11 0011-4:29
 * @modify by :
 * @since:
 */
@Service(value="userService")
public class UserServiceImpl implements UserService {

 private static Map<Long,User> userRepo = new HashMap<>();

 @PostConstruct
 public void initUserRepo(){
 User admin = new User();
 admin.setId(1).setName("admin");
 userRepo.put(admin.getId(),admin);

 User editor = new User();
 editor.setId(2).setName("editor");
 userRepo.put(editor.getId(),editor);
 }
 @Override
 public void create(User user) {
 userRepo.put(user.getId(),user);
 }

 @Override
 public void update(long id, User user) {
 userRepo.remove(id);
 user.setId(id);
 userRepo.put(id,user);
 }

 @Override
 public void delete(long id) {
 userRepo.remove(id);
 }

 @Override
 public Collection<User> findAll() {
 return userRepo.values();
 }
}

3. 使用服務(wù)組件

接下來,定義一個用戶控制器,使用**@Autowired**注解來應(yīng)用用戶服務(wù)組件,實現(xiàn)對用戶的增、刪、改、查功能:

package com.ramostear.application.controller;

import com.ramostear.application.model.User;
import com.ramostear.application.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author ramostear
 * @create-time 2019/3/11 0011-4:42
 * @modify by :
 * @since:
 */
@RestController
public class UserController {

 @Autowired
 UserService userService;


 @GetMapping("/users")
 public ResponseEntity<Object> users(){
 return new ResponseEntity<>(userService.findAll(), HttpStatus.OK);
 }

 @PostMapping("/users")
 public ResponseEntity<Object> create(@RequestBody User user){
 userService.create(user);
 return new ResponseEntity<>("User is created successfully.",HttpStatus.CREATED);
 }

 @PutMapping("/users/{id}")
 public ResponseEntity<Object> update(@PathVariable(name="id") long id,@RequestBody User user){
 userService.update(id,user);
 return new ResponseEntity<>("User is updated successfully.",HttpStatus.OK);
 }

 @DeleteMapping("/users/{id}")
 public ResponseEntity<Object> delete(@PathVariable(name = "id")long id){
 userService.delete(id);
 return new ResponseEntity<>("User is deleted successfully.",HttpStatus.OK);
 }
}

4. 數(shù)據(jù)模型

用戶對象的代碼沿用以往章節(jié)的User.java代碼:

package com.ramostear.application.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author ramostear
 * @create-time 2019/3/6 0006-3:12
 * @modify by :
 * @since:
 */
@Getter
@Setter
@NoArgsConstructor
public class User {
 private long id;
 private String name;

 public User setId(long id){
 this.id = id;
 return this;
 }

 public User setName(String name){
 this.name = name;
 return this;
 }
}

注:應(yīng)用程序主類和Maven build文件與之前章節(jié)的代碼形同,不再列舉。

5. 運(yùn)行測試

啟動Spring Boot應(yīng)用程序,然后打開Postman測試應(yīng)用程序,分別進(jìn)行如下的測試。

GET 請求:獲取所有的用戶信息。

URL地址:http://localhost:8080/users

獲取用戶信息

POST 請求:新增一位用戶信息

URL地址:http://localhost:8080/users

請求參數(shù):{“id”:3,"name":"reader"}

新增用戶

PUT請求:修改用戶信息

URL地址:http://localhost:8080/users/3

請求參數(shù):{“id”:3,"name":"ramostear"}

 

修改用戶

DELETE請求:刪除用戶信息

URL地址:http://localhost:8080/users/3

刪除用戶

6. 附件

本章節(jié)用于演示的項目源碼已經(jīng)上傳到Github代碼倉庫,你可以通過下面的地址鏈接免費(fèi)獲取本章節(jié)的全部源碼信息:

github.com/ramostear/S …(本地下載

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

相關(guān)文章

  • 帶大家深入了解Spring事務(wù)

    帶大家深入了解Spring事務(wù)

    Spring框架提供統(tǒng)一的事務(wù)抽象,通過統(tǒng)一的編程模型使得應(yīng)用程序可以很容易地在不同的事務(wù)框架之間進(jìn)行切換. 在學(xué)習(xí)Spring事務(wù)前,我們先對數(shù)據(jù)庫事務(wù)進(jìn)行簡單的介紹。,需要的朋友可以參考下
    2021-05-05
  • Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串示例

    Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串示例

    這篇文章主要介紹了Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串,簡單描述了動態(tài)規(guī)劃法的概念、原理,并結(jié)合實例形式分析了Java使用動態(tài)規(guī)劃法求最長公共子序列以及最長公共子字符串相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • JAVA 多態(tài)操作----父類與子類轉(zhuǎn)換問題實例分析

    JAVA 多態(tài)操作----父類與子類轉(zhuǎn)換問題實例分析

    這篇文章主要介紹了JAVA 多態(tài)操作----父類與子類轉(zhuǎn)換問題,結(jié)合實例形式分析了JAVA 多態(tài)操作中父類與子類轉(zhuǎn)換問題相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下
    2020-05-05
  • Spring IOC推導(dǎo)與DI構(gòu)造器注入超詳細(xì)講解

    Spring IOC推導(dǎo)與DI構(gòu)造器注入超詳細(xì)講解

    這篇文章主要介紹了Spring IOC推導(dǎo)與DI構(gòu)造器注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • SpringBoot整合MinIO實現(xiàn)文件存儲系統(tǒng)的代碼示例

    SpringBoot整合MinIO實現(xiàn)文件存儲系統(tǒng)的代碼示例

    在現(xiàn)代的應(yīng)用程序中,文件存儲和管理是一個常見的需求,MinIO是一個開源的對象存儲系統(tǒng),與Spring?Boot框架結(jié)合使用,可以快速構(gòu)建高性能的文件存儲系統(tǒng),本文將介紹如何使用Spring?Boot和MinIO來實現(xiàn)文件存儲系統(tǒng)
    2023-06-06
  • 為什么不要使用 async void的原因分析

    為什么不要使用 async void的原因分析

    這篇文章主要介紹了為什么不要使用 async void的原因分析及解決方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • springmvc不進(jìn)入Controller導(dǎo)致404的問題

    springmvc不進(jìn)入Controller導(dǎo)致404的問題

    這篇文章主要介紹了springmvc不進(jìn)入Controller導(dǎo)致404的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程

    SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程

    Swagger2是一個開源項目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Mybatis resultType返回結(jié)果為null的問題排查方式

    Mybatis resultType返回結(jié)果為null的問題排查方式

    這篇文章主要介紹了Mybatis resultType返回結(jié)果為null的問題排查方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java?8函數(shù)式接口之Consumer用法示例詳解

    Java?8函數(shù)式接口之Consumer用法示例詳解

    這篇文章主要為大家介紹了Java?8函數(shù)式接口之Consumer用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論

大埔区| 孝感市| 都匀市| 满洲里市| 长治市| 巴林左旗| 横山县| 阆中市| 南岸区| 晋城| 安阳县| 万安县| 天气| 灌南县| 岫岩| 来安县| 湘乡市| 定兴县| 连城县| 石门县| 盈江县| 奎屯市| 唐河县| 柳河县| 海门市| 东山县| 洱源县| 雅安市| 武宣县| 温泉县| 临洮县| 调兵山市| 黄平县| 天祝| 丽水市| 东乌珠穆沁旗| 安阳县| 临沭县| 美姑县| 东兰县| 喜德县|