feat: 初始化 xfg-ddd项目
- 添加基础目录结构和文件 - 配置 Spring Boot 应用 - 添加数据库配置和 MyBatis 映射文件 - 实现简单的用户服务接口和仓库接口 - 添加日志配置和示例日志 - 配置 Docker 和 Docker Compose 文件 - 添加 Jackson 配置和工具类 - 实现简单的 API 测试用例
This commit is contained in:
parent
eca07e5209
commit
96f20101ea
4
pom.xml
4
pom.xml
@ -117,7 +117,7 @@
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Swagger相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
@ -139,7 +139,7 @@
|
||||
<artifactId>swagger-bootstrap-ui</artifactId>
|
||||
<version>${swagger-bootstrap-ui.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 参数校验相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
|
@ -20,21 +20,11 @@
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
<!-- Jackson依赖 -->
|
||||
<!-- 添加对types模块的依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.13.4.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.13.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.13.4</version>
|
||||
<groupId>com.in</groupId>
|
||||
<artifactId>xfg-ddd-types</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -30,6 +30,25 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- Jackson依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<!-- Hutool工具类 -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>5.8.20</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,124 @@
|
||||
package com.in.types.common.utils;
|
||||
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Hutool工具类使用示例
|
||||
* 演示如何使用Hutool替代自定义的StringUtil和DateUtil
|
||||
*/
|
||||
public class HutoolDemo {
|
||||
|
||||
/**
|
||||
* 字符串工具类示例(替代StringUtil)
|
||||
*/
|
||||
public static void stringUtilDemo() {
|
||||
// 判断字符串是否为空
|
||||
boolean isEmpty = StrUtil.isEmpty(""); // true
|
||||
boolean isNotEmpty = StrUtil.isNotEmpty("test"); // true
|
||||
boolean isBlank = StrUtil.isBlank(" "); // true
|
||||
boolean isNotBlank = StrUtil.isNotBlank("test"); // true
|
||||
|
||||
// 生成UUID
|
||||
String uuid = UUID.fastUUID().toString(true); // 不带横线的UUID
|
||||
String simpleUUID = IdUtil.simpleUUID(); // 不带横线的UUID
|
||||
|
||||
// 截取字符串
|
||||
String sub = StrUtil.sub("abcdefg", 2, 5); // "cde"
|
||||
|
||||
// 首字母大小写转换
|
||||
String capitalize = StrUtil.upperFirst("hello"); // "Hello"
|
||||
String uncapitalize = StrUtil.lowerFirst("World"); // "world"
|
||||
|
||||
// 驼峰和下划线命名转换
|
||||
String toCamelCase = StrUtil.toCamelCase("user_name"); // "userName"
|
||||
String toUnderlineCase = StrUtil.toUnderlineCase("userName"); // "user_name"
|
||||
|
||||
// 正则匹配
|
||||
boolean matches = ReUtil.isMatch("\\d+", "12345"); // true
|
||||
|
||||
// 字符串替换
|
||||
String replaced = StrUtil.replace("hello world", "world", "hutool"); // "hello hutool"
|
||||
|
||||
System.out.println("===== String Util Demo =====");
|
||||
System.out.println("isEmpty: " + isEmpty);
|
||||
System.out.println("isNotEmpty: " + isNotEmpty);
|
||||
System.out.println("isBlank: " + isBlank);
|
||||
System.out.println("isNotBlank: " + isNotBlank);
|
||||
System.out.println("UUID: " + uuid);
|
||||
System.out.println("Simple UUID: " + simpleUUID);
|
||||
System.out.println("Substring: " + sub);
|
||||
System.out.println("Capitalize: " + capitalize);
|
||||
System.out.println("Uncapitalize: " + uncapitalize);
|
||||
System.out.println("ToCamelCase: " + toCamelCase);
|
||||
System.out.println("ToUnderlineCase: " + toUnderlineCase);
|
||||
System.out.println("Matches: " + matches);
|
||||
System.out.println("Replace: " + replaced);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期工具类示例(替代DateUtil)
|
||||
*/
|
||||
public static void dateUtilDemo() {
|
||||
// 获取当前日期时间
|
||||
DateTime now = DateUtil.date();
|
||||
|
||||
// 格式化日期
|
||||
String formatDate = DateUtil.formatDate(now); // yyyy-MM-dd
|
||||
String formatDateTime = DateUtil.formatDateTime(now); // yyyy-MM-dd HH:mm:ss
|
||||
String formatTime = DateUtil.formatTime(now); // HH:mm:ss
|
||||
String customFormat = DateUtil.format(now, "yyyy/MM/dd"); // 自定义格式
|
||||
|
||||
// 字符串转日期
|
||||
Date date1 = DateUtil.parse("2023-05-20");
|
||||
Date date2 = DateUtil.parse("2023-05-20 12:34:56", "yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
// 日期计算
|
||||
DateTime tomorrow = DateUtil.tomorrow();
|
||||
DateTime yesterday = DateUtil.yesterday();
|
||||
DateTime nextWeek = DateUtil.offsetWeek(now, 1);
|
||||
DateTime nextMonth = DateUtil.offset(now, DateField.MONTH, 1);
|
||||
|
||||
// 获取日期部分
|
||||
int year = DateUtil.year(now);
|
||||
int month = DateUtil.month(now) + 1; // 返回值从0开始
|
||||
int day = DateUtil.dayOfMonth(now);
|
||||
|
||||
// 日期比较
|
||||
boolean isSameDay = DateUtil.isSameDay(now, tomorrow); // false
|
||||
long betweenDays = DateUtil.betweenDay(now, nextMonth, true); // 日期相差天数
|
||||
|
||||
System.out.println("\n===== Date Util Demo =====");
|
||||
System.out.println("Now: " + now);
|
||||
System.out.println("Format Date: " + formatDate);
|
||||
System.out.println("Format DateTime: " + formatDateTime);
|
||||
System.out.println("Format Time: " + formatTime);
|
||||
System.out.println("Custom Format: " + customFormat);
|
||||
System.out.println("Parse Date1: " + date1);
|
||||
System.out.println("Parse Date2: " + date2);
|
||||
System.out.println("Tomorrow: " + tomorrow);
|
||||
System.out.println("Yesterday: " + yesterday);
|
||||
System.out.println("Next Week: " + nextWeek);
|
||||
System.out.println("Next Month: " + nextMonth);
|
||||
System.out.println("Year: " + year);
|
||||
System.out.println("Month: " + month);
|
||||
System.out.println("Day: " + day);
|
||||
System.out.println("Is Same Day: " + isSameDay);
|
||||
System.out.println("Between Days: " + betweenDays);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主方法,运行所有示例
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
stringUtilDemo();
|
||||
dateUtilDemo();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.in.api.util;
|
||||
package com.in.types.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
156
xfg-ddd-types/src/main/java/com/in/types/common/utils/README.md
Normal file
156
xfg-ddd-types/src/main/java/com/in/types/common/utils/README.md
Normal file
@ -0,0 +1,156 @@
|
||||
# DDD框架工具类
|
||||
|
||||
本目录包含了DDD框架中使用的通用工具类,这些工具类被放置在共享内核(xfg-ddd-types)模块中,可以被所有其他模块使用。
|
||||
|
||||
## 工具类列表
|
||||
|
||||
### 1. JacksonUtil
|
||||
|
||||
JSON处理工具类,用于对象与JSON字符串之间的转换。
|
||||
|
||||
主要功能:
|
||||
- 对象转JSON字符串
|
||||
- JSON字符串转对象
|
||||
- JSON字符串转列表
|
||||
- JSON字符串转Map
|
||||
- 获取ObjectMapper实例
|
||||
|
||||
示例用法:
|
||||
```java
|
||||
// 对象转JSON
|
||||
User user = new User(1L, "张三", 25);
|
||||
String json = JacksonUtil.bean2Json(user);
|
||||
|
||||
// JSON转对象
|
||||
User parsedUser = JacksonUtil.json2Bean(json, User.class);
|
||||
|
||||
// JSON转列表
|
||||
List<User> userList = JacksonUtil.json2List(jsonArray, User.class);
|
||||
```
|
||||
|
||||
### 2. Hutool工具类
|
||||
|
||||
项目引入了Hutool核心工具类,用于替代自定义的StringUtil和DateUtil工具类。Hutool是一个小而全的Java工具类库,提供了丰富的功能。
|
||||
|
||||
#### 2.1 字符串处理 (StrUtil)
|
||||
|
||||
主要功能:
|
||||
- 判断字符串是否为空/空白
|
||||
- 生成UUID
|
||||
- 截取字符串
|
||||
- 首字母大小写转换
|
||||
- 驼峰命名与下划线命名转换
|
||||
|
||||
示例用法:
|
||||
```java
|
||||
// 判断字符串是否为空
|
||||
boolean isEmpty = StrUtil.isEmpty(str);
|
||||
boolean isBlank = StrUtil.isBlank(str);
|
||||
|
||||
// 生成UUID
|
||||
String uuid = UUID.fastUUID().toString(true);
|
||||
String simpleUUID = IdUtil.simpleUUID();
|
||||
|
||||
// 驼峰命名转下划线
|
||||
String underlineCase = StrUtil.toUnderlineCase("userName"); // user_name
|
||||
String camelCase = StrUtil.toCamelCase("user_name"); // userName
|
||||
```
|
||||
|
||||
#### 2.2 日期处理 (DateUtil)
|
||||
|
||||
主要功能:
|
||||
- 获取当前日期/时间
|
||||
- 日期格式化与解析
|
||||
- 日期计算(增加/减少年月日时分秒)
|
||||
- 获取日期的年月日时分秒
|
||||
- 判断日期是否相同
|
||||
|
||||
示例用法:
|
||||
```java
|
||||
// 获取当前日期时间
|
||||
DateTime now = DateUtil.date();
|
||||
|
||||
// 格式化日期
|
||||
String formatDate = DateUtil.formatDate(now); // yyyy-MM-dd
|
||||
String formatTime = DateUtil.formatTime(now); // HH:mm:ss
|
||||
String formatDateTime = DateUtil.formatDateTime(now); // yyyy-MM-dd HH:mm:ss
|
||||
String customFormat = DateUtil.format(now, "yyyy/MM/dd"); // 自定义格式化
|
||||
|
||||
// 解析日期
|
||||
Date date = DateUtil.parse("2023-05-20");
|
||||
Date dateTime = DateUtil.parse("2023-05-20 12:34:56", "yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
// 日期计算
|
||||
DateTime tomorrow = DateUtil.tomorrow();
|
||||
DateTime nextWeek = DateUtil.offsetWeek(now, 1);
|
||||
DateTime nextMonth = DateUtil.offset(now, DateField.MONTH, 1);
|
||||
|
||||
// 日期比较
|
||||
boolean isSameDay = DateUtil.isSameDay(now, tomorrow);
|
||||
long betweenDays = DateUtil.betweenDay(now, nextMonth, true);
|
||||
```
|
||||
|
||||
#### 2.3 正则表达式 (ReUtil)
|
||||
|
||||
主要功能:
|
||||
- 正则表达式匹配
|
||||
- 正则表达式提取
|
||||
- 正则表达式替换
|
||||
|
||||
示例用法:
|
||||
```java
|
||||
// 正则表达式匹配
|
||||
boolean isMatch = ReUtil.isMatch("\\d+", "12345");
|
||||
|
||||
// 正则表达式提取
|
||||
String result = ReUtil.get("\\d+", "abc123def", 0); // 123
|
||||
|
||||
// 正则表达式替换
|
||||
String replaced = ReUtil.replaceAll("hello world", "world", "hutool"); // hello hutool
|
||||
```
|
||||
|
||||
## 使用说明
|
||||
|
||||
1. 在需要使用工具类的模块中,添加对xfg-ddd-types模块的依赖:
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.in</groupId>
|
||||
<artifactId>xfg-ddd-types</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
2. 在代码中导入需要的工具类:
|
||||
```java
|
||||
// 使用JacksonUtil
|
||||
import com.in.types.common.utils.JacksonUtil;
|
||||
|
||||
// 使用Hutool工具类
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
```
|
||||
|
||||
3. 查看具体用法示例:
|
||||
```java
|
||||
import com.in.types.common.utils.HutoolDemo;
|
||||
|
||||
// 运行示例代码
|
||||
HutoolDemo.stringUtilDemo();
|
||||
HutoolDemo.dateUtilDemo();
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 工具类都是无状态的,所有方法都是静态方法,可以直接调用。
|
||||
2. 如需了解更多Hutool工具类的功能,请参考[Hutool官方文档](https://hutool.cn/docs)。
|
||||
3. 如果需要添加新的工具类,请遵循以下原则:
|
||||
- 优先考虑使用Hutool提供的工具类
|
||||
- 自定义工具类应该是无状态的,所有方法都应该是静态方法
|
||||
- 工具类应该有详细的JavaDoc注释
|
||||
- 工具类应该放在com.in.types.common.utils包下
|
||||
- 工具类名称应该以Util结尾
|
Loading…
x
Reference in New Issue
Block a user