溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringMVC?RESTFul實(shí)體類如何創(chuàng)建及環(huán)境搭建

發(fā)布時(shí)間:2022-05-30 09:52:46 來源:億速云 閱讀:158 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“SpringMVC RESTFul實(shí)體類如何創(chuàng)建及環(huán)境搭建”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一、搭建 mvc 環(huán)境

新建一個(gè) module 模塊,創(chuàng)建 maven 工程,步驟跟以前一樣,各種配置文件內(nèi)容也可以拷貝修改一下即可。

SpringMVC?RESTFul實(shí)體類如何創(chuàng)建及環(huán)境搭建

二、創(chuàng)建實(shí)體類

新建個(gè) bean 包,創(chuàng)建實(shí)體類 Employee:

package com.pingguo.rest.bean;
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    //1 male, 0 female
    private Integer gender;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public Employee(Integer id, String lastName, String email, Integer gender) {
        super();
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    }
    public Employee() {
    }
}

三、準(zhǔn)備 dao 模擬數(shù)據(jù)

新建一個(gè) dao 包,創(chuàng)建 EmployeeDao 類,這里不使用數(shù)據(jù)庫,直接存放一些靜態(tài)數(shù)據(jù):

@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    static{
        employees = new HashMap<Integer, Employee>();
        employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
        employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
        employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
        employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
        employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1));
    }
    private static Integer initId = 1006;
    public void save(Employee employee){
        if(employee.getId() == null){
            employee.setId(initId++);
        }
        employees.put(employee.getId(), employee);
    }
    public Collection<Employee> getAll(){
        return employees.values();
    }
    public Employee get(Integer id){
        return employees.get(id);
    }
    public void delete(Integer id){
        employees.remove(id);
    }
}

dao 中實(shí)現(xiàn)了幾個(gè)增刪改查的操作,代替與數(shù)據(jù)庫的交互:

  • map集合 employees,里存放了 5 個(gè) Employee 對(duì)象。

  • save()方法是保存,包含了添加操作和修改操作。

  • getAll()是查詢所有,返回的是所有 Employee 對(duì)象的 value。

  • get()是查詢單個(gè)員工信息,根據(jù) id 。

  • delete()是根據(jù) id 刪除數(shù)據(jù)。

四、準(zhǔn)備控制器

controller 包下新建 EmployeeController 類:

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
}

接下來就可以一個(gè)個(gè)的實(shí)現(xiàn)功能了,大概有:

訪問首頁查詢?nèi)繑?shù)據(jù)刪除跳轉(zhuǎn)到添加數(shù)據(jù)頁面執(zhí)行保存跳轉(zhuǎn)到更新數(shù)據(jù)頁面執(zhí)行更新

  • 訪問首頁

  • 查詢?nèi)繑?shù)據(jù)

  • 刪除

  • 跳轉(zhuǎn)到添加數(shù)據(jù)頁面

  • 執(zhí)行保存

  • 跳轉(zhuǎn)到更新數(shù)據(jù)頁面

  • 執(zhí)行更新

“SpringMVC RESTFul實(shí)體類如何創(chuàng)建及環(huán)境搭建”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI