学长带的前后端分离项目,结构如下
下面是学长提出的开发规范和一些补充,仅供参考。
1. bo
bo分为AddBo、EditBo、QueryBo,代表从前端接受的插入、修改、查询的参数,如果业务没有要求可以不用写,所有参数类型用包装类。
2. cache
存放与缓存有关组件,统一管理、引用
3. common
存放共用方法、变量
3.1 子文件夹 constant
存放常量类、枚举类
3.2 子文件夹 util
存放工具类
3.3 非工具公用类
存放在common目录下
常用的有AjaxResult、
TableDataInfo、自定义异常类、异常处理类
AjaxResult
请求响应返回对象
import java.util.HashMap;
/**
* Ajax请求响应独享
* @author jjjzzzqqq
*/
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
super.put(DATA_TAG, data);
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
/**
* 方便链式调用
*
* @param key 键
* @param value 值
* @return 数据对象
*/
@Override
public AjaxResult put(String key, Object value)
{
super.put(key, value);
return this;
}
public static AjaxResult toAjax(int rows)
{
return rows > 0 ? AjaxResult.success() : AjaxResult.error();
}
}
TableDataInfo
返回分页对象
import java.io.Serializable;
import java.util.List;
/**
* 表格分页数据对象
* @author jjjzzzqqq
*/
public class TableDataInfo implements Serializable
{
private static final long serialVersionUID = 1L;
/** 总记录数 */
private long total;
/** 列表数据 */
private List<?> rows;
/** 消息状态码 */
private int code;
/** 消息内容 */
private String msg;
/**
* 表格数据对象
*/
public TableDataInfo()
{
}
/**
* 分页
*
* @param list 列表数据
* @param total 总记录数
*/
public TableDataInfo(List<?> list, long total)
{
this.rows = list;
this.total = total;
}
public long getTotal()
{
return total;
}
public void setTotal(long total)
{
this.total = total;
}
public List<?> getRows()
{
return rows;
}
public void setRows(List<?> rows)
{
this.rows = rows;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
}
4. config
存放各种配置组件,如redis序列化组件
5. controller
restful风格的url,增删改查返回统一用AjaxResult类包装,list用TableDataInfo类包装,bo都需要增加检查注解,有用不到的返回AjaxResult.error()
6. entity
每个类对应一个数据库表
7. mapper
使用Mybatis-Plus这个一般不用写,如果需要手写sql则需要在这里增加方法。
sql尽量用最简单的,由程序处理数据,避免数据量大时慢sql拖垮系统。
8. service
8.1 接口
8.2 子文件夹 impl
存放实现类
9. vo
封装在AjaxResult中,返回给前端的数据类。
前台可以展示的部分,一些比较隐蔽的数据则不予展示,时间参数加json注解