代码规范
Nuyoah VO包
当我们给前台传数据的时候,可能不想要将所有的数据项都传给前台,这时候需要使用VO映射类来封装我们的 数据
domain/vo/实体类名字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.nuyoah.domain.vo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@Data @NoArgsConstructor @AllArgsConstructor public class HotArticleVo { private Long id; private String title; private Long viewCount; }
|
实体对象转换成VO对象
service/impl/service对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.nuyoah.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fasterxml.jackson.databind.util.BeanUtil; import com.nuyoah.domain.ResponseResult; import com.nuyoah.domain.entity.Article; import com.nuyoah.domain.vo.HotArticleVo; import com.nuyoah.mapper.ArticleMapper; import com.nuyoah.service.ArticleService;
import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.List;
@Service public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService { @Override public ResponseResult hostArticleList() { LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(Article::getStatus, 0); lambdaQueryWrapper.orderByAsc(Article::getViewCount); Page<Article> page = new Page<>(1, 10); page(page, lambdaQueryWrapper);
List<Article> articles = page.getRecords(); List<HotArticleVo> hotArticleVos = new ArrayList<>(); for (Article article : articles) { HotArticleVo vo = new HotArticleVo(); BeanUtils.copyProperties(article, vo); hotArticleVos.add(vo); } return ResponseResult.okResult(hotArticleVos); }
}
|
封装工具类将Bean转换成VO的代码封装一下
utils
返回前台状态和对象
我们可以封装一个类来给前台返回对象,例如状态信息,文字信息,数据信息
enums/AppHttpCodeEnum.java前台状态码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.nuyoah.enums;
public enum AppHttpCodeEnum { SUCCESS(200,"操作成功"), NEED_LOGIN(401,"需要登录后操作"), NO_OPERATOR_AUTH(403,"无权限操作"), SYSTEM_ERROR(500,"出现错误"), USERNAME_EXIST(501,"用户名已存在"), PHONENUMBER_EXIST(502,"手机号已存在"), EMAIL_EXIST(503, "邮箱已存在"), REQUIRE_USERNAME(504, "必需填写用户名"), LOGIN_ERROR(505,"用户名或密码错误"); int code; String msg;
AppHttpCodeEnum(int code, String errorMessage){ this.code = code; this.msg = errorMessage; }
public int getCode() { return code; }
public String getMsg() { return msg; } }
|
domain/ResponseResult返回对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| package com.nuyoah.domain;
import com.fasterxml.jackson.annotation.JsonInclude; import com.nuyoah.enums.AppHttpCodeEnum; import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL) public class ResponseResult<T> implements Serializable { private Integer code; private String msg; private T data;
public ResponseResult() { this.code = AppHttpCodeEnum.SUCCESS.getCode(); this.msg = AppHttpCodeEnum.SUCCESS.getMsg(); }
public ResponseResult(Integer code, T data) { this.code = code; this.data = data; }
public ResponseResult(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; }
public ResponseResult(Integer code, String msg) { this.code = code; this.msg = msg; }
public static ResponseResult errorResult(int code, String msg) { ResponseResult result = new ResponseResult(); return result.error(code, msg); } public static ResponseResult okResult() { ResponseResult result = new ResponseResult(); return result; } public static ResponseResult okResult(int code, String msg) { ResponseResult result = new ResponseResult(); return result.ok(code, null, msg); }
public static ResponseResult okResult(Object data) { ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg()); if(data!=null) { result.setData(data); } return result; }
public static ResponseResult errorResult(AppHttpCodeEnum enums){ return setAppHttpCodeEnum(enums,enums.getMsg()); }
public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg){ return setAppHttpCodeEnum(enums,msg); }
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){ return okResult(enums.getCode(),enums.getMsg()); }
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){ return okResult(enums.getCode(),msg); }
public ResponseResult<?> error(Integer code, String msg) { this.code = code; this.msg = msg; return this; }
public ResponseResult<?> ok(Integer code, T data) { this.code = code; this.data = data; return this; }
public ResponseResult<?> ok(Integer code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; return this; }
public ResponseResult<?> ok(T data) { this.data = data; return this; }
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public T getData() { return data; }
public void setData(T data) { this.data = data; }
}
|
返回对象
service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.nuyoah.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fasterxml.jackson.databind.util.BeanUtil; import com.nuyoah.domain.ResponseResult; import com.nuyoah.domain.entity.Article; import com.nuyoah.domain.vo.HotArticleVo; import com.nuyoah.mapper.ArticleMapper; import com.nuyoah.service.ArticleService;
import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.List;
@Service public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService { @Override public ResponseResult hostArticleList() { LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(Article::getStatus, 0); lambdaQueryWrapper.orderByAsc(Article::getViewCount); Page<Article> page = new Page<>(1, 10); page(page, lambdaQueryWrapper);
List<Article> articles = page.getRecords(); List<HotArticleVo> hotArticleVos = new ArrayList<>(); for (Article article : articles) { HotArticleVo vo = new HotArticleVo(); BeanUtils.copyProperties(article, vo); hotArticleVos.add(vo); } return ResponseResult.okResult(hotArticleVos); }
}
|
常量类
当我们数据库中有数字表示状态信息的时候,在JAVA里面需要封装一个常量类,这样在修改状态字符的时候,只需要修改常量类中的数字即可
constants/SystemConstants
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.nuyoah.constants;
public class SystemConstants {
public static final int ARTICLE_STATUS_DRAFT = 1;
public static final int ARTICLE_STATUS_NORMAL = 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Service public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService { @Override public ResponseResult hostArticleList() { LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(Article::getStatus, SystemConstans.ARTICLE_STATUS_DRAFT); }
}
|