后端代码规范

编程

  1. 实体命名规范

***代表具体的业务名称

实体类:***Entity (与数据库映射持久类Persistent,类必须添加@Table注解并写好表名)

视图类:***VO(接口返回前端数据模型 Value Object)

传输类:***Dto(前端传过来的数据模型 Data Transfer Object)

DAO层接口类:Dao (通用的dao操作方法,接口有默认方法 defaultIdColumnName返回默认的id列名,id列名可以实现idColumnName方法修改)

DAO层基类:BaseDao (dao层公用的工具类)

DAO抽象类:AbstractDao (继承BaseDao,实现Dao接口)

DAO层接口实现类:***DaoImpl(继承AbstractDao)

service层接口类:***Service

service层接口实现类:***ServiceImpl

控制层:***Controller

  1. 数据库字段类型规范

主键( _id )字段:String

启用( enabled )字段:boolean

状态( status )字段:String

排序( showOrder )字段:int

优秀级( priority )字段:int

创建人( creator )字段:String

创建时间 ( createTime )字段:LocalDateTime

修改人 ( modifier )字段: String

修改时间 ( modifyTime )字段:LocalDateTime

  1. 接口命名规范

添加( create、add )

根据id修改 ( modifyById、updateById )

根据id查找( findById )

分页( findByPage )

根据id删除 ( deleteById )

  1. 统一返回类型( ResultVO<T> )

/**

* 错误代码

* success: 200

* error: 500

* exception: 700

*/

public int code;

/**

* 返回结果

* success: 成功

* error: 失败

* exception: 异常

*/

public String message;

/**

* 错误详细信息

*/

public String detail;

/**

* 范型

*/

public T result;

  1. 查询规范 (***Example)

NodeExample.java

public class NodeExample {

private Bson bson = null;

public Bson getBson() {

return bson;

}

public NodeExample andIdEq(String id) {

if (bson == null) {

bson = Filters.eq("_id", id);

} else {

bson = Filters.and(bson, Filters.eq("_id", id));

}

return this;

}

public NodeExample andIdNe(String id) {

if (bson == null) {

bson = Filters.ne("_id", id);

} else {

bson = Filters.and(bson, Filters.ne("_id", id));

}

return this;

}

public NodeExample orIdEq(String id) {

if (bson == null) {

bson = Filters.eq("_id", id);

} else {

bson = Filters.or(bson, Filters.eq("_id", id));

}

return this;

}

public NodeExample orIdNe(String id) {

if (bson == null) {

bson = Filters.ne("_id", id);

} else {

bson = Filters.or(bson, Filters.ne("_id", id));

}

return this;

}

public NodeExample andNameEq(String name) {

if (bson == null) {

bson = Filters.eq("name", name);

} else {

bson = Filters.and(bson, Filters.eq("name", name));

}

return this;

}

public NodeExample orNameEq(String name) {

if (bson == null) {

bson = Filters.eq("name", name);

} else {

bson = Filters.or(bson, Filters.eq("name", name));

}

return this;

}

public NodeExample andNameNe(String name) {

if (bson == null) {

bson = Filters.ne("name", name);

} else {

bson = Filters.and(bson, Filters.ne("name", name));

}

return this;

}

public NodeExample orNameNe(String name) {

if (bson == null) {

bson = Filters.ne("name", name);

} else {

bson = Filters.or(bson, Filters.ne("name", name));

}

return this;

}

public NodeExample andNameLike(String name) {

if (bson == null) {

bson = Filters.regex("name", name);

} else {

bson = Filters.and(bson, Filters.regex("name", name));

}

return this;

}

public NodeExample orNameLike(String name) {

if (bson == null) {

bson = Filters.regex("name", name);

} else {

bson = Filters.or(bson, Filters.regex("name", name));

}

return this;

}

}

以上是 后端代码规范 的全部内容, 来源链接: utcz.com/z/511788.html

回到顶部