[恒达官网]平台登录注册开户链接唯一地址,集团秉承诚信为本,市场在变,诚信永远不变的经营理念,得到业界一致好评!
首页 恒达平台登录 正文

恒达注册会员,恒达登录平台_SpringMVC的项目架构头脑

[恒达官网]平台登录注册开户链接 ,一个服务全面、响应迅速、安全有保障的娱乐互动平台。10年来、我们坚持客户的利益至上原则,为客户提供恒达注册、恒达登录、恒达开户以及线路测速、代理招商等项目。恒达平台将全天免费提供快捷注册、开户以及信息发布等服务,并承诺7*24不间断在线!!!欢迎您注册和了解我们的相关信息。

SpringMVC的项目架构头脑

一、DO和Model的区别

DO是对数据库表的映射。Model是处置营业逻辑的模子(领域模子)。例如:用户信息表和用户密码表分别是两个DO,然则对于实体用户来说,密码表的字段也属于用户,以是需要在Service层使用Model来处置营业逻辑,Model包含了两张表的字段的并集。在Mapper层使用DO,在Service层使用Model。

public class UserDO {
    private Integer id;
    private String name;
    private Boolean gender;
    private Integer age;
    private String telphone;
    private String registerType;
    private String thirdPartyId;
    //getter、setter
public class UserPasswordDO {
    private Integer id;
    private String encrptPassword;
    private Integer userId;
    //getter、setter

二、VO和Model的区别

VO(View Object视图工具),用于前端展示。Model中包含了用户的密码数据,不应该通过Controller层返回给前端,这是不安全的操作。在Controller层通过VO返回部门可以展示到前端的数据即可。

public class UserModel {
    private Integer id;
    private String name;
    private Boolean gender;
    private Integer age;
    private String telphone;
    private String registerMode;
    private String thirdPartyId;
    private String encrptPassword;
    //getter、setter
public class UserVO {
    private Integer id;
    private String name;
    private Boolean gender;
    private Integer age;
    private String telphone;
    //getter、setter

三、条理结构

  1. dao层:界说XXXMapper接口、对应的XXXMapper.xml在resource目录下的mapper文件夹里,使用数据模子DO(Data Object)

  2. service层:界说XXXService接口、实现类XXXServiceImpl、领域模子Model

    @Service
    public class UserServiceImpl implements UserService {
    ​
        @Autowired
        private UserDOMapper userDOMapper;
        @Autowired
        private UserPasswordDOMapper userPasswordDOMapper;
    ​
        @Override
        public UserModel getUserById(Integer id) {
            UserDO userDO = userDOMapper.selectByPrimaryKey(id);
            if (userDO == null) {
                return null;
            }
            UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUid(userDO.getId());
            return covertFromDataObject(userDO, userPasswordDO);
        }
    ​
        private UserModel covertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
            if (userDO == null) {
                return null;
            }
            UserModel userModel = new UserModel();
            BeanUtils.copyProperties(userDO, userModel);
            if (userPasswordDO != null) {
                userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
            }
            return userModel;
        }
    }
  3. controller层:界说XXXController类、视图模子VO(View Object)

    @RestController("user")
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    ​
        @RequestMapping("/get")
        public UserVO getUser(@RequestParam(name = "id") Integer id){
            UserModel userModel = userService.getUserById(id);
            //将焦点的领域模子工具转化为可供UI使用的VO
            return convertFromModel(userModel);
        }
    ​
        private UserVO convertFromModel(UserModel userModel){
            if(userModel == null){
                return null;
            }
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userModel, userVO);
            return userVO;
        }
    }

四、归一化Controller层的响应信息

  1. 界说一个公共的返回值类型

    public class CommonReturnType {
        //处置效果 success/fail
        //fail 返回通用的错误码花样
        //success 返回数据
        private String status;
        private Object data;
    ​
        public static CommonReturnType create(Object result, String status) {
            CommonReturnType returnType = new CommonReturnType();
            returnType.setStatus(status);
            returnType.setData(result);
            return returnType;
        }
    ​
        public static CommonReturnType create(Object result) {
            return CommonReturnType.create(result, "success");
        }
    ​
        public String getStatus() {
            return status;
        }
    ​
        public void setStatus(String status) {
            this.status = status;
        }
    ​
        public Object getData() {
            return data;
        }
    ​
        public void setData(Object data) {
            this.data = data;
        }
    }
  2. 在Controller层返回CommonResultType

    @RequestMapping("/get")
    public CommonReturnType getUser(@RequestParam(name = "id") Integer id){
        UserModel userModel = userService.getUserById(id);
        //将焦点的领域模子工具转化为可供UI使用的VO
        UserVO userVO =  convertFromModel(userModel);
        //返回通用工具
        return CommonReturnType.create(userVO);
    }
  3. 前端获得的返回准确的效果

    {
        "status":"success",
        "data":{
            "id":1,
            "name":"张三",
            "gender":false,
            "age":12,
            "telphone":"123456"
        }
    }
  4. 异常效果的处置(见下一Part)

五、 包装器营业异常类的实现

  1. 界说一个公共错误接口/error

    public interface CommonError {
        public int getErrCode();
        public String getErrMsg();
        public CommonError setErrMsg(String errMsg);
    }
  2. 界说一个枚举错误类/error

    public enum EmBusinessError implements CommonError {
        //通用错误类型 00001
        PARAMETER_VALIDATION_ERROR(00001,"参数不合法"),
        //用户信息相关 1000X
        USER_NOT_EXIST(10001,"用户不存在")
        //
        ;
        private EmBusinessError(int errCode, String errMsg){
            this.errCode = errCode;
            this.errMsg = errMsg;
        }
        private int errCode;
        private String errMsg;
        @Override
        public int getErrCode() {
            return this.errCode;
        }
    ​
        @Override
        public String getErrMsg() {
            return this.errMsg;
        }
    ​
        @Override
        public CommonError setErrMsg(String errMsg) {
            this.errMsg = errMsg;
            return this;
        }
    }
  3. 界说异常包装类/exception

    //包装器营业异常类的实现
    public class BusinessException extends Exception implements CommonError {
        private CommonError commonError;
    ​
        //直接吸收EmBusinessError的传参用于组织营业异常
        public BusinessException(CommonError commonError){
            super();
            this.commonError = commonError;
        }
    ​
        //吸收自界说errMsg的方式组织营业异常
        public BusinessException(CommonError commonError, String errMsg){
            super();
            this.commonError = commonError;
            this.commonError.setErrMsg(errMsg);
        }
        @Override
        public int getErrCode() {
            return this.commonError.getErrCode();
        }
    ​
        @Override
        public String getErrMsg() {
            return this.commonError.getErrMsg();
        }
    ​
        @Override
        public CommonError setErrMsg(String errMsg) {
            this.commonError.setErrMsg(errMsg);
            return this;
        }
    }
  4. 在Controller层抛出异常

    @RequestMapping("/get")
    public CommonReturnType getUser(@RequestParam(name = "id") Integer id) throws BusinessException {
        UserModel userModel = userService.getUserById(id);
        if(userModel == null){
            throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
        }
        //将焦点的领域模子工具转化为可供UI使用的VO
        UserVO userVO =  convertFromModel(userModel);
        //返回通用工具
        return CommonReturnType.create(userVO);
    }

六、异常的处置

  1. 界说exceptionHandler解决未被Controller层吸收的exception

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public Object handlerException(HttpServletRequest request, Exception ex){
        BusinessException businessException = (BusinessException) ex;
        CommonReturnType commonReturnType = new CommonReturnType();
        commonReturnType.setStatus("fail");
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("errCode",businessException.getErrCode());
        responseData.put("errMsg",businessException.getErrMsg());
        commonReturnType.setData(responseData);
        return commonReturnType;
    }
  2. 前端获得的JSON数据

    {"status":"fail",
     "data":{
         "errCode":10001,
         "errMsg":"用户不存在"
     }
    }
  3. 优化异常处置,界说BaseController,其余Controller继续基类。

    @RestController
    public class BaseController {
        @ExceptionHandler(Exception.class)
        @ResponseStatus(HttpStatus.OK)
        public Object handlerException(HttpServletRequest request, Exception ex){
            Map<String, Object> responseData = new HashMap<>();
            if(ex instanceof BusinessException){
                BusinessException businessException = (BusinessException) ex;
                CommonReturnType commonReturnType = new CommonReturnType();
                commonReturnType.setStatus("fail");
                responseData.put("errCode",businessException.getErrCode());
                responseData.put("errMsg",businessException.getErrMsg());
            }
            else{
                responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
                responseData.put("errMsg",EmBusinessError.UNKNOWN_ERROR.getErrMsg());
            }
            return CommonReturnType.create(responseData,"fail");
        }
    }