SpringMVC?RESTFul實體類創(chuàng)建及環(huán)境搭建
一、搭建 mvc 環(huán)境
新建一個 module 模塊,創(chuàng)建 maven 工程,步驟跟以前一樣,各種配置文件內容也可以拷貝修改一下即可。
二、創(chuàng)建實體類
新建個 bean 包,創(chuàng)建實體類 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() { } }
三、準備 dao 模擬數(shù)據(jù)
新建一個 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 中實現(xiàn)了幾個增刪改查的操作,代替與數(shù)據(jù)庫的交互:
- map集合 employees,里存放了 5 個 Employee 對象。
- save()方法是保存,包含了添加操作和修改操作。
- getAll()是查詢所有,返回的是所有 Employee 對象的 value。
- get()是查詢單個員工信息,根據(jù) id 。
- delete()是根據(jù) id 刪除數(shù)據(jù)。
四、準備控制器
controller 包下新建 EmployeeController 類:
@Controller public class EmployeeController { @Autowired private EmployeeDao employeeDao; }
接下來就可以一個個的實現(xiàn)功能了,大概有:
訪問首頁查詢全部數(shù)據(jù)刪除跳轉到添加數(shù)據(jù)頁面執(zhí)行保存跳轉到更新數(shù)據(jù)頁面執(zhí)行更新
- 訪問首頁
- 查詢全部數(shù)據(jù)
- 刪除
- 跳轉到添加數(shù)據(jù)頁面
- 執(zhí)行保存
- 跳轉到更新數(shù)據(jù)頁面
- 執(zhí)行更新
感謝《尚硅谷》的學習資源。
以上就是SpringMVC RESTFul實體類創(chuàng)建及環(huán)境搭建的詳細內容,更多關于SpringMVC RESTFul實體類環(huán)境的資料請關注腳本之家其它相關文章!
相關文章
Flink開發(fā)IDEA環(huán)境搭建與測試的方法
這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03git stash 和unstash的使用操作,git unstash failed
這篇文章主要介紹了git stash 和unstash的使用操作,git unstash failed,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02