1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-26 19:20:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

改正telephone拼写错误;添加viewobject;修正age类型错误

This commit is contained in:
程序员小墨 2022-03-01 17:31:51 +08:00
parent efd4e04343
commit 829cafa95d
4 changed files with 94 additions and 25 deletions

View File

@ -16,22 +16,28 @@
> http://localhost:8090/user/get?id=1 > http://localhost:8090/user/get?id=1
预期效果:
```json
{"id":1,"name":"admin","gender":2,"age":18,"telephone":"110"}
```
### 层次结构以User为例自上向下 ### 层次结构以User为例自上向下
| | 目录 | Java对象类型 | 说明 | 举例 | | | 目录 | Java对象类型 | 说明 | 举例 |
| ----------------------------- |-----------------------| ----------------- | ----------------------- | ----------------------------- | | ----------------------------- | --------------------- | ----------------- | ---------------------------------------- | ---------------------------- |
| **<nobr>Controller层</nobr>** | | | | | | **<nobr>Controller层</nobr>** | | | | |
| Controller | controller | 类 class | | controller.UserController | | Controller | controller | 类 class | | controller.UserController |
| Controller | controller/viewobject | 类 class | | controller.UserController | | View Object | controller/viewobject | 类 class | 将用户Model转化为可供UI使用的View Object | controller.UserController |
| | | | | | | | | | | |
| **<nobr>Service层</nobr>** | 转换成业务模型 | | | | | **<nobr>Service层</nobr>** | 转换成业务模型 | | | |
| Service | service | 接口 interface | | service.UserService | | Service | service | 接口 interface | | service.UserService |
| ServiceImpl | service/impl | Service接口实现类 | 将DataObject组装成Model | service.impl.UserServiceImpl | | ServiceImpl | service/impl | Service接口实现类 | 将DataObject组装成Model | service.impl.UserServiceImpl |
| Model | service/model | 类 class | 用户模型Model | service.model.UserModel | | Model | service/model | 类 class | 用户模型Model | service.model.UserModel |
| | | | | | | | | | | |
| **<nobr>Dao层</nobr>** | 对数据库的映射 | | | | | **<nobr>Dao层</nobr>** | 对数据库的映射 | | | |
| Mapper | dao | 接口 interface | | dao.UserDOMapper | | Mapper | dao | 接口 interface | | dao.UserDOMapper |
| Mapping | resources/mapping | Mapper接口实现类 | xml格式SQL语句 | mapping/UserDOMapper.xml | | Mapping | resources/mapping | Mapper接口实现类 | xml格式SQL语句 | mapping/UserDOMapper.xml |
| Data Object | dataobject | 类 class | | dataobject.UserDO | | Data Object | dataobject | 类 class | | dataobject.UserDO |
**Tips:** Model与Data Object并非完全一一对应例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。 **Tips:** Model与Data Object并非完全一一对应例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。

View File

@ -1,8 +1,10 @@
package com.cxyxiaomo.flashsale.controller; package com.cxyxiaomo.flashsale.controller;
import com.cxyxiaomo.flashsale.controller.viewobject.UserVO;
import com.cxyxiaomo.flashsale.service.UserService; import com.cxyxiaomo.flashsale.service.UserService;
import com.cxyxiaomo.flashsale.service.model.UserModel; import com.cxyxiaomo.flashsale.service.model.UserModel;
import org.apache.catalina.User; import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -18,9 +20,20 @@ public class UserController {
@RequestMapping("/get") @RequestMapping("/get")
@ResponseBody @ResponseBody
public UserModel getUser(@RequestParam(name = "id") Integer id) { public UserVO getUser(@RequestParam(name = "id") Integer id) {
// 调用Services服务获取对应id的用户对象并返回给前端 // 调用Services服务获取对应id的用户对象并返回给前端
UserModel userModel = userService.getUserById(id); UserModel userModel = userService.getUserById(id);
return userModel; // 将核心领域模型用户对象转化为可供UI使用的viewobject
return convertFromModel(userModel);
}
private UserVO convertFromModel(UserModel userModel) {
if (userModel == null) {
return null;
}
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userModel, userVO);
return userVO;
} }
} }

View File

@ -0,0 +1,50 @@
package com.cxyxiaomo.flashsale.controller.viewobject;
public class UserVO {
private Integer id;
private String name;
private Byte gender;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getGender() {
return gender;
}
public void setGender(Byte gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
private String telephone;
}

View File

@ -4,8 +4,8 @@ public class UserModel {
private Integer id; private Integer id;
private String name; private String name;
private Byte gender; private Byte gender;
private String age; private Integer age;
private String telphone; private String telephone;
private String registerMode; private String registerMode;
private String thirdPartyId; private String thirdPartyId;
@ -35,20 +35,20 @@ public class UserModel {
this.gender = gender; this.gender = gender;
} }
public String getAge() { public Integer getAge() {
return age; return age;
} }
public void setAge(String age) { public void setAge(Integer age) {
this.age = age; this.age = age;
} }
public String getTelphone() { public String getTelephone() {
return telphone; return telephone;
} }
public void setTelphone(String telphone) { public void setTelephone(String telephone) {
this.telphone = telphone; this.telephone = telephone;
} }
public String getRegisterMode() { public String getRegisterMode() {