SpringMVC?RESTFul實體類創(chuàng)建及環(huán)境搭建
一、搭建 mvc 環(huán)境
新建一個 module 模塊,創(chuàng)建 maven 工程,步驟跟以前一樣,各種配置文件內(nèi)容也可以拷貝修改一下即可。

二、創(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() {
}
}三、準(zhǔn)備 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ù)。
四、準(zhǔn)備控制器
controller 包下新建 EmployeeController 類:
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
}接下來就可以一個個的實現(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í)行更新
感謝《尚硅谷》的學(xué)習(xí)資源。
以上就是SpringMVC RESTFul實體類創(chuàng)建及環(huán)境搭建的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC RESTFul實體類環(huán)境的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Flink開發(fā)IDEA環(huán)境搭建與測試的方法
這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
git stash 和unstash的使用操作,git unstash failed
這篇文章主要介紹了git stash 和unstash的使用操作,git unstash failed,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

