偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

深入理解Spring Boot架構(gòu)

開發(fā) 架構(gòu)
本文詳細(xì)介紹基于Spring Boot框架的常見層次結(jié)構(gòu)示例,幫助讀者更好地了解和應(yīng)用該框架。

本文的內(nèi)容有助于理解Java Spring Boot框架的層次結(jié)構(gòu)。

“我決定不讓自己徹底崩潰,而是每個周二晚上都讓自己小崩潰一下?!?—— Graham Parke

圖片

檢查任何軟件的最好方法是將其分成層,然后將這些層合并在一起。我們在這里遵循同樣的方法。

在深入研究Java Spring Boot之前,讓我們先來看一個眾所周知的例子——計算機(jī)網(wǎng)絡(luò)中的OSI模型。雖然網(wǎng)絡(luò)整體上看起來很復(fù)雜,但我們通常將其分成層次以組織協(xié)議。我們還聲明每個層都依賴于下面一層提供的服務(wù)。在Spring Boot中,同樣的原則也適用。

1 Spring Boot的層次結(jié)構(gòu)

圖片

我們主要可以將Spring Boot分成四層:

1.1 控制器層

系統(tǒng)與客戶端請求交互的第一部分是控制器。它們定義了API的端點,可以將端點想象為有效的路由和請求方法(GET、POST、PUT)??刂破鞯闹饕繕?biāo)是向客戶端提供服務(wù),即提供響應(yīng)、狀態(tài)等??刂破骼梅?wù)層提供的服務(wù)來為客戶端提供服務(wù)。

端點的示例:

  • /login (POST)
  • /register (POST)
  • /products (GET)

1.2 服務(wù)層

服務(wù)層旨在實現(xiàn)業(yè)務(wù)邏輯。服務(wù)層的主要目的是向控制器層提供服務(wù)。所有對數(shù)據(jù)的計算都在這一層中執(zhí)行,因此服務(wù)層需要數(shù)據(jù)。所以,它們依賴于DAO/Repository層提供的服務(wù)。

1.3 DAO/Repository層

DAO代表數(shù)據(jù)訪問對象,這一層的主要目標(biāo)是從數(shù)據(jù)庫中高效地訪問(查詢)數(shù)據(jù),并向服務(wù)層提供服務(wù)。

圖片

在Spring Boot中存在提供CRUD操作(創(chuàng)建、檢索、更新、刪除)的接口。因此,Repository層可以實現(xiàn)其中的一個。

1.4 模型層

模型表示現(xiàn)實世界中的對象,這些對象被稱為模型。JPA(Java Persistence API)提供了關(guān)于ORM(對象關(guān)系映射)的參考或詳細(xì)信息,這意味著Java類可以與數(shù)據(jù)庫表相關(guān)聯(lián)。在Spring Boot中存在許多JPA ORM的實現(xiàn),其中之一是Hibernate。因此,您需要現(xiàn)實世界實體的Java類,然后將其映射到關(guān)系(表)中。

2 上述層次結(jié)構(gòu)的實現(xiàn)模板

注意:對于實施,我們把項目管理作為一個主題。

2.1 控制器層:

ProjectController.java

package com.example.Controller;
//導(dǎo)入語句在此處
@RestController
public class UserController {
   
    //列出所有可用項目
    @GetMapping(path = "/projects", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Project>> getProjects() {
     
    // 執(zhí)行驗證檢查
    // 返回服務(wù)層提供的服務(wù)
    }
    //申請項目
    @PostMapping(path = "/apply-project", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<HttpStatus> applyProject(@RequestBody Map<String,String> json) {
    // 執(zhí)行驗證檢查
    // 返回服務(wù)層提供的服務(wù)
    }
    
    //上傳簡歷
    @PostMapping(path = "/upload-resume/{usn}")
    public ResponseEntity<List<Object>> uploadToDB(@RequestParam("file") MultipartFile[] file,@PathVariable String usn) {
    
    // 執(zhí)行驗證檢查
    // 返回服務(wù)層提供的服務(wù)
    }
    //下載簡歷
    @GetMapping("/files/download/{fileName:.+}")
    public ResponseEntity downloadFromDB(@PathVariable String fileName) {
    // 執(zhí)行驗證檢查
    // 返回服務(wù)層提供的服務(wù)
    }
}

上述示例使用了@注釋,這些注釋用于告知spring是否是RestController,PostMapping等。

2.2 服務(wù)層:

ProjectService.java

package com.example.Service;

// 導(dǎo)入語句

public interface ProjectService {

    ResponseEntity<List<Project>> getProjects();

    HttpStatus applyProject(String USN,int project_id);

    ResponseEntity<List<Object>> uploadProjectDocument(MultipartFile[] files,int project_id);

}

ProjectServiceImpl.Java

package com.example.Service;

//導(dǎo)入語句
@Service
public class ProjectServiceImpl implements ProjectService {
//將DAO進(jìn)行依賴注入(Autowire)
  
    @Override
    public ResponseEntity<List<Project>> getProjects() {
        try {
           //利用DAO服務(wù)實現(xiàn)業(yè)務(wù)邏輯
        } catch (Exception e) {
            return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR) ;
        }
    }
   
    @Override
    public HttpStatus applyProject(String USN, int project_id) {
   
    //利用DAO服務(wù)實現(xiàn)業(yè)務(wù)邏輯
    }
  
   //輔助函數(shù)
    public ResponseEntity uploadToLocalFileSystem(MultipartFile file,int project_id) {
     
    }
    @Override
    public ResponseEntity<List<Object>> uploadProjectDocument(MultipartFile[] files,int project_id) {
       //利用DAO服務(wù)實現(xiàn)業(yè)務(wù)邏輯
    }

}

2.3 Repository/DAO層:

ProjectDAO.java

package com.example.Dao;

//導(dǎo)入語句

public interface ProjectDao extends JpaRepository<Project,Integer> {

//你也可以在JPA提供的CRUD操作之上包含本地查詢
//使用@Query注釋和相應(yīng)的函數(shù)在此處添加查詢

    @Query(value = "Your SQL query ",nativeQuery = true)
    public List<Project> getProjects();

}

}

2.4 模型層:

Project.java

package com.example.Entity;

//導(dǎo)入語句

@Entity
@Table(name = "project")
public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int project_id;
    @Column(nullable = false, name = "company_name")
    private String company_name;

    @Column(nullable = false, name = "description")
    private String description;

    @Column(nullable = false, name = "requirements")
    private String requirements;

    @Column(nullable = false, name = "manager")
    private String manager;
    @Column(nullable = false, name = "start_date")
    private Date start_date = new Date();

    @Column( name = "end_date")
    private Date end_date = new Date();
    @Column(nullable = false,name = "opening")
    private int opening;
    @Column(name = "resources")
    private String resources;
    public Set<Staff> getStaff_incharge() {
        return staff_incharge;
    }
    public void setStaff_incharge(Set<Staff> staff_incharge) {
        this.staff_incharge = staff_incharge;
    }
    public Set<Student> getApplied_students() {
        return applied_students;
    }
    public Set<Document> getDocuments() {
        return documents;
    }
    public void setDocuments(Set<Document> documents) {
        this.documents = documents;
    }
    @JsonIgnore
    @ManyToMany(mappedBy="funded_projects")
    private Set<Fund> funds;
    public Set<Fund> getFunds() {
        return funds;
    }
    public void setFunds(Set<Fund> funds) {
        this.funds = funds;
    }
    public void setApplied_students(Set<Student> applied_students) {
        this.applied_students = applied_students;
    }
    public Set<Student> getWorking_students() {
        return working_students;
    }
    public void setWorking_students(Set<Student> working_students) {
        this.working_students = working_students;
    }
//構(gòu)造函數(shù)
    public Project() {
        super();
    }
    public Project(int project_id, String company_name, String description, String requirements, String manager, Date start_date, Date end_date, int opening, String resources) {
        super();
        this.project_id = project_id;
        this.company_name = company_name;
        this.description = description;
        this.requirements = requirements;
        this.manager = manager;
        this.start_date = start_date;
        this.end_date = end_date;
        this.opening = opening;
        this.resources = resources;
    }
    public int getProject_id() {
        return project_id;
    }
    public void setProject_id(int project_id) {
        this.project_id = project_id;
    }
    public String getCompany_name() {
        return company_name;
    }
    public void setCompany_name(String company_name) {
        this.company_name = company_name;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getRequirements() {
        return requirements;
    }
    public void setRequirements(String requirements) {
        this.requirements = requirements;
    }
    public String getManager() {
        return manager;
    }
    public void setManager(String manager) {
        this.manager = manager;
    }
    public Date getStart_date() {
        return start_date;
    }
    public void setStart_date(Date start_date) {
        this.start_date = start_date;
    }
    public Date getEnd_date() {
        return end_date;
    }
    public void setEnd_date(Date end_date) {
        this.end_date = end_date;
    }
    public int getOpening() {
        return opening;
    }
    public void setOpening(int opening) {
        this.opening = opening;
    }
    public String getResources() {
        return resources;
    }
    public void setResources(String resources) {
        this.resources = resources;
    }
    @Override
    public String toString() {
        return "Project{" +
                "project_id=" + project_id +
                ", company_name='" + company_name + '\'' +
                ", descriptinotallow='" + description + '\'' +
                ", requirements='" + requirements + '\'' +
                ", manager='" + manager + '\'' +
                ", start_date=" + start_date +
                ", end_date=" + end_date +
                ", opening=" + opening +
                ", resources='" + resources + '\'' +
                '}';
    }
}

在上面的示例中,該類表示一個表,其數(shù)據(jù)成員表示表的屬性。我們還可以使用OneToOne、ManyToOne、ManyToMany注釋表示表之間的關(guān)系。

上述實現(xiàn)是不完整的,因為本文的目的是了解工作流程和層次結(jié)構(gòu)。Spring Boot非常龐大,本文只涵蓋了其中的一小部分。如果本文有任何錯誤,在此深表歉意,希望對您有所幫助,謝謝!

責(zé)任編輯:武曉燕 來源: Java學(xué)研大本營
相關(guān)推薦

2017-08-15 13:05:58

Serverless架構(gòu)開發(fā)運維

2021-03-10 10:55:51

SpringJava代碼

2018-12-27 12:34:42

HadoopHDFS分布式系統(tǒng)

2018-04-16 11:04:23

HBaseRegion Serv數(shù)據(jù)庫

2019-03-18 09:50:44

Nginx架構(gòu)服務(wù)器

2022-01-14 12:28:18

架構(gòu)OpenFeign遠(yuǎn)程

2010-06-01 15:25:27

JavaCLASSPATH

2016-12-08 15:36:59

HashMap數(shù)據(jù)結(jié)構(gòu)hash函數(shù)

2020-07-21 08:26:08

SpringSecurity過濾器

2022-08-22 08:04:25

Spring事務(wù)Atomicity

2025-01-23 08:53:15

2021-09-03 09:55:43

架構(gòu)Yarn內(nèi)部

2023-01-16 18:32:15

架構(gòu)APNacos

2019-09-24 13:41:22

Hadoop面試分布式

2023-10-19 11:12:15

Netty代碼

2021-02-17 11:25:33

前端JavaScriptthis

2009-09-25 09:14:35

Hibernate日志

2013-09-22 14:57:19

AtWood

2020-09-23 10:00:26

Redis數(shù)據(jù)庫命令

2019-06-25 10:32:19

UDP編程通信
點贊
收藏

51CTO技術(shù)棧公眾號