文章目录
- 前言
- 一、封装数据传输类型的基类
-
- [1.1 该放在哪里呢?](#1.1 该放在哪里呢?)
- [1.2 在 common-domain 下创建 domain 包](#1.2 在 common-domain 下创建 domain 包)
- [1.3 在 domain 的包下创建 dataobject、dto、vo 包](#1.3 在 domain 的包下创建 dataobject、dto、vo 包)
- [1.4 在 dataobject 包下创建 BaseDO 类](#1.4 在 dataobject 包下创建 BaseDO 类)
- [1.5 在 dto 包下创建 BasePageDTO 和 BasePageReqDTO 类](#1.5 在 dto 包下创建 BasePageDTO 和 BasePageReqDTO 类)
- [1.6 在 vo 包下创建 BasePageVO 类](#1.6 在 vo 包下创建 BasePageVO 类)
- 二、常量类的封装
-
- [2.1 在 common-domain 包下创建 constants 包](#2.1 在 common-domain 包下创建 constants 包)
- [2.2 创建 CacheConstants 缓存常量](#2.2 创建 CacheConstants 缓存常量)
- [2.3 创建 DictionaryConstants 字典类型常量类](#2.3 创建 DictionaryConstants 字典类型常量类)
- [2.4 创建 MessageConstants 短信服务相关常量](#2.4 创建 MessageConstants 短信服务相关常量)
- [2.5 创建 SecurityConstants 安全相关常量](#2.5 创建 SecurityConstants 安全相关常量)
- [2.6 创建 TokenConstants Token常量](#2.6 创建 TokenConstants Token常量)
- [2.7 创建 UserStatusConstants 人员状态常量类](#2.7 创建 UserStatusConstants 人员状态常量类)
- [2.8 创建 CommonConstants 通用常量](#2.8 创建 CommonConstants 通用常量)
- END
鸡汤:
● 你不必追赶太阳,做一株会呼吸的苔藓也很好------在石缝里蓄水,在阴影里织绿,安静地把荒芜走成风景。
● 世界总说"向上生长",但真正的力量常藏在向下扎根的沉默里:根系越深,风雨来时越能听见大地的心跳。
前言
前面我们实现了文件服务的实现,为了便利之后其他服务的开发,我将一些通用的数据传输类型的基类写在这里。
一、封装数据传输类型的基类
1.1 该放在哪里呢?
首先作为通用的数据类型的基类,我们首先想到应该放在 common 包下,但是 common 包下还有很多的模块,应该放在哪里?

显然我们应该放在通用协议包里,也就是 common-domain 里
1.2 在 common-domain 下创建 domain 包

1.3 在 domain 的包下创建 dataobject、dto、vo 包
这里的统一返回数据结构和统一状态码已经讲过了,就不多说了。
Java项目:Java脚手架项目的统一模块的封装(四)

1.4 在 dataobject 包下创建 BaseDO 类

我以后会让所有对应数据库表的实体类都基础这个类,这个类只有一个 id
BaseDO:
java
package com.my.commondomain.domain.dataobject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
* do基类
*/
@Data
public class BaseDO {
@TableId(value = "id",type = IdType.AUTO)
private Long id;
}
1.5 在 dto 包下创建 BasePageDTO 和 BasePageReqDTO 类
我们是一个通用的脚手架项目,自然要想到以后基于我们脚手架开发的服务可能会有返回分页列表的要求,比如商品的分页...

所以我们提供 BasePageDTO 和 BasePageReqDTO 用来继承,可以少写很多代码。
BasePageDTO:
java
package com.my.commondomain.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.List;
/**
* 分页相应基类
* @param <T>
*/
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class BasePageDTO<T> {
/**
* 查询结果总数
*/
private Integer totals;
/**
* 总页数
*/
private Integer totalPages;
/**
* 数据列表
*/
private List<T> list;
public BasePageDTO(List<T> list) {
this.list = list;
}
/**
* 计算总页数
*
* @param totals 总数量
* @param pageSize 页大小
* @return 页数
*/
public static int calculateTotalPages(long totals, int pageSize) {
if (pageSize <= 0) {
throw new IllegalArgumentException("Page size must be greater than 0.");
}
return (int) Math.ceil((double) totals / pageSize);
}
}
BasePageReqDTO:
java
package com.my.commondomain.domain.dto;
import lombok.Getter;
import lombok.Setter;
/**
* 分页查询基类DTO
*/
@Setter
@Getter
public class BasePageReqDTO {
/**
* 分页编码
*/
private Integer pageNo = 1;
/**
* 分页数量
*/
private Integer pageSize = 10;
/**
* 获取偏移
*
* @return 偏移信息
*/
public Integer getOffset() {
return (pageNo - 1) * pageSize;
}
}
1.6 在 vo 包下创建 BasePageVO 类
字段啥的和 BasePageDTO 类一样,但是因为分层规则,我们提供了 BasePageVO 类
BasePageVO:
java
package com.my.commondomain.domain.vo;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* 分页相应基类
* @param <T>
*/
@Setter
@Getter
public class BasePageVO<T> {
/**
* 查询结果总数
*/
private Integer totals;
/**
* 总页数
*/
private Integer totalPages;
/**
* 数据列表
*/
private List<T> list;
}
二、常量类的封装
本来想这里的常量,是写到每个服务时在一个一个加的,但是感觉可能会很乱,所以就 ''剧透'' 一下吧
2.1 在 common-domain 包下创建 constants 包

2.2 创建 CacheConstants 缓存常量
CacheConstants:
java
package com.my.commondomain.constants;
/**
* 缓存常量
*/
public class CacheConstants {
/**
* 缓存分割符
*/
public final static String CACHE_SPLIT_COLON = ":";
/**
* 缓存有效期,默认720(分钟)
*/
public final static long EXPIRATION = 720;
/**
* 缓存刷新时间,默认120(分钟)
*/
public final static long REFRESH_TIME = 120;
}
2.3 创建 DictionaryConstants 字典类型常量类
DictionaryConstants :
java
package com.my.commondomain.constants;
/**
* 字典类型常量类
*/
public class DictionaryConstants {
/**
* 代码中要用到的 字典类型
* 封装常量类
*
* 身份 type
*/
public static final String IDENTITY_TYPE_KEY = "admin";
/**
* 状态 type
*/
public static final String COMMON_STATUS = "common_status";
}
2.4 创建 MessageConstants 短信服务相关常量
之后会引入阿里云的短信服务,短信服务相关常量就是哪里的常量
MessageConstants :
java
package com.my.commondomain.constants;
/**
* 短信服务相关常量
*/
public class MessageConstants {
/**
* 阿里服务返回状态码
*/
public static final String SMS_MSG_OK = "OK";
/**
* 同一手机号每日限制 Key
*/
public static final String SMS_CODE_LIMIT_KEY = "sms:limit:";
/**
* 缓存验证码
*/
public static final String SMS_CODE_KEY = "sms:code:";
/**
* 默认验证码长度
*/
public static final int DEFAULT_SMS_LENGTH = 6;
/**
* 默认验证码
*/
public static final String DEFAULT_SMS_CODE = "123456";
}
2.5 创建 SecurityConstants 安全相关常量
之后的 Token服务,用于管理用户的生命周期
SecurityConstants :
java
package com.my.commondomain.constants;
/**
* 安全相关常量
*/
public class SecurityConstants {
/**
* 用户标识
*/
public static final String USER_KEY = "user_key";
/**
* 用户ID
*/
public static final String USER_ID = "user_id";
/**
* 用户来源
*/
public static final String USER_FROM = "user_from";
/**
* 用户名称
*/
public static final String USERNAME = "username";
/**
* 授权信息字段
*/
public static final String AUTHENTICATION = "Authorization";
}
2.6 创建 TokenConstants Token常量
TokenConstants :
java
package com.my.commondomain.constants;
/**
* Token常量
*/
public class TokenConstants {
/**
* 密钥:Base64编码的密钥
*/
public static final String TOKEN_SECRET = "fBTJreGwVVHW0TZY/pS8rFC94se0Yg9I+BddwlMNoqs=";
/**
* token 前缀
*/
public static final String TOKEN_PREFIX = "Bearer ";
/**
* 登录token 缓存的key
*/
public static final String REDIS_LOGIN_TOKEN_KEY = "logintoken:";
}
2.7 创建 UserStatusConstants 人员状态常量类
UserStatusConstants :
java
package com.my.commondomain.constants;
/**
* 人员状态常量类
*/
public class UserStatusConstants {
/**
* 停用状态
*/
public static final String DISABLE = "disable";
/**
* 启用状态
*/
public static final String ENABLE = "enable";
}
2.8 创建 CommonConstants 通用常量
提供模板,提供代码规范
CommonConstants :
java
package com.my.commondomain.constants;
/**
* 通用常量
*/
public class CommonConstants {
/**
* 通用日期格式
*/
public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 默认编码
*/
public final static String UTF8 = "UTF-8";
}
END
可以接下来的服务开发了