Skip to content

Component 模块 ​

guava-component-starter — 公共组件模块,提供基础模型、异常处理、安全加密、工具类等通用能力。

基本信息 ​

属性值
工程名guava-component-starter
类型组件库模块
作用封装通用基础组件,供所有服务引用

基础模型 ​

BasePO 持久化对象基类 ​

所有持久化实体继承此类,包含通用字段:

java
@TableName("sys_user")
public class SysUserPO extends BasePO {
    // 自动继承以下字段:
    // id, createTime, updateTime, createBy, updateBy, delFlag, version
}

通用字段:

字段类型说明
idLong主键 ID
createTimeLocalDateTime创建时间
updateTimeLocalDateTime更新时间
createByString创建人
updateByString更新人
delFlagInteger逻辑删除(0=正常,1=删除)
versionInteger乐观锁版本号

BaseDTO 数据传输对象基类 ​

服务间数据传输的基类。

BaseModel 模型基类 ​

所有模型的基类。

异常处理 ​

BizCode2 业务错误码 ​

java
// 常用错误码
BizCode2.METHOD_ARGS_NOTNULL    // 参数不能为空
BizCode2.VARIABLE_NOT_EMPTY     // 变量不能为空

BizException2 业务异常 ​

java
// 抛出业务异常
throw new BizException2(BizCode2.VARIABLE_NOT_EMPTY);

// 自定义消息
throw new BizException2(10001, "自定义错误消息");

AppException / AppRuntimeException ​

java
// 应用异常
throw new AppException("应用异常");

// 运行时异常
throw new AppRuntimeException("运行时异常");

工具类详解 ​

AssertUtil2 断言工具 ​

参数校验与业务异常抛出:

java
// 断言对象不为 null(返回原对象)
User user = AssertUtil2.notNull(user, BizCode2.METHOD_ARGS_NOTNULL);

// 断言对象为 null
AssertUtil2.isNull(result, BizCode2.VARIABLE_NOT_EMPTY);

// 断言字符串不为空
String name = AssertUtil2.notEmpty(name, BizCode2.VARIABLE_NOT_EMPTY);

// 带占位符参数
AssertUtil2.notNull(user, BizCode2.METHOD_ARGS_NOTNULL, "user");
// 错误消息:参数不能为空,参数名:user

// 直接抛出异常
AssertUtil2.errMsg(BizCode2.VARIABLE_NOT_EMPTY);
AssertUtil2.errMsg("自定义错误消息");

StrUtil2 字符串工具 ​

java
// 首字母转小写
String lower = StrUtil2.firChar2Lower("Hello");  // "hello"

// 首字母转大写
String upper = StrUtil2.firChar2Upper("hello");  // "Hello"

// 占位符替换
String msg = StrUtil2.placeHolder("用户[{0}]已存在", "admin");
// 输出:用户[admin]已存在

// 驼峰转下划线
String underline = StrUtil2.toLine("userName");  // "user_name"

// 下划线转驼峰
String camel = StrUtil2.toCamelCase("user_name");  // "userName"

// 继承 Hutool StrUtil 的所有方法
boolean empty = StrUtil2.isEmpty(str);
boolean blank = StrUtil2.isBlank(str);
String trimmed = StrUtil2.trim(str);

DateUtil2 日期工具 ​

java
// 时间戳转 LocalDateTime
LocalDateTime dateTime = DateUtil2.fromTimeStamp(1700000000L);

// LocalDateTime 转时间戳
Long timestamp = DateUtil2.getTimeStamp(LocalDateTime.now());

// LocalDateTime 转 Date
Date date = DateUtil2.toDate(LocalDateTime.now());

// Date 转 LocalDateTime
LocalDateTime dateTime = DateUtil2.toLocalDateTime(new Date());

// 格式化日期
String dateStr = LocalDateTime.now().format(DateUtil2.DFY_MD_HMS);  // "2024-01-01 12:00:00"
String dateStr2 = LocalDateTime.now().format(DateUtil2.DFY_MD);     // "2024-01-01"

// 继承 Hutool DateUtil 的所有方法
String now = DateUtil2.now();
Date yesterday = DateUtil2.yesterday();

Map2ObjUtil Map 与对象转换 ​

java
// Map 转对象
User user = Map2ObjUtil.copyProperties(map, User.class);

// 对象转 Map
Map<String, Object> map = Map2ObjUtil.toMap(user);

// 批量转换
List<User> users = Map2ObjUtil.copyPropertiesList(list, User.class);

QcBean 查询条件 Bean ​

动态查询条件的封装对象:

java
QcBean qc = new QcBean();
qc.setId("userId");
qc.setOp("=");        // 操作符:=, !=, like, in, >, < 等
qc.setValue("123");    // 值
qc.setCode("code");    // 编码
qc.setDf("format");    // 格式化
qc.setKind("text");    // 类型:text, number, date, dic, month, datetime
qc.setAction("show");  // 动作
qc.setDs("fieldName"); // 数据源/字段名

ThreadPoolMyUtil 线程池工具 ​

java
// 创建单线程线程池
ExecutorService executor = ThreadPoolMyUtil.newSingleThreadExecutor();

// 创建指定线程工厂的单线程线程池
ExecutorService executor = ThreadPoolMyUtil.newSingleThreadExecutor(new NamedThreadFactory("biz"));

// 创建单线程定时任务线程池
ScheduledExecutorService scheduler = ThreadPoolMyUtil.newSingleThreadScheduledExecutor();

// 默认并发能力
int capacity = ThreadPoolMyUtil.DEFAULT_CONCURRENTCY_CAPACITY;  // 100

NamedThreadFactory 命名线程工厂 ​

java
// 创建命名线程工厂
ThreadFactory factory = new NamedThreadFactory("biz-pool");

// 创建的线程名称格式:biz-pool-1、biz-pool-2...

HttpStatusMyUtil HTTP 状态码工具 ​

java
// 获取状态码描述
String desc = HttpStatusMyUtil.getDesc(200);  // "OK"

// 判断是否成功
boolean success = HttpStatusMyUtil.isSuccess(200);  // true

JSON 序列化 ​

java
// LocalDateTime 序列化(注册到 ObjectMapper)
// 使用 JsonLocalDateTimeSerializer

// LocalDateTime 反序列化
// 使用 JsonLocalDateTimeDeserializer

// LocalDate 序列化
// 使用 JsonLocalDateSerializer

// LocalDate 反序列化
// 使用 JsonLocalDateDeserializer

常量定义 ​

常量类说明
AppConstant应用常量
SysConstant系统常量
GlobalConstant全局常量
Constant通用常量
HeaderConstant请求头常量
RedisConstantRedis 常量(key 前缀、过期时间等)
DicConstant字典常量
FormatConstant格式化常量(日期格式、数字格式等)
MybatisConstantMyBatis 常量
ExcelCheckConstantExcel 校验常量
HttpStatusConstantHTTP 状态码常量
SpringBeanConstantSpring Bean 常量
ConstantPool常量池(集中管理所有常量)
SpringBeanConstantSpring Bean 名称常量

相关文档 ​