chore(build): 更新项目依赖版本并优化代码格式
- 升级 Spring Boot 父项目版本至 3.5.9 - 更新 MySQL 连接器版本至 9.4.0 - 更新 MyBatis-Plus 版本至 3.5.14 - 更新 Fastjson2 版本至 2.0.59 - 更新 Hutool 版本至 5.8.41 - 更新 JJWT 版本至 0.12.6 - 更新 Knife4j 版本至 4.5.0 - 更新 Undertow 版本至 2.3.20.Final - 更新 Oshi 版本至 6.6.5 - 更新 Guava 版本至 33.5.0-jre - 格式化注解文件中的代码缩进 - 移除多余的空行和注释格式调整
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>cutejava</artifactId>
|
<artifactId>cutejava</artifactId>
|
||||||
<groupId>cn.odboy</groupId>
|
<groupId>cn.odboy</groupId>
|
||||||
<version>1.4.1</version>
|
<version>1.4.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>cutejava-framework</artifactId>
|
<artifactId>cutejava-framework</artifactId>
|
||||||
<name>公共模块</name>
|
<name>公共模块</name>
|
||||||
</project>
|
</project>
|
||||||
+28
-28
@@ -41,38 +41,38 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@RequestMapping(method = RequestMethod.DELETE)
|
@RequestMapping(method = RequestMethod.DELETE)
|
||||||
public @interface AnonymousDeleteMapping {
|
public @interface AnonymousDeleteMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#name}.
|
* Alias for {@link RequestMapping#name}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#path}.
|
* Alias for {@link RequestMapping#path}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#params}.
|
* Alias for {@link RequestMapping#params}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#headers}.
|
* Alias for {@link RequestMapping#headers}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#consumes}.
|
* Alias for {@link RequestMapping#consumes}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#produces}.
|
* Alias for {@link RequestMapping#produces}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-30
@@ -39,40 +39,40 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@RequestMapping(method = RequestMethod.GET)
|
@RequestMapping(method = RequestMethod.GET)
|
||||||
public @interface AnonymousGetMapping {
|
public @interface AnonymousGetMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#name}.
|
* Alias for {@link RequestMapping#name}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#path}.
|
* Alias for {@link RequestMapping#path}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#params}.
|
* Alias for {@link RequestMapping#params}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#headers}.
|
* Alias for {@link RequestMapping#headers}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#consumes}.
|
* Alias for {@link RequestMapping#consumes}.
|
||||||
*
|
*
|
||||||
* @since 4.3.5
|
* @since 4.3.5
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#produces}.
|
* Alias for {@link RequestMapping#produces}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-28
@@ -41,38 +41,38 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@RequestMapping(method = RequestMethod.PATCH)
|
@RequestMapping(method = RequestMethod.PATCH)
|
||||||
public @interface AnonymousPatchMapping {
|
public @interface AnonymousPatchMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#name}.
|
* Alias for {@link RequestMapping#name}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#path}.
|
* Alias for {@link RequestMapping#path}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#params}.
|
* Alias for {@link RequestMapping#params}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#headers}.
|
* Alias for {@link RequestMapping#headers}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#consumes}.
|
* Alias for {@link RequestMapping#consumes}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#produces}.
|
* Alias for {@link RequestMapping#produces}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-28
@@ -41,38 +41,38 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@RequestMapping(method = RequestMethod.POST)
|
@RequestMapping(method = RequestMethod.POST)
|
||||||
public @interface AnonymousPostMapping {
|
public @interface AnonymousPostMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#name}.
|
* Alias for {@link RequestMapping#name}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#path}.
|
* Alias for {@link RequestMapping#path}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#params}.
|
* Alias for {@link RequestMapping#params}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#headers}.
|
* Alias for {@link RequestMapping#headers}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#consumes}.
|
* Alias for {@link RequestMapping#consumes}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#produces}.
|
* Alias for {@link RequestMapping#produces}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-28
@@ -41,38 +41,38 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@RequestMapping(method = RequestMethod.PUT)
|
@RequestMapping(method = RequestMethod.PUT)
|
||||||
public @interface AnonymousPutMapping {
|
public @interface AnonymousPutMapping {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#name}.
|
* Alias for {@link RequestMapping#name}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
@AliasFor(annotation = RequestMapping.class) String name() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#path}.
|
* Alias for {@link RequestMapping#path}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] path() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#params}.
|
* Alias for {@link RequestMapping#params}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] params() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#headers}.
|
* Alias for {@link RequestMapping#headers}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] headers() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#consumes}.
|
* Alias for {@link RequestMapping#consumes}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] consumes() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for {@link RequestMapping#produces}.
|
* Alias for {@link RequestMapping#produces}.
|
||||||
*/
|
*/
|
||||||
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
@AliasFor(annotation = RequestMapping.class) String[] produces() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,20 @@ import lombok.Setter;
|
|||||||
* @date 2025-07-23
|
* @date 2025-07-23
|
||||||
*/
|
*/
|
||||||
public class KitArgs {
|
public class KitArgs {
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public static class FindByLongId extends KitObject {
|
|
||||||
@NotNull(message = "id必填") private Long id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public static class DeleteById extends KitObject {
|
public static class FindByLongId extends KitObject {
|
||||||
@NotNull(message = "id必填") private Long id;
|
|
||||||
}
|
@NotNull(message = "id必填")
|
||||||
|
private Long id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class DeleteById extends KitObject {
|
||||||
|
|
||||||
|
@NotNull(message = "id必填")
|
||||||
|
private Long id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -28,6 +28,9 @@ import lombok.Setter;
|
|||||||
@Setter
|
@Setter
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitBaseCreateTimeTb extends KitObject {
|
public class KitBaseCreateTimeTb extends KitObject {
|
||||||
@Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true) @TableField(fill = FieldFill.INSERT) private Date
|
|
||||||
createTime;
|
@Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
@TableField(fill = FieldFill.INSERT)
|
||||||
|
private Date
|
||||||
|
createTime;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-4
@@ -28,9 +28,13 @@ import lombok.Setter;
|
|||||||
@Setter
|
@Setter
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitBaseTimeTb extends KitObject {
|
public class KitBaseTimeTb extends KitObject {
|
||||||
@TableField(fill = FieldFill.INSERT) @Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true) private Date
|
|
||||||
createTime;
|
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT_UPDATE) @Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
@TableField(fill = FieldFill.INSERT)
|
||||||
private Date updateTime;
|
@Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
private Date
|
||||||
|
createTime;
|
||||||
|
|
||||||
|
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||||
|
@Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
private Date updateTime;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -28,6 +28,8 @@ import lombok.Setter;
|
|||||||
@Setter
|
@Setter
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitBaseUpdateTimeTb extends KitObject {
|
public class KitBaseUpdateTimeTb extends KitObject {
|
||||||
@Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true) @TableField(fill = FieldFill.INSERT_UPDATE)
|
|
||||||
private Date updateTime;
|
@Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||||
|
private Date updateTime;
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-20
@@ -32,29 +32,38 @@ import org.springframework.data.annotation.CreatedBy;
|
|||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitBaseUserCreateTimeTb extends KitObject {
|
public class KitBaseUserCreateTimeTb extends KitObject {
|
||||||
|
|
||||||
@CreatedBy @TableField(fill = FieldFill.INSERT) @Schema(name = "创建人", hidden = true) private String createBy;
|
@CreatedBy
|
||||||
|
@TableField(fill = FieldFill.INSERT)
|
||||||
|
@Schema(name = "创建人", hidden = true)
|
||||||
|
private String createBy;
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT) @Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true) private Date
|
@TableField(fill = FieldFill.INSERT)
|
||||||
createTime;
|
@Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
private Date
|
||||||
|
createTime;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
ToStringBuilder builder = new ToStringBuilder(this);
|
ToStringBuilder builder = new ToStringBuilder(this);
|
||||||
Field[] fields = this.getClass().getDeclaredFields();
|
Field[] fields = this.getClass().getDeclaredFields();
|
||||||
try {
|
try {
|
||||||
for (Field f : fields) {
|
for (Field f : fields) {
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
builder.append(f.getName(), f.get(this)).append("\n");
|
builder.append(f.getName(), f.get(this)).append("\n");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
builder.append("toString builder encounter an error");
|
builder.append("toString builder encounter an error");
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/* 分组校验 */
|
/* 分组校验 */
|
||||||
public @interface Create {}
|
public @interface Create {
|
||||||
|
|
||||||
/* 分组校验 */
|
}
|
||||||
public @interface Update {}
|
|
||||||
|
/* 分组校验 */
|
||||||
|
public @interface Update {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-9
@@ -35,13 +35,24 @@ import org.springframework.data.annotation.LastModifiedBy;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class KitBaseUserTimeLogicTb extends KitObject {
|
public class KitBaseUserTimeLogicTb extends KitObject {
|
||||||
@CreatedBy @TableField(value = "create_by", fill = FieldFill.INSERT) @Schema(name = "创建人", hidden = true)
|
|
||||||
private String createBy;
|
@CreatedBy
|
||||||
@LastModifiedBy @TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
|
@TableField(value = "create_by", fill = FieldFill.INSERT)
|
||||||
@Schema(name = "更新人", hidden = true) private String updateBy;
|
@Schema(name = "创建人", hidden = true)
|
||||||
@TableField(value = "create_time", fill = FieldFill.INSERT) @Schema(name = "创建时间", hidden = true) private Date
|
private String createBy;
|
||||||
createTime;
|
@LastModifiedBy
|
||||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) @Schema(name = "更新时间", hidden = true)
|
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
|
||||||
private Date updateTime;
|
@Schema(name = "更新人", hidden = true)
|
||||||
@Schema(name = "数据有效性", hidden = true) @TableLogic @TableField("available") private Integer available;
|
private String updateBy;
|
||||||
|
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||||
|
@Schema(name = "创建时间", hidden = true)
|
||||||
|
private Date
|
||||||
|
createTime;
|
||||||
|
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||||
|
@Schema(name = "更新时间", hidden = true)
|
||||||
|
private Date updateTime;
|
||||||
|
@Schema(name = "数据有效性", hidden = true)
|
||||||
|
@TableLogic
|
||||||
|
@TableField("available")
|
||||||
|
private Integer available;
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-24
@@ -33,35 +33,48 @@ import org.springframework.data.annotation.LastModifiedBy;
|
|||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitBaseUserTimeTb extends KitObject {
|
public class KitBaseUserTimeTb extends KitObject {
|
||||||
|
|
||||||
@CreatedBy @TableField(fill = FieldFill.INSERT) @Schema(name = "创建人", hidden = true) private String createBy;
|
@CreatedBy
|
||||||
|
@TableField(fill = FieldFill.INSERT)
|
||||||
|
@Schema(name = "创建人", hidden = true)
|
||||||
|
private String createBy;
|
||||||
|
|
||||||
@LastModifiedBy @TableField(fill = FieldFill.INSERT_UPDATE) @Schema(name = "更新人", hidden = true) private String
|
@LastModifiedBy
|
||||||
updateBy;
|
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||||
|
@Schema(name = "更新人", hidden = true)
|
||||||
|
private String
|
||||||
|
updateBy;
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT) @Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true) private Date
|
@TableField(fill = FieldFill.INSERT)
|
||||||
createTime;
|
@Schema(name = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
private Date
|
||||||
|
createTime;
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT_UPDATE) @Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||||
private Date updateTime;
|
@Schema(name = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
|
||||||
|
private Date updateTime;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
ToStringBuilder builder = new ToStringBuilder(this);
|
ToStringBuilder builder = new ToStringBuilder(this);
|
||||||
Field[] fields = this.getClass().getDeclaredFields();
|
Field[] fields = this.getClass().getDeclaredFields();
|
||||||
try {
|
try {
|
||||||
for (Field f : fields) {
|
for (Field f : fields) {
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
builder.append(f.getName(), f.get(this)).append("\n");
|
builder.append(f.getName(), f.get(this)).append("\n");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
builder.append("toString builder encounter an error");
|
builder.append("toString builder encounter an error");
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/* 分组校验 */
|
/* 分组校验 */
|
||||||
public @interface Create {}
|
public @interface Create {
|
||||||
|
|
||||||
/* 分组校验 */
|
}
|
||||||
public @interface Update {}
|
|
||||||
|
/* 分组校验 */
|
||||||
|
public @interface Update {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,5 +26,7 @@ import java.io.Serializable;
|
|||||||
* @date 2022-01-12
|
* @date 2022-01-12
|
||||||
*/
|
*/
|
||||||
public class KitObject implements Serializable {
|
public class KitObject implements Serializable {
|
||||||
@Serial private static final long serialVersionUID = 1L;
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-5
@@ -34,9 +34,14 @@ import lombok.NoArgsConstructor;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class KitPageArgs<T> implements Serializable {
|
public class KitPageArgs<T> implements Serializable {
|
||||||
@NotNull(message = "参数page不能为空") @Min(value = 1, message = "参数page最小值为1")
|
|
||||||
@Schema(name = "页码", example = "1") private Integer page;
|
@NotNull(message = "参数page不能为空")
|
||||||
@NotNull(message = "参数size不能为空") @Min(value = 1, message = "参数size最小值为1")
|
@Min(value = 1, message = "参数page最小值为1")
|
||||||
@Schema(name = "每页数据量", example = "10") private Integer size;
|
@Schema(name = "页码", example = "1")
|
||||||
private T args;
|
private Integer page;
|
||||||
|
@NotNull(message = "参数size不能为空")
|
||||||
|
@Min(value = 1, message = "参数size最小值为1")
|
||||||
|
@Schema(name = "每页数据量", example = "10")
|
||||||
|
private Integer size;
|
||||||
|
private T args;
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-14
@@ -32,20 +32,21 @@ import lombok.NoArgsConstructor;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitPageResult<T> extends KitObject {
|
public class KitPageResult<T> extends KitObject {
|
||||||
private List<T> content;
|
|
||||||
private long totalElements;
|
|
||||||
|
|
||||||
public static <T> KitPageResult<T> emptyListData() {
|
private List<T> content;
|
||||||
KitPageResult<T> result = new KitPageResult<>();
|
private long totalElements;
|
||||||
result.setTotalElements(0);
|
|
||||||
result.setContent(new ArrayList<>());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> KitPageResult<T> listData(IPage<T> page) {
|
public static <T> KitPageResult<T> emptyListData() {
|
||||||
KitPageResult<T> result = new KitPageResult<>();
|
KitPageResult<T> result = new KitPageResult<>();
|
||||||
result.setTotalElements(page.getTotal());
|
result.setTotalElements(0);
|
||||||
result.setContent(page.getRecords());
|
result.setContent(new ArrayList<>());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> KitPageResult<T> listData(IPage<T> page) {
|
||||||
|
KitPageResult<T> result = new KitPageResult<>();
|
||||||
|
result.setTotalElements(page.getTotal());
|
||||||
|
result.setContent(page.getRecords());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -35,7 +35,8 @@ import lombok.NoArgsConstructor;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class KitSelectOptionVo extends KitObject {
|
public class KitSelectOptionVo extends KitObject {
|
||||||
private String label;
|
|
||||||
private String value;
|
private String label;
|
||||||
private Map<String, Object> ext;
|
private String value;
|
||||||
|
private Map<String, Object> ext;
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-20
@@ -20,24 +20,24 @@ package cn.odboy.constant;
|
|||||||
* 验证码配置枚举
|
* 验证码配置枚举
|
||||||
*/
|
*/
|
||||||
public enum CaptchaCodeEnum {
|
public enum CaptchaCodeEnum {
|
||||||
/**
|
/**
|
||||||
* 算数
|
* 算数
|
||||||
*/
|
*/
|
||||||
ARITHMETIC,
|
ARITHMETIC,
|
||||||
/**
|
/**
|
||||||
* 中文
|
* 中文
|
||||||
*/
|
*/
|
||||||
CHINESE,
|
CHINESE,
|
||||||
/**
|
/**
|
||||||
* 中文闪图
|
* 中文闪图
|
||||||
*/
|
*/
|
||||||
CHINESE_GIF,
|
CHINESE_GIF,
|
||||||
/**
|
/**
|
||||||
* 闪图
|
* 闪图
|
||||||
*/
|
*/
|
||||||
GIF,
|
GIF,
|
||||||
/**
|
/**
|
||||||
* 静态
|
* 静态
|
||||||
*/
|
*/
|
||||||
SPEC
|
SPEC
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -22,9 +22,9 @@ import lombok.Getter;
|
|||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum FileTypeEnum {
|
public enum FileTypeEnum {
|
||||||
/* 文件类型 */
|
/* 文件类型 */
|
||||||
IMAGE("image", "图片"), DOC("doc", "文档"), VOICE("voice", "音频"), VIDEO("video", "视频"), OTHER("other", "其他");
|
IMAGE("image", "图片"), DOC("doc", "文档"), VOICE("voice", "音频"), VIDEO("video", "视频"), OTHER("other", "其他");
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private final String description;
|
private final String description;
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-35
@@ -23,47 +23,47 @@ import lombok.Getter;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum RequestMethodEnum {
|
public enum RequestMethodEnum {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜寻 @AnonymousGetMapping
|
* 搜寻 @AnonymousGetMapping
|
||||||
*/
|
*/
|
||||||
GET("GET"),
|
GET("GET"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜寻 @AnonymousPostMapping
|
* 搜寻 @AnonymousPostMapping
|
||||||
*/
|
*/
|
||||||
POST("POST"),
|
POST("POST"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜寻 @AnonymousPutMapping
|
* 搜寻 @AnonymousPutMapping
|
||||||
*/
|
*/
|
||||||
PUT("PUT"),
|
PUT("PUT"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜寻 @AnonymousPatchMapping
|
* 搜寻 @AnonymousPatchMapping
|
||||||
*/
|
*/
|
||||||
PATCH("PATCH"),
|
PATCH("PATCH"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜寻 @AnonymousDeleteMapping
|
* 搜寻 @AnonymousDeleteMapping
|
||||||
*/
|
*/
|
||||||
DELETE("DELETE"),
|
DELETE("DELETE"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 否则就是所有 Request 接口都放行
|
* 否则就是所有 Request 接口都放行
|
||||||
*/
|
*/
|
||||||
ALL("All");
|
ALL("All");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request 类型
|
* Request 类型
|
||||||
*/
|
*/
|
||||||
private final String type;
|
private final String type;
|
||||||
|
|
||||||
public static RequestMethodEnum find(String type) {
|
public static RequestMethodEnum find(String type) {
|
||||||
for (RequestMethodEnum value : RequestMethodEnum.values()) {
|
for (RequestMethodEnum value : RequestMethodEnum.values()) {
|
||||||
if (value.getType().equals(type)) {
|
if (value.getType().equals(type)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return ALL;
|
|
||||||
}
|
}
|
||||||
|
return ALL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-26
@@ -20,34 +20,35 @@ package cn.odboy.constant;
|
|||||||
* 常用静态常量
|
* 常用静态常量
|
||||||
*/
|
*/
|
||||||
public interface SystemConst {
|
public interface SystemConst {
|
||||||
/**
|
|
||||||
* win 系统
|
|
||||||
*/
|
|
||||||
String WIN = "win";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mac 系统
|
* win 系统
|
||||||
*/
|
*/
|
||||||
String MAC = "mac";
|
String WIN = "win";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求头名称
|
* mac 系统
|
||||||
*/
|
*/
|
||||||
String HEADER_NAME = "Authorization";
|
String MAC = "mac";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Token前缀
|
* 请求头名称
|
||||||
*/
|
*/
|
||||||
String TOKEN_PREFIX = "Bearer";
|
String HEADER_NAME = "Authorization";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小数点
|
* Token前缀
|
||||||
*/
|
*/
|
||||||
String SYMBOL_DOT = ".";
|
String TOKEN_PREFIX = "Bearer";
|
||||||
String SYMBOL_EQUAL = "=";
|
|
||||||
String SYMBOL_ADD = "+";
|
/**
|
||||||
String SYMBOL_SUBTRACT = "-";
|
* 小数点
|
||||||
String SYMBOL_AT = "@";
|
*/
|
||||||
String PROPERTY_OS_NAME = "os.name";
|
String SYMBOL_DOT = ".";
|
||||||
String OS_NAME_WINDOWS = "Windows";
|
String SYMBOL_EQUAL = "=";
|
||||||
|
String SYMBOL_ADD = "+";
|
||||||
|
String SYMBOL_SUBTRACT = "-";
|
||||||
|
String SYMBOL_AT = "@";
|
||||||
|
String PROPERTY_OS_NAME = "os.name";
|
||||||
|
String OS_NAME_WINDOWS = "Windows";
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-4
@@ -30,8 +30,9 @@ import org.springframework.stereotype.Component;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class BeanLoadFinishListener implements ApplicationListener<ContextRefreshedEvent> {
|
public class BeanLoadFinishListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||||
@Override
|
|
||||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
@Override
|
||||||
log.info("所有的Bean都加载完毕了");
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
}
|
log.info("所有的Bean都加载完毕了");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+19
-18
@@ -32,23 +32,24 @@ import org.springframework.core.env.Environment;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class BootApplication {
|
public class BootApplication {
|
||||||
protected static void inited(ConfigurableApplicationContext application) throws UnknownHostException {
|
|
||||||
Environment env = application.getEnvironment();
|
|
||||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
|
||||||
String port = env.getProperty("server.port");
|
|
||||||
String path = StrUtil.isEmpty(env.getProperty("config.servlet.context-path")) ? ""
|
|
||||||
: env.getProperty("config.servlet.context-path");
|
|
||||||
log.info("""
|
|
||||||
----------------------------------------------------------
|
|
||||||
\tApplication is running! Access URLs:
|
|
||||||
\tLocal: \t\thttp://localhost:{}{}
|
|
||||||
\tExternal: \thttp://{}:{}{}
|
|
||||||
\tSwagger文档: \thttp://{}:{}{}/doc.html
|
|
||||||
----------------------------------------------------------""", port, path, ip, port, path, ip, port, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
protected static void inited(ConfigurableApplicationContext application) throws UnknownHostException {
|
||||||
public KitSpringBeanHolder springBeanHolder() {
|
Environment env = application.getEnvironment();
|
||||||
return new KitSpringBeanHolder();
|
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||||
}
|
String port = env.getProperty("server.port");
|
||||||
|
String path = StrUtil.isEmpty(env.getProperty("config.servlet.context-path")) ? ""
|
||||||
|
: env.getProperty("config.servlet.context-path");
|
||||||
|
log.info("""
|
||||||
|
----------------------------------------------------------
|
||||||
|
\tApplication is running! Access URLs:
|
||||||
|
\tLocal: \t\thttp://localhost:{}{}
|
||||||
|
\tExternal: \thttp://{}:{}{}
|
||||||
|
\tSwagger文档: \thttp://{}:{}{}/doc.html
|
||||||
|
----------------------------------------------------------""", port, path, ip, port, path, ip, port, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public KitSpringBeanHolder springBeanHolder() {
|
||||||
|
return new KitSpringBeanHolder();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+4
-4
@@ -27,8 +27,8 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|||||||
*/
|
*/
|
||||||
public class KitRequestHolder {
|
public class KitRequestHolder {
|
||||||
|
|
||||||
public static HttpServletRequest getHttpServletRequest() {
|
public static HttpServletRequest getHttpServletRequest() {
|
||||||
return ((ServletRequestAttributes)Objects.requireNonNull(
|
return ((ServletRequestAttributes) Objects.requireNonNull(
|
||||||
RequestContextHolder.getRequestAttributes())).getRequest();
|
RequestContextHolder.getRequestAttributes())).getRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-24
@@ -31,32 +31,33 @@ import org.springframework.stereotype.Component;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class KitScriptHelper {
|
public class KitScriptHelper {
|
||||||
public Object evalObj(String script, Map<String, Object> args) {
|
|
||||||
try (Context rhino = Context.enter()) {
|
|
||||||
Scriptable scope = rhino.initStandardObjects();
|
|
||||||
bindArgumentsToScope(scope, args);
|
|
||||||
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
|
||||||
log.info("执行结果: {}", Context.toString(result));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Map<?, ?> args) {
|
public Object evalObj(String script, Map<String, Object> args) {
|
||||||
if (args == null) {
|
try (Context rhino = Context.enter()) {
|
||||||
return;
|
Scriptable scope = rhino.initStandardObjects();
|
||||||
}
|
bindArgumentsToScope(scope, args);
|
||||||
for (Map.Entry<?, ?> entry : args.entrySet()) {
|
Object result = rhino.evaluateString(scope, script, "JavaScript", 1, null);
|
||||||
Object jsValue = Context.javaToJS(entry.getValue(), scope);
|
log.info("执行结果: {}", Context.toString(result));
|
||||||
scope.put(entry.getKey().toString(), scope, jsValue);
|
return result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void bindArgumentsToScope(Scriptable scope, Object args) {
|
private void bindArgumentsToScope(Scriptable scope, Map<?, ?> args) {
|
||||||
if (args == null) {
|
if (args == null) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
// 单个对象参数
|
|
||||||
Object jsValue = Context.javaToJS(args, scope);
|
|
||||||
scope.put("args", scope, jsValue);
|
|
||||||
}
|
}
|
||||||
|
for (Map.Entry<?, ?> entry : args.entrySet()) {
|
||||||
|
Object jsValue = Context.javaToJS(entry.getValue(), scope);
|
||||||
|
scope.put(entry.getKey().toString(), scope, jsValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindArgumentsToScope(Scriptable scope, Object args) {
|
||||||
|
if (args == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 单个对象参数
|
||||||
|
Object jsValue = Context.javaToJS(args, scope);
|
||||||
|
scope.put("args", scope, jsValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+118
-118
@@ -31,140 +31,140 @@ import org.springframework.stereotype.Service;
|
|||||||
@SuppressWarnings({"unchecked", "all"})
|
@SuppressWarnings({"unchecked", "all"})
|
||||||
public class KitSpringBeanHolder implements ApplicationContextAware, DisposableBean {
|
public class KitSpringBeanHolder implements ApplicationContextAware, DisposableBean {
|
||||||
|
|
||||||
private static final List<CallBack> CALL_BACKS = new ArrayList<>();
|
private static final List<CallBack> CALL_BACKS = new ArrayList<>();
|
||||||
private static ApplicationContext applicationContext = null;
|
private static ApplicationContext applicationContext = null;
|
||||||
private static boolean addCallback = true;
|
private static boolean addCallback = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 针对 某些初始化方法, 在SpringContextHolder 未初始化时 提交回调方法。 在SpringContextHolder 初始化后, 进行回调使用
|
* 针对 某些初始化方法, 在SpringContextHolder 未初始化时 提交回调方法。 在SpringContextHolder 初始化后, 进行回调使用
|
||||||
*
|
*
|
||||||
* @param callBack 回调函数
|
* @param callBack 回调函数
|
||||||
*/
|
*/
|
||||||
public synchronized static void addCallBacks(CallBack callBack) {
|
public synchronized static void addCallBacks(CallBack callBack) {
|
||||||
if (addCallback) {
|
if (addCallback) {
|
||||||
KitSpringBeanHolder.CALL_BACKS.add(callBack);
|
KitSpringBeanHolder.CALL_BACKS.add(callBack);
|
||||||
} else {
|
} else {
|
||||||
log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName());
|
log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName());
|
||||||
callBack.executor();
|
callBack.executor();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||||
*/
|
*/
|
||||||
public static <T> T getBean(String name) {
|
public static <T> T getBean(String name) {
|
||||||
assertContextInjected();
|
assertContextInjected();
|
||||||
return (T)applicationContext.getBean(name);
|
return (T) applicationContext.getBean(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||||
|
*/
|
||||||
|
public static <T> T getBean(Class<T> requiredType) {
|
||||||
|
assertContextInjected();
|
||||||
|
return applicationContext.getBean(requiredType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取SpringBoot 配置信息
|
||||||
|
*
|
||||||
|
* @param property 属性key
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @param requiredType 返回类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) {
|
||||||
|
T result = defaultValue;
|
||||||
|
try {
|
||||||
|
result = getBean(Environment.class).getProperty(property, requiredType);
|
||||||
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
* 获取SpringBoot 配置信息
|
||||||
*/
|
*
|
||||||
public static <T> T getBean(Class<T> requiredType) {
|
* @param property 属性key
|
||||||
assertContextInjected();
|
* @return /
|
||||||
return applicationContext.getBean(requiredType);
|
*/
|
||||||
|
public static String getProperties(String property) {
|
||||||
|
return getProperties(property, null, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取SpringBoot 配置信息
|
||||||
|
*
|
||||||
|
* @param property 属性key
|
||||||
|
* @param requiredType 返回类型
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static <T> T getProperties(String property, Class<T> requiredType) {
|
||||||
|
return getProperties(property, null, requiredType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查ApplicationContext不为空.
|
||||||
|
*/
|
||||||
|
private static void assertContextInjected() {
|
||||||
|
if (applicationContext == null) {
|
||||||
|
throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" +
|
||||||
|
".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取SpringBoot 配置信息
|
* 清除SpringContextHolder中的ApplicationContext为Null.
|
||||||
*
|
*/
|
||||||
* @param property 属性key
|
private static void clearHolder() {
|
||||||
* @param defaultValue 默认值
|
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
|
||||||
* @param requiredType 返回类型
|
applicationContext = null;
|
||||||
* @return /
|
}
|
||||||
*/
|
|
||||||
public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) {
|
/**
|
||||||
T result = defaultValue;
|
* 获取 @Service 的所有 bean 名称
|
||||||
try {
|
*
|
||||||
result = getBean(Environment.class).getProperty(property, requiredType);
|
* @return /
|
||||||
} catch (Exception ignored) {
|
*/
|
||||||
}
|
public static List<String> getAllServiceBeanName() {
|
||||||
return result;
|
return new ArrayList<>(Arrays.asList(applicationContext.getBeanNamesForAnnotation(Service.class)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
KitSpringBeanHolder.clearHolder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
if (KitSpringBeanHolder.applicationContext != null) {
|
||||||
|
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" +
|
||||||
|
KitSpringBeanHolder.applicationContext);
|
||||||
}
|
}
|
||||||
|
KitSpringBeanHolder.applicationContext = applicationContext;
|
||||||
/**
|
if (addCallback) {
|
||||||
* 获取SpringBoot 配置信息
|
for (CallBack callBack : KitSpringBeanHolder.CALL_BACKS) {
|
||||||
*
|
callBack.executor();
|
||||||
* @param property 属性key
|
}
|
||||||
* @return /
|
CALL_BACKS.clear();
|
||||||
*/
|
|
||||||
public static String getProperties(String property) {
|
|
||||||
return getProperties(property, null, String.class);
|
|
||||||
}
|
}
|
||||||
|
KitSpringBeanHolder.addCallback = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CallBack {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取SpringBoot 配置信息
|
* 回调执行方法
|
||||||
*
|
|
||||||
* @param property 属性key
|
|
||||||
* @param requiredType 返回类型
|
|
||||||
* @return /
|
|
||||||
*/
|
*/
|
||||||
public static <T> T getProperties(String property, Class<T> requiredType) {
|
void executor();
|
||||||
return getProperties(property, null, requiredType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查ApplicationContext不为空.
|
* 本回调任务名称
|
||||||
*/
|
|
||||||
private static void assertContextInjected() {
|
|
||||||
if (applicationContext == null) {
|
|
||||||
throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" +
|
|
||||||
".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除SpringContextHolder中的ApplicationContext为Null.
|
|
||||||
*/
|
|
||||||
private static void clearHolder() {
|
|
||||||
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
|
|
||||||
applicationContext = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 @Service 的所有 bean 名称
|
|
||||||
*
|
*
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static List<String> getAllServiceBeanName() {
|
default String getCallBackName() {
|
||||||
return new ArrayList<>(Arrays.asList(applicationContext.getBeanNamesForAnnotation(Service.class)));
|
return Thread.currentThread().getId() + ":" + this.getClass().getName();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
KitSpringBeanHolder.clearHolder();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
if (KitSpringBeanHolder.applicationContext != null) {
|
|
||||||
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" +
|
|
||||||
KitSpringBeanHolder.applicationContext);
|
|
||||||
}
|
|
||||||
KitSpringBeanHolder.applicationContext = applicationContext;
|
|
||||||
if (addCallback) {
|
|
||||||
for (CallBack callBack : KitSpringBeanHolder.CALL_BACKS) {
|
|
||||||
callBack.executor();
|
|
||||||
}
|
|
||||||
CALL_BACKS.clear();
|
|
||||||
}
|
|
||||||
KitSpringBeanHolder.addCallback = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CallBack {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 回调执行方法
|
|
||||||
*/
|
|
||||||
void executor();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 本回调任务名称
|
|
||||||
*
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
default String getCallBackName() {
|
|
||||||
return Thread.currentThread().getId() + ":" + this.getClass().getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-14
@@ -16,30 +16,30 @@
|
|||||||
|
|
||||||
package cn.odboy.framework.exception;
|
package cn.odboy.framework.exception;
|
||||||
|
|
||||||
|
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统一异常处理
|
* 统一异常处理
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class BadRequestException extends RuntimeException {
|
public class BadRequestException extends RuntimeException {
|
||||||
|
|
||||||
private Integer status = BAD_REQUEST.value();
|
private Integer status = BAD_REQUEST.value();
|
||||||
|
|
||||||
public BadRequestException(String msg) {
|
public BadRequestException(String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BadRequestException(HttpStatus status, String msg) {
|
public BadRequestException(HttpStatus status, String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
this.status = status.value();
|
this.status = status.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BadRequestException(Throwable cause) {
|
public BadRequestException(Throwable cause) {
|
||||||
super(cause.getMessage());
|
super(cause.getMessage());
|
||||||
this.status = BAD_REQUEST.value();
|
this.status = BAD_REQUEST.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-14
@@ -16,27 +16,27 @@
|
|||||||
|
|
||||||
package cn.odboy.framework.exception;
|
package cn.odboy.framework.exception;
|
||||||
|
|
||||||
|
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class ServerException extends RuntimeException {
|
public class ServerException extends RuntimeException {
|
||||||
|
|
||||||
private Integer status = INTERNAL_SERVER_ERROR.value();
|
private Integer status = INTERNAL_SERVER_ERROR.value();
|
||||||
|
|
||||||
public ServerException(String msg) {
|
public ServerException(String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerException(HttpStatus status, String msg) {
|
public ServerException(HttpStatus status, String msg) {
|
||||||
super(msg);
|
super(msg);
|
||||||
this.status = status.value();
|
this.status = status.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerException(Throwable cause) {
|
public ServerException(Throwable cause) {
|
||||||
super(cause.getMessage());
|
super(cause.getMessage());
|
||||||
this.status = INTERNAL_SERVER_ERROR.value();
|
this.status = INTERNAL_SERVER_ERROR.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-17
@@ -21,26 +21,26 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public final class ApiError {
|
public final class ApiError {
|
||||||
|
|
||||||
private Integer status = 400;
|
private Integer status = 400;
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
private ApiError() {
|
private ApiError() {
|
||||||
timestamp = System.currentTimeMillis();
|
timestamp = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApiError error(final String message) {
|
public static ApiError error(final String message) {
|
||||||
ApiError apiError = new ApiError();
|
ApiError apiError = new ApiError();
|
||||||
apiError.setMessage(message);
|
apiError.setMessage(message);
|
||||||
return apiError;
|
return apiError;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApiError error(final Integer status, final String message) {
|
public static ApiError error(final Integer status, final String message) {
|
||||||
ApiError apiError = new ApiError();
|
ApiError apiError = new ApiError();
|
||||||
apiError.setStatus(status);
|
apiError.setStatus(status);
|
||||||
apiError.setMessage(message);
|
apiError.setMessage(message);
|
||||||
return apiError;
|
return apiError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+47
-47
@@ -37,56 +37,56 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理所有不可知的异常
|
* 处理所有不可知的异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public ResponseEntity<ApiError> handleException(Throwable e) {
|
public ResponseEntity<ApiError> handleException(Throwable e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error(ExceptionUtil.stacktraceToString(e));
|
||||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
return buildResponseEntity(ApiError.error(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BadCredentialsException
|
* BadCredentialsException
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(BadCredentialsException.class)
|
@ExceptionHandler(BadCredentialsException.class)
|
||||||
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e) {
|
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
||||||
log.error(message);
|
log.error(message);
|
||||||
return buildResponseEntity(ApiError.error(message));
|
return buildResponseEntity(ApiError.error(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理自定义异常
|
* 处理自定义异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = BadRequestException.class)
|
@ExceptionHandler(value = BadRequestException.class)
|
||||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error(ExceptionUtil.stacktraceToString(e));
|
||||||
return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理所有接口数据验证异常
|
* 处理所有接口数据验证异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error(ExceptionUtil.stacktraceToString(e));
|
||||||
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
||||||
String message = objectError.getDefaultMessage();
|
String message = objectError.getDefaultMessage();
|
||||||
if (objectError instanceof FieldError fieldError) {
|
if (objectError instanceof FieldError fieldError) {
|
||||||
message = fieldError.getField() + ": " + message;
|
message = fieldError.getField() + ": " + message;
|
||||||
}
|
|
||||||
return buildResponseEntity(ApiError.error(message));
|
|
||||||
}
|
}
|
||||||
|
return buildResponseEntity(ApiError.error(message));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统一返回
|
* 统一返回
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
|
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
|
||||||
return new ResponseEntity<>(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
return new ResponseEntity<>(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-8
@@ -33,13 +33,14 @@ import java.lang.annotation.Target;
|
|||||||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ExecutionTimeStatisticsFace {
|
public @interface ExecutionTimeStatisticsFace {
|
||||||
/**
|
|
||||||
* 接口描述
|
|
||||||
*/
|
|
||||||
String description();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否展示异常日志
|
* 接口描述
|
||||||
*/
|
*/
|
||||||
boolean showErrLog() default true;
|
String description();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否展示异常日志
|
||||||
|
*/
|
||||||
|
boolean showErrLog() default true;
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-16
@@ -33,21 +33,22 @@ import org.springframework.stereotype.Component;
|
|||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class ExecutionTimeStatisticsFaceAspect {
|
public class ExecutionTimeStatisticsFaceAspect {
|
||||||
@Around("@annotation(executionTimeStatisticsFace)")
|
|
||||||
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeStatisticsFace executionTimeStatisticsFace)
|
@Around("@annotation(executionTimeStatisticsFace)")
|
||||||
throws Throwable {
|
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeStatisticsFace executionTimeStatisticsFace)
|
||||||
TimeInterval timeInterval = new TimeInterval();
|
throws Throwable {
|
||||||
try {
|
TimeInterval timeInterval = new TimeInterval();
|
||||||
Object proceed = joinPoint.proceed();
|
try {
|
||||||
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeStatisticsFace.description(),
|
Object proceed = joinPoint.proceed();
|
||||||
timeInterval.intervalMs());
|
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeStatisticsFace.description(),
|
||||||
return proceed;
|
timeInterval.intervalMs());
|
||||||
} catch (Exception e) {
|
return proceed;
|
||||||
if (executionTimeStatisticsFace.showErrLog()) {
|
} catch (Exception e) {
|
||||||
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(),
|
if (executionTimeStatisticsFace.showErrLog()) {
|
||||||
timeInterval.intervalMs(), e);
|
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(),
|
||||||
}
|
timeInterval.intervalMs(), e);
|
||||||
throw e;
|
}
|
||||||
}
|
throw e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-22
@@ -31,29 +31,31 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RequestMapping("/healthCheck")
|
@RequestMapping("/healthCheck")
|
||||||
@Tag(name = "系统:健康检查接口")
|
@Tag(name = "系统:健康检查接口")
|
||||||
public class HealthCheckController {
|
public class HealthCheckController {
|
||||||
@Autowired private KitHealthCheckPointService kitHealthCheckPointService;
|
|
||||||
|
|
||||||
/**
|
@Autowired
|
||||||
* 就绪检查
|
private KitHealthCheckPointService kitHealthCheckPointService;
|
||||||
*/
|
|
||||||
@AnonymousGetMapping(value = "/readiness")
|
|
||||||
public ResponseEntity<?> doReadiness() {
|
|
||||||
return kitHealthCheckPointService.doReadiness();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存活检查
|
* 就绪检查
|
||||||
*/
|
*/
|
||||||
@AnonymousGetMapping(value = "/liveness")
|
@AnonymousGetMapping(value = "/readiness")
|
||||||
public ResponseEntity<?> doLiveness() {
|
public ResponseEntity<?> doReadiness() {
|
||||||
return kitHealthCheckPointService.doLiveness();
|
return kitHealthCheckPointService.doReadiness();
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * 访问首页提示
|
* 存活检查
|
||||||
// */
|
*/
|
||||||
// @AnonymousGetMapping("/")
|
@AnonymousGetMapping(value = "/liveness")
|
||||||
// public String index() {
|
public ResponseEntity<?> doLiveness() {
|
||||||
// return "success";
|
return kitHealthCheckPointService.doLiveness();
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 访问首页提示
|
||||||
|
// */
|
||||||
|
// @AnonymousGetMapping("/")
|
||||||
|
// public String index() {
|
||||||
|
// return "success";
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-8
@@ -19,13 +19,14 @@ package cn.odboy.framework.monitor.service;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
public interface KitHealthCheckPointService {
|
public interface KitHealthCheckPointService {
|
||||||
/**
|
|
||||||
* 就绪检查
|
|
||||||
*/
|
|
||||||
ResponseEntity<?> doReadiness();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存活检查
|
* 就绪检查
|
||||||
*/
|
*/
|
||||||
ResponseEntity<?> doLiveness();
|
ResponseEntity<?> doReadiness();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存活检查
|
||||||
|
*/
|
||||||
|
ResponseEntity<?> doLiveness();
|
||||||
}
|
}
|
||||||
|
|||||||
+114
-114
@@ -25,129 +25,129 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@SuppressWarnings({"unchecked", "all"})
|
@SuppressWarnings({"unchecked", "all"})
|
||||||
public enum DataTypeEnum {
|
public enum DataTypeEnum {
|
||||||
/**
|
/**
|
||||||
* mysql
|
* mysql
|
||||||
*/
|
*/
|
||||||
MYSQL("mysql", "mysql", "com.mysql.jdbc.Driver", "`", "`", "'", "'"),
|
MYSQL("mysql", "mysql", "com.mysql.jdbc.Driver", "`", "`", "'", "'"),
|
||||||
/**
|
/**
|
||||||
* oracle
|
* oracle
|
||||||
*/
|
*/
|
||||||
ORACLE("oracle", "oracle", "oracle.jdbc.driver.OracleDriver", "\"", "\"", "\"", "\""),
|
ORACLE("oracle", "oracle", "oracle.jdbc.driver.OracleDriver", "\"", "\"", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* sql config
|
* sql config
|
||||||
*/
|
*/
|
||||||
SQLSERVER("sqlserver", "sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "\"", "\"", "\"", "\""),
|
SQLSERVER("sqlserver", "sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "\"", "\"", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* h2
|
* h2
|
||||||
*/
|
*/
|
||||||
H2("h2", "h2", "org.h2.Driver", "`", "`", "\"", "\""),
|
H2("h2", "h2", "org.h2.Driver", "`", "`", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* phoenix
|
* phoenix
|
||||||
*/
|
*/
|
||||||
PHOENIX("phoenix", "hbase phoenix", "org.apache.phoenix.jdbc.PhoenixDriver", "", "", "\"", "\""),
|
PHOENIX("phoenix", "hbase phoenix", "org.apache.phoenix.jdbc.PhoenixDriver", "", "", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* mongo
|
* mongo
|
||||||
*/
|
*/
|
||||||
MONGODB("mongo", "mongodb", "mongodb.jdbc.MongoDriver", "`", "`", "\"", "\""),
|
MONGODB("mongo", "mongodb", "mongodb.jdbc.MongoDriver", "`", "`", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* sql4es
|
* sql4es
|
||||||
*/
|
*/
|
||||||
ELASTICSEARCH("sql4es", "elasticsearch", "nl.anchormen.sql4es.jdbc.ESDriver", "", "", "'", "'"),
|
ELASTICSEARCH("sql4es", "elasticsearch", "nl.anchormen.sql4es.jdbc.ESDriver", "", "", "'", "'"),
|
||||||
/**
|
/**
|
||||||
* presto
|
* presto
|
||||||
*/
|
*/
|
||||||
PRESTO("presto", "presto", "com.facebook.presto.jdbc.PrestoDriver", "", "", "\"", "\""),
|
PRESTO("presto", "presto", "com.facebook.presto.jdbc.PrestoDriver", "", "", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* moonbox
|
* moonbox
|
||||||
*/
|
*/
|
||||||
MOONBOX("moonbox", "moonbox", "moonbox.jdbc.MbDriver", "`", "`", "`", "`"),
|
MOONBOX("moonbox", "moonbox", "moonbox.jdbc.MbDriver", "`", "`", "`", "`"),
|
||||||
/**
|
/**
|
||||||
* cassandra
|
* cassandra
|
||||||
*/
|
*/
|
||||||
CASSANDRA("cassandra", "cassandra", "com.github.adejanovski.cassandra.jdbc.CassandraDriver", "", "", "'", "'"),
|
CASSANDRA("cassandra", "cassandra", "com.github.adejanovski.cassandra.jdbc.CassandraDriver", "", "", "'", "'"),
|
||||||
/**
|
/**
|
||||||
* click house
|
* click house
|
||||||
*/
|
*/
|
||||||
CLICKHOUSE("clickhouse", "clickhouse", "ru.yandex.clickhouse.ClickHouseDriver", "", "", "\"", "\""),
|
CLICKHOUSE("clickhouse", "clickhouse", "ru.yandex.clickhouse.ClickHouseDriver", "", "", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* kylin
|
* kylin
|
||||||
*/
|
*/
|
||||||
KYLIN("kylin", "kylin", "org.apache.kylin.jdbc.Driver", "\"", "\"", "\"", "\""),
|
KYLIN("kylin", "kylin", "org.apache.kylin.jdbc.Driver", "\"", "\"", "\"", "\""),
|
||||||
/**
|
/**
|
||||||
* vertica
|
* vertica
|
||||||
*/
|
*/
|
||||||
VERTICA("vertica", "vertica", "com.vertica.jdbc.Driver", "", "", "'", "'"),
|
VERTICA("vertica", "vertica", "com.vertica.jdbc.Driver", "", "", "'", "'"),
|
||||||
/**
|
/**
|
||||||
* sap
|
* sap
|
||||||
*/
|
*/
|
||||||
HANA("sap", "sap hana", "com.sap.db.jdbc.Driver", "", "", "'", "'"),
|
HANA("sap", "sap hana", "com.sap.db.jdbc.Driver", "", "", "'", "'"),
|
||||||
/**
|
/**
|
||||||
* impala
|
* impala
|
||||||
*/
|
*/
|
||||||
IMPALA("impala", "impala", "com.cloudera.impala.jdbc41.Driver", "", "", "'", "'");
|
IMPALA("impala", "impala", "com.cloudera.impala.jdbc41.Driver", "", "", "'", "'");
|
||||||
private static final String JDBC_URL_PREFIX = "jdbc:";
|
private static final String JDBC_URL_PREFIX = "jdbc:";
|
||||||
private String feature;
|
private String feature;
|
||||||
private String desc;
|
private String desc;
|
||||||
private String driver;
|
private String driver;
|
||||||
private String keywordPrefix;
|
private String keywordPrefix;
|
||||||
private String keywordSuffix;
|
private String keywordSuffix;
|
||||||
private String aliasPrefix;
|
private String aliasPrefix;
|
||||||
private String aliasSuffix;
|
private String aliasSuffix;
|
||||||
|
|
||||||
DataTypeEnum(String feature, String desc, String driver, String keywordPrefix, String keywordSuffix,
|
DataTypeEnum(String feature, String desc, String driver, String keywordPrefix, String keywordSuffix,
|
||||||
String aliasPrefix, String aliasSuffix) {
|
String aliasPrefix, String aliasSuffix) {
|
||||||
this.feature = feature;
|
this.feature = feature;
|
||||||
this.desc = desc;
|
this.desc = desc;
|
||||||
this.driver = driver;
|
this.driver = driver;
|
||||||
this.keywordPrefix = keywordPrefix;
|
this.keywordPrefix = keywordPrefix;
|
||||||
this.keywordSuffix = keywordSuffix;
|
this.keywordSuffix = keywordSuffix;
|
||||||
this.aliasPrefix = aliasPrefix;
|
this.aliasPrefix = aliasPrefix;
|
||||||
this.aliasSuffix = aliasSuffix;
|
this.aliasSuffix = aliasSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataTypeEnum urlOf(String jdbcUrl) {
|
public static DataTypeEnum urlOf(String jdbcUrl) {
|
||||||
String url = jdbcUrl.toLowerCase().trim();
|
String url = jdbcUrl.toLowerCase().trim();
|
||||||
for (DataTypeEnum dataTypeEnum : values()) {
|
for (DataTypeEnum dataTypeEnum : values()) {
|
||||||
if (url.startsWith(JDBC_URL_PREFIX + dataTypeEnum.feature)) {
|
if (url.startsWith(JDBC_URL_PREFIX + dataTypeEnum.feature)) {
|
||||||
try {
|
try {
|
||||||
Class<?> aClass = Class.forName(dataTypeEnum.getDriver());
|
Class<?> aClass = Class.forName(dataTypeEnum.getDriver());
|
||||||
if (null == aClass) {
|
if (null == aClass) {
|
||||||
throw new BadRequestException("Unable to get driver instance for jdbcUrl: " + jdbcUrl);
|
throw new BadRequestException("Unable to get driver instance for jdbcUrl: " + jdbcUrl);
|
||||||
}
|
}
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new BadRequestException("Unable to get driver instance: " + jdbcUrl);
|
throw new BadRequestException("Unable to get driver instance: " + jdbcUrl);
|
||||||
}
|
|
||||||
return dataTypeEnum;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return dataTypeEnum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public String getFeature() {
|
public String getFeature() {
|
||||||
return feature;
|
return feature;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDesc() {
|
public String getDesc() {
|
||||||
return desc;
|
return desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDriver() {
|
public String getDriver() {
|
||||||
return driver;
|
return driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKeywordPrefix() {
|
public String getKeywordPrefix() {
|
||||||
return keywordPrefix;
|
return keywordPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKeywordSuffix() {
|
public String getKeywordSuffix() {
|
||||||
return keywordSuffix;
|
return keywordSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAliasPrefix() {
|
public String getAliasPrefix() {
|
||||||
return aliasPrefix;
|
return aliasPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAliasSuffix() {
|
public String getAliasSuffix() {
|
||||||
return aliasSuffix;
|
return aliasSuffix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-52
@@ -20,56 +20,56 @@ package cn.odboy.framework.mybatisplus.config;
|
|||||||
* 动态查询 枚举
|
* 动态查询 枚举
|
||||||
*/
|
*/
|
||||||
public enum KitMpQTypeEnum {
|
public enum KitMpQTypeEnum {
|
||||||
/**
|
/**
|
||||||
* 相等
|
* 相等
|
||||||
*/
|
*/
|
||||||
EQUAL
|
EQUAL
|
||||||
/**
|
/**
|
||||||
* 大于等于
|
* 大于等于
|
||||||
*/
|
*/
|
||||||
, GREATER_THAN
|
, GREATER_THAN
|
||||||
/**
|
/**
|
||||||
* 小于等于
|
* 小于等于
|
||||||
*/
|
*/
|
||||||
, LESS_THAN
|
, LESS_THAN
|
||||||
/**
|
/**
|
||||||
* 中模糊查询
|
* 中模糊查询
|
||||||
*/
|
*/
|
||||||
, INNER_LIKE
|
, INNER_LIKE
|
||||||
/**
|
/**
|
||||||
* 左模糊查询
|
* 左模糊查询
|
||||||
*/
|
*/
|
||||||
, LEFT_LIKE
|
, LEFT_LIKE
|
||||||
/**
|
/**
|
||||||
* 右模糊查询
|
* 右模糊查询
|
||||||
*/
|
*/
|
||||||
, RIGHT_LIKE
|
, RIGHT_LIKE
|
||||||
/**
|
/**
|
||||||
* 小于
|
* 小于
|
||||||
*/
|
*/
|
||||||
, LESS_THAN_NQ
|
, LESS_THAN_NQ
|
||||||
/**
|
/**
|
||||||
* 包含
|
* 包含
|
||||||
*/
|
*/
|
||||||
, IN
|
, IN
|
||||||
/**
|
/**
|
||||||
* 不包含
|
* 不包含
|
||||||
*/
|
*/
|
||||||
, NOT_IN
|
, NOT_IN
|
||||||
/**
|
/**
|
||||||
* 不等于
|
* 不等于
|
||||||
*/
|
*/
|
||||||
, NOT_EQUAL
|
, NOT_EQUAL
|
||||||
/**
|
/**
|
||||||
* between(注:被注解的属性一定得是数组或者集合, 长度大于等于2)
|
* between(注:被注解的属性一定得是数组或者集合, 长度大于等于2)
|
||||||
*/
|
*/
|
||||||
, BETWEEN
|
, BETWEEN
|
||||||
/**
|
/**
|
||||||
* 不为空
|
* 不为空
|
||||||
*/
|
*/
|
||||||
, NOT_NULL
|
, NOT_NULL
|
||||||
/**
|
/**
|
||||||
* 为空
|
* 为空
|
||||||
*/
|
*/
|
||||||
, IS_NULL
|
, IS_NULL
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-12
@@ -29,18 +29,19 @@ import java.lang.annotation.Target;
|
|||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface KitMpQuery {
|
public @interface KitMpQuery {
|
||||||
/**
|
|
||||||
* 基本对象的属性名
|
|
||||||
*/
|
|
||||||
String propName() default "";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询方式
|
* 基本对象的属性名
|
||||||
*/
|
*/
|
||||||
KitMpQTypeEnum type() default KitMpQTypeEnum.EQUAL;
|
String propName() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多字段模糊搜索, 仅支持String类型字段, 多个用逗号隔开, 如@MpQuery(blurry = "email,username")
|
* 查询方式
|
||||||
*/
|
*/
|
||||||
String blurry() default "";
|
KitMpQTypeEnum type() default KitMpQTypeEnum.EQUAL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多字段模糊搜索, 仅支持String类型字段, 多个用逗号隔开, 如@MpQuery(blurry = "email,username")
|
||||||
|
*/
|
||||||
|
String blurry() default "";
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-38
@@ -25,44 +25,45 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MpCustomP6SpyLogger implements MessageFormattingStrategy {
|
public class MpCustomP6SpyLogger implements MessageFormattingStrategy {
|
||||||
/**
|
|
||||||
* 重置颜色
|
|
||||||
*/
|
|
||||||
private static final String RESET = "\u001B[0m";
|
|
||||||
/**
|
|
||||||
* 红色
|
|
||||||
*/
|
|
||||||
private static final String RED = "\u001B[31m";
|
|
||||||
/**
|
|
||||||
* 绿色
|
|
||||||
*/
|
|
||||||
private static final String GREEN = "\u001B[32m";
|
|
||||||
/**
|
|
||||||
* 蓝色
|
|
||||||
*/
|
|
||||||
private static final String BLUE = "\u001B[34m";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化 sql
|
* 重置颜色
|
||||||
*
|
*/
|
||||||
* @param connectionId 连接id
|
private static final String RESET = "\u001B[0m";
|
||||||
* @param now 当前时间
|
/**
|
||||||
* @param elapsed 执行时长
|
* 红色
|
||||||
* @param category sql分类
|
*/
|
||||||
* @param prepared 预编译sql
|
private static final String RED = "\u001B[31m";
|
||||||
* @param sql 执行sql
|
/**
|
||||||
* @param url 数据库连接url
|
* 绿色
|
||||||
* @return 格式化后的sql
|
*/
|
||||||
*/
|
private static final String GREEN = "\u001B[32m";
|
||||||
@Override
|
/**
|
||||||
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared,
|
* 蓝色
|
||||||
String sql, String url) {
|
*/
|
||||||
// 去掉换行和多余空格
|
private static final String BLUE = "\u001B[34m";
|
||||||
if (StrUtil.isNotBlank(sql)) {
|
|
||||||
sql = sql.replaceAll("\\s+", " ").trim();
|
/**
|
||||||
}
|
* 格式化 sql
|
||||||
// 格式化并加上颜色
|
*
|
||||||
return String.format("CuteJava - %s%s%s %s[%s] %sREVIEW_SQL [Interval: %dms]%s -> %s;", RED, now, RESET, GREEN,
|
* @param connectionId 连接id
|
||||||
Thread.currentThread().getName(), BLUE, elapsed, RESET, sql);
|
* @param now 当前时间
|
||||||
|
* @param elapsed 执行时长
|
||||||
|
* @param category sql分类
|
||||||
|
* @param prepared 预编译sql
|
||||||
|
* @param sql 执行sql
|
||||||
|
* @param url 数据库连接url
|
||||||
|
* @return 格式化后的sql
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared,
|
||||||
|
String sql, String url) {
|
||||||
|
// 去掉换行和多余空格
|
||||||
|
if (StrUtil.isNotBlank(sql)) {
|
||||||
|
sql = sql.replaceAll("\\s+", " ").trim();
|
||||||
}
|
}
|
||||||
|
// 格式化并加上颜色
|
||||||
|
return String.format("CuteJava - %s%s%s %s[%s] %sREVIEW_SQL [Interval: %dms]%s -> %s;", RED, now, RESET, GREEN,
|
||||||
|
Thread.currentThread().getName(), BLUE, elapsed, RESET, sql);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+10
-9
@@ -32,13 +32,14 @@ import org.apache.ibatis.session.Configuration;
|
|||||||
* @date 2025-01-11
|
* @date 2025-01-11
|
||||||
*/
|
*/
|
||||||
public class MpInsertBatchSqlInjector extends DefaultSqlInjector {
|
public class MpInsertBatchSqlInjector extends DefaultSqlInjector {
|
||||||
@Override
|
|
||||||
public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
|
@Override
|
||||||
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
|
public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
|
||||||
// 新增自定义方法, InsertBatchSomeColumn这个方法Mp没有开放出来
|
List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
|
||||||
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
|
// 新增自定义方法, InsertBatchSomeColumn这个方法Mp没有开放出来
|
||||||
// 全量更新, 包括null字段
|
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
|
||||||
methodList.add(new AlwaysUpdateSomeColumnById(i -> i.getFieldFill() != FieldFill.INSERT));
|
// 全量更新, 包括null字段
|
||||||
return methodList;
|
methodList.add(new AlwaysUpdateSomeColumnById(i -> i.getFieldFill() != FieldFill.INSERT));
|
||||||
}
|
return methodList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+24
-23
@@ -28,28 +28,29 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
public class MybatisPlusConfig {
|
public class MybatisPlusConfig {
|
||||||
@Bean
|
|
||||||
public MybatisPlusInterceptor paginationInterceptor() {
|
|
||||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
|
||||||
// MySQL的分页拦截器
|
|
||||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
|
|
||||||
paginationInnerInterceptor.setMaxLimit(500L);
|
|
||||||
/// 这个配置会导致:当分页没数据时, 游标从0开始,会导致无论page为什么值,就算分页超出最大页数,依然能够查询到
|
|
||||||
/// paginationInnerInterceptor.setOverflow(true);
|
|
||||||
paginationInnerInterceptor.setOptimizeJoin(true);
|
|
||||||
interceptor.addInnerInterceptor(paginationInnerInterceptor);
|
|
||||||
// 乐观锁
|
|
||||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
|
||||||
// 防全表更新与删除
|
|
||||||
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
|
|
||||||
return interceptor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@Bean
|
||||||
* Mapper支持批量插入
|
public MybatisPlusInterceptor paginationInterceptor() {
|
||||||
*/
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
@Bean
|
// MySQL的分页拦截器
|
||||||
public MpInsertBatchSqlInjector insertBatchSqlInjector() {
|
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
|
||||||
return new MpInsertBatchSqlInjector();
|
paginationInnerInterceptor.setMaxLimit(500L);
|
||||||
}
|
/// 这个配置会导致:当分页没数据时, 游标从0开始,会导致无论page为什么值,就算分页超出最大页数,依然能够查询到
|
||||||
|
/// paginationInnerInterceptor.setOverflow(true);
|
||||||
|
paginationInnerInterceptor.setOptimizeJoin(true);
|
||||||
|
interceptor.addInnerInterceptor(paginationInnerInterceptor);
|
||||||
|
// 乐观锁
|
||||||
|
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||||
|
// 防全表更新与删除
|
||||||
|
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapper支持批量插入
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MpInsertBatchSqlInjector insertBatchSqlInjector() {
|
||||||
|
return new MpInsertBatchSqlInjector();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+46
-46
@@ -43,55 +43,55 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
matchIfMissing = true)
|
matchIfMissing = true)
|
||||||
public class RemoveDruidAdConfig {
|
public class RemoveDruidAdConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方法名: removeDruidAdFilterRegistrationBean 方法描述 除去页面底部的广告
|
* 方法名: removeDruidAdFilterRegistrationBean 方法描述 除去页面底部的广告
|
||||||
*
|
*
|
||||||
* @param properties com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties
|
* @param properties com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties
|
||||||
* @return org.springframework.boot.web.servlet.FilterRegistrationBean
|
* @return org.springframework.boot.web.servlet.FilterRegistrationBean
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
|
public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
|
||||||
|
|
||||||
// 获取web监控页面的参数
|
// 获取web监控页面的参数
|
||||||
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
||||||
// 提取common.js的配置路径
|
// 提取common.js的配置路径
|
||||||
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
||||||
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
||||||
|
|
||||||
final String filePath = "support/http/resources/js/common.js";
|
final String filePath = "support/http/resources/js/common.js";
|
||||||
|
|
||||||
//创建filter进行过滤
|
//创建filter进行过滤
|
||||||
Filter filter = new Filter() {
|
Filter filter = new Filter() {
|
||||||
@Override
|
@Override
|
||||||
public void init(FilterConfig filterConfig) throws ServletException {
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
HttpServletRequest httpRequest = (HttpServletRequest)request;
|
HttpServletRequest httpRequest = (HttpServletRequest) request;
|
||||||
HttpServletResponse httpResponse = (HttpServletResponse)response;
|
HttpServletResponse httpResponse = (HttpServletResponse) response;
|
||||||
if (httpRequest.getRequestURI().endsWith("js/common.js")) {
|
if (httpRequest.getRequestURI().endsWith("js/common.js")) {
|
||||||
// 获取common.js
|
// 获取common.js
|
||||||
String text = Utils.readFromResource(filePath);
|
String text = Utils.readFromResource(filePath);
|
||||||
// 正则替换banner, 除去底部的广告信息
|
// 正则替换banner, 除去底部的广告信息
|
||||||
text = text.replaceAll("<a.*?druid_banner\"></a><br/>", "");
|
text = text.replaceAll("<a.*?druid_banner\"></a><br/>", "");
|
||||||
text = text.replaceAll("powered by.*?shrek.wang</a>", "");
|
text = text.replaceAll("powered by.*?shrek.wang</a>", "");
|
||||||
httpResponse.setContentType("application/javascript");
|
httpResponse.setContentType("application/javascript");
|
||||||
httpResponse.setCharacterEncoding("UTF-8");
|
httpResponse.setCharacterEncoding("UTF-8");
|
||||||
httpResponse.getWriter().write(text);
|
httpResponse.getWriter().write(text);
|
||||||
} else {
|
} else {
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||||
registrationBean.setFilter(filter);
|
registrationBean.setFilter(filter);
|
||||||
registrationBean.addUrlPatterns(commonJsPattern);
|
registrationBean.addUrlPatterns(commonJsPattern);
|
||||||
return registrationBean;
|
return registrationBean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-41
@@ -30,53 +30,54 @@ import java.util.List;
|
|||||||
* @date 2025-05-15
|
* @date 2025-05-15
|
||||||
*/
|
*/
|
||||||
public class KitMpAnyQUtil {
|
public class KitMpAnyQUtil {
|
||||||
public static <T> T selectOne(BaseMapper<T> baseMapper, Object queryParams) {
|
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
|
||||||
return baseMapper.selectOne(queryWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T, M> M selectOne(BaseMapper<T> baseMapper, Object queryParams, Class<M> mapperClazz) {
|
public static <T> T selectOne(BaseMapper<T> baseMapper, Object queryParams) {
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
T mapperTarget = baseMapper.selectOne(queryWrapper);
|
return baseMapper.selectOne(queryWrapper);
|
||||||
if (mapperTarget == null) {
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return BeanUtil.copyProperties(mapperTarget, mapperClazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> List<T> selectList(BaseMapper<T> baseMapper, Object queryParams) {
|
public static <T, M> M selectOne(BaseMapper<T> baseMapper, Object queryParams, Class<M> mapperClazz) {
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
return baseMapper.selectList(queryWrapper);
|
T mapperTarget = baseMapper.selectOne(queryWrapper);
|
||||||
|
if (mapperTarget == null) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
return BeanUtil.copyProperties(mapperTarget, mapperClazz);
|
||||||
|
}
|
||||||
|
|
||||||
public static <T, M> List<M> selectList(BaseMapper<T> baseMapper, Object queryParams, Class<M> mapperClazz) {
|
public static <T> List<T> selectList(BaseMapper<T> baseMapper, Object queryParams) {
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
List<T> mapperTarget = baseMapper.selectList(queryWrapper);
|
return baseMapper.selectList(queryWrapper);
|
||||||
if (mapperTarget == null) {
|
}
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return BeanUtil.copyToList(mapperTarget, mapperClazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> Long selectCount(BaseMapper<T> baseMapper, Object queryParams) {
|
public static <T, M> List<M> selectList(BaseMapper<T> baseMapper, Object queryParams, Class<M> mapperClazz) {
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
return baseMapper.selectCount(queryWrapper);
|
List<T> mapperTarget = baseMapper.selectList(queryWrapper);
|
||||||
|
if (mapperTarget == null) {
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
return BeanUtil.copyToList(mapperTarget, mapperClazz);
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> Page<T> selectPage(BaseMapper<T> baseMapper, Page<T> page, Object queryParams) {
|
public static <T> Long selectCount(BaseMapper<T> baseMapper, Object queryParams) {
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
return baseMapper.selectPage(page, queryWrapper);
|
return baseMapper.selectCount(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T, M> Page<M> selectPage(BaseMapper<T> baseMapper, Page<T> page, Object queryParams,
|
public static <T> Page<T> selectPage(BaseMapper<T> baseMapper, Page<T> page, Object queryParams) {
|
||||||
Class<M> mapperClazz) {
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
return baseMapper.selectPage(page, queryWrapper);
|
||||||
Page<T> tPage = baseMapper.selectPage(page, queryWrapper);
|
}
|
||||||
Page<M> mapperTarget = new Page<>();
|
|
||||||
mapperTarget.setRecords(BeanUtil.copyToList(tPage.getRecords(), mapperClazz));
|
public static <T, M> Page<M> selectPage(BaseMapper<T> baseMapper, Page<T> page, Object queryParams,
|
||||||
mapperTarget.setTotal(tPage.getTotal());
|
Class<M> mapperClazz) {
|
||||||
mapperTarget.setSize(tPage.getSize());
|
QueryWrapper<T> queryWrapper = KitMpQUtil.build(queryParams);
|
||||||
mapperTarget.setCurrent(tPage.getCurrent());
|
Page<T> tPage = baseMapper.selectPage(page, queryWrapper);
|
||||||
return mapperTarget;
|
Page<M> mapperTarget = new Page<>();
|
||||||
}
|
mapperTarget.setRecords(BeanUtil.copyToList(tPage.getRecords(), mapperClazz));
|
||||||
|
mapperTarget.setTotal(tPage.getTotal());
|
||||||
|
mapperTarget.setSize(tPage.getSize());
|
||||||
|
mapperTarget.setCurrent(tPage.getCurrent());
|
||||||
|
return mapperTarget;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+154
-153
@@ -49,165 +49,166 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class KitMpCmdGenUtil {
|
public class KitMpCmdGenUtil {
|
||||||
// 项目根路径。生成结果 C:\Users\Administrator\Documents\GitHub\cutejava\cutejava
|
|
||||||
private static final String projectRootPath = System.getProperty("user.dir") + "/";
|
|
||||||
/**
|
|
||||||
* 数据库类型 MySQL
|
|
||||||
*/
|
|
||||||
private static final IDbQuery DB_QUERY = new MySqlQuery();
|
|
||||||
/**
|
|
||||||
* 类型转换
|
|
||||||
*/
|
|
||||||
private static final String TYPE_TINY_INT = "tinyint";
|
|
||||||
private static final String TYPE_DATETIME = "datetime";
|
|
||||||
private static final String TYPE_DATE = "date";
|
|
||||||
private String databaseUrl;
|
|
||||||
private String databaseUsername;
|
|
||||||
private String databasePassword;
|
|
||||||
private String driverClassName;
|
|
||||||
private String parentPackageName = "cn.odboy";
|
|
||||||
private String parentModuleName;
|
|
||||||
|
|
||||||
private static Consumer<StrategyConfig.Builder> getEntityConfigConsumer() {
|
// 项目根路径。生成结果 C:\Users\Administrator\Documents\GitHub\cutejava\cutejava
|
||||||
return strategyConfigBuilder -> strategyConfigBuilder.entityBuilder().enableLombok()
|
private static final String projectRootPath = System.getProperty("user.dir") + "/";
|
||||||
// 如果不需要生成注解, 去掉.enableTableFieldAnnotation()
|
/**
|
||||||
.enableTableFieldAnnotation().enableFileOverride().naming(NamingStrategy.underline_to_camel)
|
* 数据库类型 MySQL
|
||||||
.columnNaming(NamingStrategy.underline_to_camel).idType(IdType.AUTO);
|
*/
|
||||||
}
|
private static final IDbQuery DB_QUERY = new MySqlQuery();
|
||||||
|
/**
|
||||||
|
* 类型转换
|
||||||
|
*/
|
||||||
|
private static final String TYPE_TINY_INT = "tinyint";
|
||||||
|
private static final String TYPE_DATETIME = "datetime";
|
||||||
|
private static final String TYPE_DATE = "date";
|
||||||
|
private String databaseUrl;
|
||||||
|
private String databaseUsername;
|
||||||
|
private String databasePassword;
|
||||||
|
private String driverClassName;
|
||||||
|
private String parentPackageName = "cn.odboy";
|
||||||
|
private String parentModuleName;
|
||||||
|
|
||||||
private static Consumer<StrategyConfig.Builder> getStrategyConfigConsumer(List<String> tableNames) {
|
private static Consumer<StrategyConfig.Builder> getEntityConfigConsumer() {
|
||||||
return strategyConfigBuilder -> strategyConfigBuilder.enableCapitalMode().enableSkipView().disableSqlFilter()
|
return strategyConfigBuilder -> strategyConfigBuilder.entityBuilder().enableLombok()
|
||||||
.addInclude(tableNames)
|
// 如果不需要生成注解, 去掉.enableTableFieldAnnotation()
|
||||||
// 实体类生成策略
|
.enableTableFieldAnnotation().enableFileOverride().naming(NamingStrategy.underline_to_camel)
|
||||||
.entityBuilder().superClass(KitBaseUserTimeTb.class)
|
.columnNaming(NamingStrategy.underline_to_camel).idType(IdType.AUTO);
|
||||||
.addIgnoreColumns("create_time", "update_time", "create_by", "update_by")
|
}
|
||||||
// 开启生成实体时生成字段注解。
|
|
||||||
// 会在实体类的属性前,添加[@TableField("nickname")]
|
|
||||||
.enableTableFieldAnnotation().disableSerialVersionUID()
|
|
||||||
// 阶段2:Mapper策略配置
|
|
||||||
.mapperBuilder()
|
|
||||||
// 开启 @Mapper 注解。
|
|
||||||
// 会在mapper接口上添加注解[@Mapper]
|
|
||||||
// .enableMapperAnnotation()
|
|
||||||
// 启用 BaseResultMap 生成。
|
|
||||||
// 会在mapper.xml文件生成[通用查询映射结果]配置。
|
|
||||||
// .enableBaseResultMap()
|
|
||||||
// 启用 BaseColumnList。
|
|
||||||
// 会在mapper.xml文件生成[通用查询结果列 ]配置
|
|
||||||
// .enableBaseColumnList()
|
|
||||||
// 阶段4:Controller策略配置
|
|
||||||
.controllerBuilder()
|
|
||||||
// 会在控制类中加[@RestController]注解。
|
|
||||||
.enableRestStyle()
|
|
||||||
// 开启驼峰转连字符
|
|
||||||
.enableHyphenStyle().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Consumer<GlobalConfig.Builder> getGlobalConfigConsumer() {
|
private static Consumer<StrategyConfig.Builder> getStrategyConfigConsumer(List<String> tableNames) {
|
||||||
String outputDir = projectRootPath + "CodeGen";
|
return strategyConfigBuilder -> strategyConfigBuilder.enableCapitalMode().enableSkipView().disableSqlFilter()
|
||||||
return globalConfigBuilder -> globalConfigBuilder.enableSwagger().outputDir(outputDir).author("codegen")
|
.addInclude(tableNames)
|
||||||
.commentDate("yyyy-MM-dd").dateType(DateType.TIME_PACK);
|
// 实体类生成策略
|
||||||
}
|
.entityBuilder().superClass(KitBaseUserTimeTb.class)
|
||||||
|
.addIgnoreColumns("create_time", "update_time", "create_by", "update_by")
|
||||||
|
// 开启生成实体时生成字段注解。
|
||||||
|
// 会在实体类的属性前,添加[@TableField("nickname")]
|
||||||
|
.enableTableFieldAnnotation().disableSerialVersionUID()
|
||||||
|
// 阶段2:Mapper策略配置
|
||||||
|
.mapperBuilder()
|
||||||
|
// 开启 @Mapper 注解。
|
||||||
|
// 会在mapper接口上添加注解[@Mapper]
|
||||||
|
// .enableMapperAnnotation()
|
||||||
|
// 启用 BaseResultMap 生成。
|
||||||
|
// 会在mapper.xml文件生成[通用查询映射结果]配置。
|
||||||
|
// .enableBaseResultMap()
|
||||||
|
// 启用 BaseColumnList。
|
||||||
|
// 会在mapper.xml文件生成[通用查询结果列 ]配置
|
||||||
|
// .enableBaseColumnList()
|
||||||
|
// 阶段4:Controller策略配置
|
||||||
|
.controllerBuilder()
|
||||||
|
// 会在控制类中加[@RestController]注解。
|
||||||
|
.enableRestStyle()
|
||||||
|
// 开启驼峰转连字符
|
||||||
|
.enableHyphenStyle().build();
|
||||||
|
}
|
||||||
|
|
||||||
private static Consumer<StrategyConfig.Builder> getStrategyConfigConsumer() {
|
private static Consumer<GlobalConfig.Builder> getGlobalConfigConsumer() {
|
||||||
return strategyConfigBuilder -> strategyConfigBuilder.mapperBuilder().mapperAnnotation(Mapper.class)
|
String outputDir = projectRootPath + "CodeGen";
|
||||||
.formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").entityBuilder().formatFileName("%sTb");
|
return globalConfigBuilder -> globalConfigBuilder.enableSwagger().outputDir(outputDir).author("codegen")
|
||||||
}
|
.commentDate("yyyy-MM-dd").dateType(DateType.TIME_PACK);
|
||||||
|
}
|
||||||
|
|
||||||
private static Consumer<DataSourceConfig.Builder> getDataSourceConfigConsumer() {
|
private static Consumer<StrategyConfig.Builder> getStrategyConfigConsumer() {
|
||||||
return builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
|
return strategyConfigBuilder -> strategyConfigBuilder.mapperBuilder().mapperAnnotation(Mapper.class)
|
||||||
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
|
.formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").entityBuilder().formatFileName("%sTb");
|
||||||
if (typeCode == Types.SMALLINT) {
|
}
|
||||||
return DbColumnType.INTEGER;
|
|
||||||
}
|
|
||||||
if (typeCode == Types.DATE) {
|
|
||||||
return DbColumnType.DATE;
|
|
||||||
}
|
|
||||||
if (typeCode == Types.TIME) {
|
|
||||||
return DbColumnType.DATE;
|
|
||||||
}
|
|
||||||
if (typeCode == Types.TIMESTAMP) {
|
|
||||||
return DbColumnType.DATE;
|
|
||||||
}
|
|
||||||
return typeRegistry.getColumnType(metaInfo);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private static Consumer<DataSourceConfig.Builder> getDataSourceConfigConsumer() {
|
||||||
* 代码生成函数
|
return builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
|
||||||
*
|
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
|
||||||
* @param moduleName 模块名称
|
if (typeCode == Types.SMALLINT) {
|
||||||
* @param tablePrefix 表前缀(可空)
|
return DbColumnType.INTEGER;
|
||||||
* @param tableNames 表名称, 多个用逗号分隔
|
}
|
||||||
*/
|
if (typeCode == Types.DATE) {
|
||||||
public void gen(String moduleName, String tablePrefix, List<String> tableNames) {
|
return DbColumnType.DATE;
|
||||||
this.parentModuleName = moduleName;
|
}
|
||||||
// 1.数据库配置
|
if (typeCode == Types.TIME) {
|
||||||
DataSourceConfig.Builder dataSourceConfigBuilder =
|
return DbColumnType.DATE;
|
||||||
new DataSourceConfig.Builder(this.databaseUrl, this.databaseUsername,
|
}
|
||||||
this.databasePassword).driverClassName(this.driverClassName);
|
if (typeCode == Types.TIMESTAMP) {
|
||||||
dataSourceConfigBuilder.dbQuery(DB_QUERY);
|
return DbColumnType.DATE;
|
||||||
dataSourceConfigBuilder.keyWordsHandler(new MySqlKeyWordsHandler());
|
}
|
||||||
// 1.1.快速生成器
|
return typeRegistry.getColumnType(metaInfo);
|
||||||
FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder);
|
});
|
||||||
// 2.全局配置
|
}
|
||||||
// 自定义类型转换
|
|
||||||
fastAutoGenerator.dataSourceConfig(getDataSourceConfigConsumer());
|
|
||||||
// 覆盖已生成文件
|
|
||||||
// 添加swagger注解
|
|
||||||
// 设置注释的作者
|
|
||||||
// 设置注释的日期格式
|
|
||||||
// 使用java8新的时间类型
|
|
||||||
fastAutoGenerator.globalConfig(getGlobalConfigConsumer());
|
|
||||||
// 3.包配置
|
|
||||||
// 设置父包名
|
|
||||||
// 设置父包模块名
|
|
||||||
// 设置MVC下各个模块的包名
|
|
||||||
// 设置XML资源文件的目录
|
|
||||||
fastAutoGenerator.packageConfig(getPackageConfigConsumer());
|
|
||||||
// 4.模板配置
|
|
||||||
// 使用Freemarker引擎模板, 默认的是Velocity引擎模板
|
|
||||||
AbstractTemplateEngine templateEngine = new FreemarkerTemplateEngine();
|
|
||||||
fastAutoGenerator.templateEngine(templateEngine);
|
|
||||||
// 5.策略配置
|
|
||||||
// 设置需要生成的表名
|
|
||||||
// 设置过滤表前缀
|
|
||||||
fastAutoGenerator.strategyConfig(getStrategyConfigConsumer(tableNames));
|
|
||||||
if (tablePrefix != null) {
|
|
||||||
// 表前缀, 可多个
|
|
||||||
fastAutoGenerator.strategyConfig(
|
|
||||||
strategyConfigBuilder -> strategyConfigBuilder.addTablePrefix(tablePrefix));
|
|
||||||
}
|
|
||||||
// 5.1.Entity策略配置
|
|
||||||
// 生成实体时生成字段的注解, 包括@TableId注解等
|
|
||||||
// 数据库表和字段映射到实体的命名策略, 为下划线转驼峰
|
|
||||||
// 全局主键类型为None
|
|
||||||
// 实体名称格式化为XXXEntity
|
|
||||||
fastAutoGenerator.strategyConfig(getEntityConfigConsumer());
|
|
||||||
// 5.2.Controller策略配置
|
|
||||||
// 开启生成@RestController控制器
|
|
||||||
fastAutoGenerator.strategyConfig(
|
|
||||||
strategyConfigBuilder -> strategyConfigBuilder.controllerBuilder().enableRestStyle());
|
|
||||||
// 如果不需要生成Controller, fastAutoGenerator.templateConfig(templateConfig -> templateConfig.controller(""))。5.3
|
|
||||||
// .Service策略配置:格式化service接口和实现类的文件名称, 去掉默认的ServiceName前面的I
|
|
||||||
fastAutoGenerator.strategyConfig(
|
|
||||||
strategyConfigBuilder -> strategyConfigBuilder.serviceBuilder().formatServiceFileName("%sService")
|
|
||||||
.formatServiceImplFileName("%sServiceImpl"));
|
|
||||||
// 5.4.Mapper策略配置
|
|
||||||
// 格式化 mapper文件名,格式化xml实现类文件名称
|
|
||||||
fastAutoGenerator.strategyConfig(getStrategyConfigConsumer());
|
|
||||||
// 6.生成代码
|
|
||||||
fastAutoGenerator.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Consumer<PackageConfig.Builder> getPackageConfigConsumer() {
|
/**
|
||||||
String mapperPath = projectRootPath + "CodeGen/resources/mapper";
|
* 代码生成函数
|
||||||
if (StrUtil.isBlank(this.parentModuleName)) {
|
*
|
||||||
throw new BadRequestException("模块名称必填");
|
* @param moduleName 模块名称
|
||||||
}
|
* @param tablePrefix 表前缀(可空)
|
||||||
return packageConfigBuilder -> packageConfigBuilder.parent(this.parentPackageName)
|
* @param tableNames 表名称, 多个用逗号分隔
|
||||||
.moduleName(this.parentModuleName).entity("dal.dataobject").mapper("dal.mysql").service("service")
|
*/
|
||||||
.serviceImpl("service.impl").controller("controller")
|
public void gen(String moduleName, String tablePrefix, List<String> tableNames) {
|
||||||
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperPath));
|
this.parentModuleName = moduleName;
|
||||||
|
// 1.数据库配置
|
||||||
|
DataSourceConfig.Builder dataSourceConfigBuilder =
|
||||||
|
new DataSourceConfig.Builder(this.databaseUrl, this.databaseUsername,
|
||||||
|
this.databasePassword).driverClassName(this.driverClassName);
|
||||||
|
dataSourceConfigBuilder.dbQuery(DB_QUERY);
|
||||||
|
dataSourceConfigBuilder.keyWordsHandler(new MySqlKeyWordsHandler());
|
||||||
|
// 1.1.快速生成器
|
||||||
|
FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder);
|
||||||
|
// 2.全局配置
|
||||||
|
// 自定义类型转换
|
||||||
|
fastAutoGenerator.dataSourceConfig(getDataSourceConfigConsumer());
|
||||||
|
// 覆盖已生成文件
|
||||||
|
// 添加swagger注解
|
||||||
|
// 设置注释的作者
|
||||||
|
// 设置注释的日期格式
|
||||||
|
// 使用java8新的时间类型
|
||||||
|
fastAutoGenerator.globalConfig(getGlobalConfigConsumer());
|
||||||
|
// 3.包配置
|
||||||
|
// 设置父包名
|
||||||
|
// 设置父包模块名
|
||||||
|
// 设置MVC下各个模块的包名
|
||||||
|
// 设置XML资源文件的目录
|
||||||
|
fastAutoGenerator.packageConfig(getPackageConfigConsumer());
|
||||||
|
// 4.模板配置
|
||||||
|
// 使用Freemarker引擎模板, 默认的是Velocity引擎模板
|
||||||
|
AbstractTemplateEngine templateEngine = new FreemarkerTemplateEngine();
|
||||||
|
fastAutoGenerator.templateEngine(templateEngine);
|
||||||
|
// 5.策略配置
|
||||||
|
// 设置需要生成的表名
|
||||||
|
// 设置过滤表前缀
|
||||||
|
fastAutoGenerator.strategyConfig(getStrategyConfigConsumer(tableNames));
|
||||||
|
if (tablePrefix != null) {
|
||||||
|
// 表前缀, 可多个
|
||||||
|
fastAutoGenerator.strategyConfig(
|
||||||
|
strategyConfigBuilder -> strategyConfigBuilder.addTablePrefix(tablePrefix));
|
||||||
}
|
}
|
||||||
|
// 5.1.Entity策略配置
|
||||||
|
// 生成实体时生成字段的注解, 包括@TableId注解等
|
||||||
|
// 数据库表和字段映射到实体的命名策略, 为下划线转驼峰
|
||||||
|
// 全局主键类型为None
|
||||||
|
// 实体名称格式化为XXXEntity
|
||||||
|
fastAutoGenerator.strategyConfig(getEntityConfigConsumer());
|
||||||
|
// 5.2.Controller策略配置
|
||||||
|
// 开启生成@RestController控制器
|
||||||
|
fastAutoGenerator.strategyConfig(
|
||||||
|
strategyConfigBuilder -> strategyConfigBuilder.controllerBuilder().enableRestStyle());
|
||||||
|
// 如果不需要生成Controller, fastAutoGenerator.templateConfig(templateConfig -> templateConfig.controller(""))。5.3
|
||||||
|
// .Service策略配置:格式化service接口和实现类的文件名称, 去掉默认的ServiceName前面的I
|
||||||
|
fastAutoGenerator.strategyConfig(
|
||||||
|
strategyConfigBuilder -> strategyConfigBuilder.serviceBuilder().formatServiceFileName("%sService")
|
||||||
|
.formatServiceImplFileName("%sServiceImpl"));
|
||||||
|
// 5.4.Mapper策略配置
|
||||||
|
// 格式化 mapper文件名,格式化xml实现类文件名称
|
||||||
|
fastAutoGenerator.strategyConfig(getStrategyConfigConsumer());
|
||||||
|
// 6.生成代码
|
||||||
|
fastAutoGenerator.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Consumer<PackageConfig.Builder> getPackageConfigConsumer() {
|
||||||
|
String mapperPath = projectRootPath + "CodeGen/resources/mapper";
|
||||||
|
if (StrUtil.isBlank(this.parentModuleName)) {
|
||||||
|
throw new BadRequestException("模块名称必填");
|
||||||
|
}
|
||||||
|
return packageConfigBuilder -> packageConfigBuilder.parent(this.parentPackageName)
|
||||||
|
.moduleName(this.parentModuleName).entity("dal.dataobject").mapper("dal.mysql").service("service")
|
||||||
|
.serviceImpl("service.impl").controller("controller")
|
||||||
|
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperPath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+142
-135
@@ -42,156 +42,163 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class KitMpQUtil {
|
public class KitMpQUtil {
|
||||||
public static <R, Q> QueryWrapper<R> build(Q query) {
|
|
||||||
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
|
|
||||||
if (query == null) {
|
|
||||||
return queryWrapper;
|
|
||||||
}
|
|
||||||
Class<?> clazz = query.getClass();
|
|
||||||
try {
|
|
||||||
List<Field> fields = getAllFields(clazz, new ArrayList<>());
|
|
||||||
for (Field field : fields) {
|
|
||||||
if (!field.canAccess(query)) {
|
|
||||||
field.setAccessible(true);
|
|
||||||
KitMpQuery q = field.getAnnotation(KitMpQuery.class);
|
|
||||||
if (q != null) {
|
|
||||||
String attributeName = getAttributeName(q, field);
|
|
||||||
Object fieldVal = field.get(query);
|
|
||||||
if (ObjectUtil.isNull(fieldVal) || "".equals(fieldVal)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (StrUtil.isNotBlank(q.blurry())) {
|
|
||||||
handleBlurryQuery(queryWrapper, q.blurry(), fieldVal);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
handleWrapper(q, queryWrapper, attributeName, fieldVal);
|
|
||||||
}
|
|
||||||
field.setAccessible(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to build query wrapper for class: {}", clazz.getName(), e);
|
|
||||||
}
|
|
||||||
return queryWrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getAttributeName(KitMpQuery q, Field field) {
|
public static <R, Q> QueryWrapper<R> build(Q query) {
|
||||||
String attributeName = StrUtil.isBlank(q.propName()) ? field.getName() : q.propName();
|
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
|
||||||
return StrUtil.toUnderlineCase(attributeName);
|
if (query == null) {
|
||||||
|
return queryWrapper;
|
||||||
}
|
}
|
||||||
|
Class<?> clazz = query.getClass();
|
||||||
private static <R> void handleWrapper(KitMpQuery q, QueryWrapper<R> queryWrapper, String attributeName,
|
try {
|
||||||
Object fieldVal) {
|
List<Field> fields = getAllFields(clazz, new ArrayList<>());
|
||||||
switch (q.type()) {
|
for (Field field : fields) {
|
||||||
case EQUAL -> queryWrapper.eq(attributeName, fieldVal);
|
if (!field.canAccess(query)) {
|
||||||
case GREATER_THAN -> queryWrapper.ge(attributeName, fieldVal);
|
field.setAccessible(true);
|
||||||
case LESS_THAN -> queryWrapper.le(attributeName, fieldVal);
|
KitMpQuery q = field.getAnnotation(KitMpQuery.class);
|
||||||
case LESS_THAN_NQ -> queryWrapper.lt(attributeName, fieldVal);
|
if (q != null) {
|
||||||
case INNER_LIKE -> queryWrapper.like(attributeName, fieldVal);
|
String attributeName = getAttributeName(q, field);
|
||||||
case LEFT_LIKE -> queryWrapper.likeLeft(attributeName, fieldVal);
|
Object fieldVal = field.get(query);
|
||||||
case RIGHT_LIKE -> queryWrapper.likeRight(attributeName, fieldVal);
|
if (ObjectUtil.isNull(fieldVal) || "".equals(fieldVal)) {
|
||||||
case IN -> handleInOrNotQuery(true, queryWrapper, attributeName, fieldVal);
|
continue;
|
||||||
case NOT_IN -> handleInOrNotQuery(false, queryWrapper, attributeName, fieldVal);
|
|
||||||
case NOT_EQUAL -> queryWrapper.ne(attributeName, fieldVal);
|
|
||||||
case NOT_NULL -> queryWrapper.isNotNull(attributeName);
|
|
||||||
case IS_NULL -> queryWrapper.isNull(attributeName);
|
|
||||||
case BETWEEN -> handleBetweenQuery(queryWrapper, fieldVal, attributeName);
|
|
||||||
default -> {
|
|
||||||
}
|
}
|
||||||
|
if (StrUtil.isNotBlank(q.blurry())) {
|
||||||
|
handleBlurryQuery(queryWrapper, q.blurry(), fieldVal);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
handleWrapper(q, queryWrapper, attributeName, fieldVal);
|
||||||
|
}
|
||||||
|
field.setAccessible(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to build query wrapper for class: {}", clazz.getName(), e);
|
||||||
}
|
}
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
private static <R> void handleInOrNotQuery(boolean b, QueryWrapper<R> queryWrapper, String attributeName,
|
private static String getAttributeName(KitMpQuery q, Field field) {
|
||||||
Object fieldVal) {
|
String attributeName = StrUtil.isBlank(q.propName()) ? field.getName() : q.propName();
|
||||||
Collection<?> wrapNotInVal = (Collection<?>)fieldVal;
|
return StrUtil.toUnderlineCase(attributeName);
|
||||||
if (CollectionUtil.isNotEmpty(wrapNotInVal)) {
|
}
|
||||||
Optional<?> anyValOptional = wrapNotInVal.stream().findAny();
|
|
||||||
if (anyValOptional.isPresent()) {
|
private static <R> void handleWrapper(KitMpQuery q, QueryWrapper<R> queryWrapper, String attributeName,
|
||||||
Object o = anyValOptional.get();
|
Object fieldVal) {
|
||||||
if (o instanceof Long) {
|
switch (q.type()) {
|
||||||
if (b) {
|
case EQUAL -> queryWrapper.eq(attributeName, fieldVal);
|
||||||
queryWrapper.in(attributeName, fieldVal);
|
case GREATER_THAN -> queryWrapper.ge(attributeName, fieldVal);
|
||||||
} else {
|
case LESS_THAN -> queryWrapper.le(attributeName, fieldVal);
|
||||||
queryWrapper.notIn(attributeName, fieldVal);
|
case LESS_THAN_NQ -> queryWrapper.lt(attributeName, fieldVal);
|
||||||
}
|
case INNER_LIKE -> queryWrapper.like(attributeName, fieldVal);
|
||||||
} else if (o instanceof Integer) {
|
case LEFT_LIKE -> queryWrapper.likeLeft(attributeName, fieldVal);
|
||||||
if (b) {
|
case RIGHT_LIKE -> queryWrapper.likeRight(attributeName, fieldVal);
|
||||||
queryWrapper.in(attributeName, fieldVal);
|
case IN -> handleInOrNotQuery(true, queryWrapper, attributeName, fieldVal);
|
||||||
} else {
|
case NOT_IN -> handleInOrNotQuery(false, queryWrapper, attributeName, fieldVal);
|
||||||
queryWrapper.notIn(attributeName, fieldVal);
|
case NOT_EQUAL -> queryWrapper.ne(attributeName, fieldVal);
|
||||||
}
|
case NOT_NULL -> queryWrapper.isNotNull(attributeName);
|
||||||
} else {
|
case IS_NULL -> queryWrapper.isNull(attributeName);
|
||||||
throw new BadRequestException("InOrNotIn查询参数类型的对象属性值必须是Long/Integer类型集合");
|
case BETWEEN -> handleBetweenQuery(queryWrapper, fieldVal, attributeName);
|
||||||
}
|
default -> {
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static <R> void handleBetweenQuery(QueryWrapper<R> queryWrapper, Object fieldVal,
|
private static <R> void handleInOrNotQuery(boolean b, QueryWrapper<R> queryWrapper, String attributeName,
|
||||||
String finalAttributeName) {
|
Object fieldVal) {
|
||||||
if (fieldVal instanceof List) {
|
Collection<?> wrapNotInVal = (Collection<?>) fieldVal;
|
||||||
List<Object> between = new ArrayList<>((List<?>)fieldVal);
|
if (CollectionUtil.isNotEmpty(wrapNotInVal)) {
|
||||||
int minLength = 2;
|
Optional<?> anyValOptional = wrapNotInVal.stream().findAny();
|
||||||
if (CollectionUtil.isNotEmpty(between) && between.size() >= minLength) {
|
if (anyValOptional.isPresent()) {
|
||||||
queryWrapper.between(finalAttributeName, between.get(0), between.get(1));
|
Object o = anyValOptional.get();
|
||||||
} else {
|
if (o instanceof Long) {
|
||||||
throw new BadRequestException("BETWEEN类型的对象列表长度必须 >= 2");
|
if (b) {
|
||||||
}
|
queryWrapper.in(attributeName, fieldVal);
|
||||||
|
} else {
|
||||||
|
queryWrapper.notIn(attributeName, fieldVal);
|
||||||
|
}
|
||||||
|
} else if (o instanceof Integer) {
|
||||||
|
if (b) {
|
||||||
|
queryWrapper.in(attributeName, fieldVal);
|
||||||
|
} else {
|
||||||
|
queryWrapper.notIn(attributeName, fieldVal);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new BadRequestException("BETWEEN类型的对象必须是一个List子集合");
|
throw new BadRequestException("InOrNotIn查询参数类型的对象属性值必须是Long/Integer类型集合");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
private static <R> void handleBetweenQuery(QueryWrapper<R> queryWrapper, Object fieldVal,
|
||||||
* 模糊多字段
|
String finalAttributeName) {
|
||||||
*
|
if (fieldVal instanceof List) {
|
||||||
* @param queryWrapper /
|
List<Object> between = new ArrayList<>((List<?>) fieldVal);
|
||||||
* @param blurry /
|
int minLength = 2;
|
||||||
* @param fieldVal /
|
if (CollectionUtil.isNotEmpty(between) && between.size() >= minLength) {
|
||||||
* @param <R> /
|
queryWrapper.between(finalAttributeName, between.get(0), between.get(1));
|
||||||
*/
|
} else {
|
||||||
private static <R> void handleBlurryQuery(QueryWrapper<R> queryWrapper, String blurry, Object fieldVal) {
|
throw new BadRequestException("BETWEEN类型的对象列表长度必须 >= 2");
|
||||||
List<String> blurryList = Arrays.stream(blurry.split(",")).filter(StrUtil::isNotBlank).distinct().toList();
|
}
|
||||||
queryWrapper.and(wrapper -> {
|
} else {
|
||||||
for (String blurryItem : blurryList) {
|
throw new BadRequestException("BETWEEN类型的对象必须是一个List子集合");
|
||||||
String column = StrUtil.toUnderlineCase(blurryItem);
|
|
||||||
wrapper.or();
|
|
||||||
wrapper.like(column, fieldVal.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<Field> getAllFields(Class<?> clazz, List<Field> fields) {
|
/**
|
||||||
if (clazz != null) {
|
* 模糊多字段
|
||||||
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
*
|
||||||
getAllFields(clazz.getSuperclass(), fields);
|
* @param queryWrapper /
|
||||||
}
|
* @param blurry /
|
||||||
return fields;
|
* @param fieldVal /
|
||||||
}
|
* @param <R> /
|
||||||
|
*/
|
||||||
|
private static <R> void handleBlurryQuery(QueryWrapper<R> queryWrapper, String blurry, Object fieldVal) {
|
||||||
|
List<String> blurryList = Arrays.stream(blurry.split(",")).filter(StrUtil::isNotBlank).distinct().toList();
|
||||||
|
queryWrapper.and(wrapper -> {
|
||||||
|
for (String blurryItem : blurryList) {
|
||||||
|
String column = StrUtil.toUnderlineCase(blurryItem);
|
||||||
|
wrapper.or();
|
||||||
|
wrapper.like(column, fieldVal.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static List<Field> getAllFields(Class<?> clazz, List<Field> fields) {
|
||||||
QueryWrapper<TestDomain> query = new QueryWrapper<>();
|
if (clazz != null) {
|
||||||
query.or(wrapper -> wrapper.eq("username", 1).or().eq("nickname", 2));
|
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
||||||
query.eq("id", 1);
|
getAllFields(clazz.getSuperclass(), fields);
|
||||||
query.orderByDesc("id");
|
|
||||||
System.err.println("getSqlSelect=================================");
|
|
||||||
System.err.println(query.getSqlSelect());
|
|
||||||
System.err.println("getSqlSegment=================================");
|
|
||||||
System.err.println(query.getSqlSegment());
|
|
||||||
System.err.println("getSqlComment=================================");
|
|
||||||
System.err.println(query.getSqlComment());
|
|
||||||
System.err.println("getSqlSet=================================");
|
|
||||||
System.err.println(query.getSqlSet());
|
|
||||||
System.err.println("getTargetSql=================================");
|
|
||||||
System.err.println(query.getTargetSql());
|
|
||||||
}
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
@TableName("test_domain")
|
public static void main(String[] args) {
|
||||||
private static class TestDomain {
|
QueryWrapper<TestDomain> query = new QueryWrapper<>();
|
||||||
@NotNull(groups = KitBaseUserTimeTb.Update.class) @TableId(value = "user_id", type = IdType.AUTO) private Long
|
query.or(wrapper -> wrapper.eq("username", 1).or().eq("nickname", 2));
|
||||||
id;
|
query.eq("id", 1);
|
||||||
@TableField(value = "dept_id") private Long deptId;
|
query.orderByDesc("id");
|
||||||
@NotBlank private String username;
|
System.err.println("getSqlSelect=================================");
|
||||||
@NotBlank private String nickName;
|
System.err.println(query.getSqlSelect());
|
||||||
}
|
System.err.println("getSqlSegment=================================");
|
||||||
|
System.err.println(query.getSqlSegment());
|
||||||
|
System.err.println("getSqlComment=================================");
|
||||||
|
System.err.println(query.getSqlComment());
|
||||||
|
System.err.println("getSqlSet=================================");
|
||||||
|
System.err.println(query.getSqlSet());
|
||||||
|
System.err.println("getTargetSql=================================");
|
||||||
|
System.err.println(query.getTargetSql());
|
||||||
|
}
|
||||||
|
|
||||||
|
@TableName("test_domain")
|
||||||
|
private static class TestDomain {
|
||||||
|
|
||||||
|
@NotNull(groups = KitBaseUserTimeTb.Update.class)
|
||||||
|
@TableId(value = "user_id", type = IdType.AUTO)
|
||||||
|
private Long
|
||||||
|
id;
|
||||||
|
@TableField(value = "dept_id")
|
||||||
|
private Long deptId;
|
||||||
|
@NotBlank
|
||||||
|
private String username;
|
||||||
|
@NotBlank
|
||||||
|
private String nickName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+131
-130
@@ -42,146 +42,147 @@ import org.springframework.data.domain.Pageable;
|
|||||||
* @date 2022-04-03
|
* @date 2022-04-03
|
||||||
*/
|
*/
|
||||||
public interface KitMpMapper<E> extends BaseMapper<E> {
|
public interface KitMpMapper<E> extends BaseMapper<E> {
|
||||||
/**
|
|
||||||
* 批量插入
|
|
||||||
*
|
|
||||||
* @param entityList List<E>
|
|
||||||
* @return 影响的行数
|
|
||||||
*/
|
|
||||||
int insertBatchSomeColumn(List<E> entityList);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id全量更新
|
* 批量插入
|
||||||
*
|
*
|
||||||
* @param entity E
|
* @param entityList List<E>
|
||||||
* @return 影响的行数
|
* @return 影响的行数
|
||||||
*/
|
*/
|
||||||
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
int insertBatchSomeColumn(List<E> entityList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典
|
* 根据id全量更新
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
* @param entity E
|
||||||
* @param keyName 键对应的列名称
|
* @return 影响的行数
|
||||||
* @param valueName 值对应的列名称
|
*/
|
||||||
* @return Map<String, Object>
|
int alwaysUpdateSomeColumnById(@Param("et") E entity);
|
||||||
*/
|
|
||||||
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
/**
|
||||||
Map<String, Object> result = new ConcurrentHashMap<>(1);
|
* 获取字典
|
||||||
List<E> list = this.selectList(wrapper);
|
*
|
||||||
if (CollUtil.isEmpty(list)) {
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
return new HashMap<>(3);
|
* @param keyName 键对应的列名称
|
||||||
}
|
* @param valueName 值对应的列名称
|
||||||
E tempObj = list.stream().findFirst().orElse(null);
|
* @return Map<String, Object>
|
||||||
if (tempObj == null) {
|
*/
|
||||||
return new HashMap<>(3);
|
default Map<String, Object> getMapWithKv(LambdaQueryWrapper<E> wrapper, String keyName, String valueName) {
|
||||||
}
|
Map<String, Object> result = new ConcurrentHashMap<>(1);
|
||||||
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
List<E> list = this.selectList(wrapper);
|
||||||
for (E e : list) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
Field keyField = null;
|
return new HashMap<>(3);
|
||||||
Field valueField = null;
|
|
||||||
for (Field field : fields) {
|
|
||||||
String fieldName = field.getName();
|
|
||||||
if (fieldName.equals(keyName)) {
|
|
||||||
keyField = field;
|
|
||||||
} else if (fieldName.equals(valueName)) {
|
|
||||||
valueField = field;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
|
||||||
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
|
||||||
if (fieldKey != null) {
|
|
||||||
result.put(fieldKey, fieldValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
E tempObj = list.stream().findFirst().orElse(null);
|
||||||
/**
|
if (tempObj == null) {
|
||||||
* 条件查询列表, 并返回期望的对象
|
return new HashMap<>(3);
|
||||||
*
|
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
|
||||||
* @param clazz 期望的对象类型
|
|
||||||
* @return List<T>
|
|
||||||
*/
|
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
|
||||||
List<E> list = this.selectList(wrapper);
|
|
||||||
if (CollUtil.isEmpty(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return BeanUtil.copyToList(list, clazz);
|
|
||||||
}
|
}
|
||||||
|
Field[] fields = ReflectUtil.getFields(tempObj.getClass());
|
||||||
/**
|
for (E e : list) {
|
||||||
* 条件查询列表, 并返回期望的对象
|
Field keyField = null;
|
||||||
*
|
Field valueField = null;
|
||||||
* @param wrapper LambdaQueryWrapper<E>
|
for (Field field : fields) {
|
||||||
* @param clazz 期望的对象类型
|
String fieldName = field.getName();
|
||||||
* @return List<T>
|
if (fieldName.equals(keyName)) {
|
||||||
*/
|
keyField = field;
|
||||||
default <T> List<T> queryFeatureClazzList(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
} else if (fieldName.equals(valueName)) {
|
||||||
List<E> list = wrapper.list();
|
valueField = field;
|
||||||
if (CollUtil.isEmpty(list)) {
|
break;
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
return BeanUtil.copyToList(list, clazz);
|
}
|
||||||
|
String fieldKey = String.valueOf(ReflectUtil.getFieldValue(e, keyField));
|
||||||
|
Object fieldValue = ReflectUtil.getFieldValue(e, valueField);
|
||||||
|
if (fieldKey != null) {
|
||||||
|
result.put(fieldKey, fieldValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
* 条件查询列表, 并返回期望的对象
|
||||||
*
|
*
|
||||||
* @param pageable 分页参数
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
* @param clazz 期望的对象类型
|
||||||
* @param clazz 期望的对象类型
|
* @return List<T>
|
||||||
* @return IPage<T>
|
*/
|
||||||
*/
|
default <T> List<T> queryFeatureClazzList(LambdaQueryWrapper<E> wrapper, Class<T> clazz) {
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryWrapper<E> wrapper,
|
List<E> list = this.selectList(wrapper);
|
||||||
Class<T> clazz) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
int pageNumber = pageable.getPageNumber();
|
return new ArrayList<>();
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = this.selectPage(new Page<>(pageNumber, pageSize), wrapper);
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return new KitPageResult<>(new ArrayList<>(), pageInfo.getTotal());
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(BeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
|
||||||
}
|
}
|
||||||
|
return BeanUtil.copyToList(list, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查询分页列表, 并返回期望的对象
|
* 条件查询列表, 并返回期望的对象
|
||||||
*
|
*
|
||||||
* @param pageable 分页参数
|
* @param wrapper LambdaQueryWrapper<E>
|
||||||
* @param wrapper LambdaQueryWrapper<E> wrapper
|
* @param clazz 期望的对象类型
|
||||||
* @param clazz 期望的对象类型
|
* @return List<T>
|
||||||
* @return IPage<T>
|
*/
|
||||||
*/
|
default <T> List<T> queryFeatureClazzList(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
||||||
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryChainWrapper<E> wrapper,
|
List<E> list = wrapper.list();
|
||||||
Class<T> clazz) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
int pageNumber = pageable.getPageNumber();
|
return new ArrayList<>();
|
||||||
int pageSize = pageable.getPageSize();
|
|
||||||
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
|
||||||
pageSize = pageSize <= 0 ? 10 : pageSize;
|
|
||||||
IPage<E> pageInfo = wrapper.page(new Page<>(pageNumber, pageSize));
|
|
||||||
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
|
||||||
return KitPageUtil.emptyListData();
|
|
||||||
}
|
|
||||||
return new KitPageResult<>(BeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
|
||||||
}
|
}
|
||||||
|
return BeanUtil.copyToList(list, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取目标类
|
* 条件查询分页列表, 并返回期望的对象
|
||||||
*
|
*
|
||||||
* @param wrapper LambdaQueryChainWrapper<E> wrapper
|
* @param pageable 分页参数
|
||||||
* @param clazz 期望的对象类型
|
* @param wrapper LambdaQueryWrapper<E> wrapper
|
||||||
* @return T
|
* @param clazz 期望的对象类型
|
||||||
*/
|
* @return IPage<T>
|
||||||
default <T> T getFeatureClazz(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
*/
|
||||||
E e = this.selectOne(wrapper);
|
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryWrapper<E> wrapper,
|
||||||
if (e == null) {
|
Class<T> clazz) {
|
||||||
return null;
|
int pageNumber = pageable.getPageNumber();
|
||||||
}
|
int pageSize = pageable.getPageSize();
|
||||||
return BeanUtil.copyProperties(e, clazz);
|
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
||||||
|
pageSize = pageSize <= 0 ? 10 : pageSize;
|
||||||
|
IPage<E> pageInfo = this.selectPage(new Page<>(pageNumber, pageSize), wrapper);
|
||||||
|
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
||||||
|
return new KitPageResult<>(new ArrayList<>(), pageInfo.getTotal());
|
||||||
}
|
}
|
||||||
|
return new KitPageResult<>(BeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件查询分页列表, 并返回期望的对象
|
||||||
|
*
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @param wrapper LambdaQueryWrapper<E> wrapper
|
||||||
|
* @param clazz 期望的对象类型
|
||||||
|
* @return IPage<T>
|
||||||
|
*/
|
||||||
|
default <T> KitPageResult<T> queryFeatureClazzPage(Pageable pageable, LambdaQueryChainWrapper<E> wrapper,
|
||||||
|
Class<T> clazz) {
|
||||||
|
int pageNumber = pageable.getPageNumber();
|
||||||
|
int pageSize = pageable.getPageSize();
|
||||||
|
pageNumber = pageNumber <= 0 ? 1 : pageNumber;
|
||||||
|
pageSize = pageSize <= 0 ? 10 : pageSize;
|
||||||
|
IPage<E> pageInfo = wrapper.page(new Page<>(pageNumber, pageSize));
|
||||||
|
if (CollUtil.isEmpty(pageInfo.getRecords())) {
|
||||||
|
return KitPageUtil.emptyListData();
|
||||||
|
}
|
||||||
|
return new KitPageResult<>(BeanUtil.copyToList(pageInfo.getRecords(), clazz), pageInfo.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取目标类
|
||||||
|
*
|
||||||
|
* @param wrapper LambdaQueryChainWrapper<E> wrapper
|
||||||
|
* @param clazz 期望的对象类型
|
||||||
|
* @return T
|
||||||
|
*/
|
||||||
|
default <T> T getFeatureClazz(LambdaQueryChainWrapper<E> wrapper, Class<T> clazz) {
|
||||||
|
E e = this.selectOne(wrapper);
|
||||||
|
if (e == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return BeanUtil.copyProperties(e, clazz);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-19
@@ -32,40 +32,41 @@ import java.util.List;
|
|||||||
* @date 2022-04-03
|
* @date 2022-04-03
|
||||||
*/
|
*/
|
||||||
public interface KitMpService<T> extends IService<T> {
|
public interface KitMpService<T> extends IService<T> {
|
||||||
<G> int saveFeatureClazz(G resources);
|
|
||||||
|
|
||||||
<G> int saveFeatureClazzList(List<G> resources);
|
<G> int saveFeatureClazz(G resources);
|
||||||
|
|
||||||
<G> int modifyFeatureClazzById(G resources);
|
<G> int saveFeatureClazzList(List<G> resources);
|
||||||
|
|
||||||
<G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize);
|
<G> int modifyFeatureClazzById(G resources);
|
||||||
|
|
||||||
<G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz);
|
<G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize);
|
||||||
|
|
||||||
T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
<G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz);
|
||||||
|
|
||||||
<G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz);
|
T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn);
|
||||||
|
|
||||||
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
<G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz);
|
||||||
|
|
||||||
<Q> List<T> queryClazzListByArgs(Q criteria);
|
<G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz);
|
||||||
|
|
||||||
List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper);
|
<Q> List<T> queryClazzListByArgs(Q criteria);
|
||||||
|
|
||||||
<G, Q> List<G> queryFeatureClazzListByArgs(Q criteria, Class<G> targetClazz);
|
List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper);
|
||||||
|
|
||||||
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
<G, Q> List<G> queryFeatureClazzListByArgs(Q criteria, Class<G> targetClazz);
|
||||||
|
|
||||||
KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
<G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz);
|
||||||
|
|
||||||
<G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable);
|
||||||
Class<G> targetClazz);
|
|
||||||
|
|
||||||
<G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q criteria, IPage<T> pageable, Class<G> targetClazz);
|
<G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
||||||
|
Class<G> targetClazz);
|
||||||
|
|
||||||
<Q> KitPageResult<T> queryClazzPageByArgs(Q criteria, IPage<T> pageable);
|
<G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q criteria, IPage<T> pageable, Class<G> targetClazz);
|
||||||
|
|
||||||
int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity);
|
<Q> KitPageResult<T> queryClazzPageByArgs(Q criteria, IPage<T> pageable);
|
||||||
|
|
||||||
int removeClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity);
|
||||||
|
|
||||||
|
int removeClazzByArgs(LambdaQueryWrapper<T> wrapper);
|
||||||
}
|
}
|
||||||
|
|||||||
+194
-193
@@ -39,221 +39,222 @@ import org.apache.ibatis.binding.MapperMethod;
|
|||||||
* @date 2022-04-03
|
* @date 2022-04-03
|
||||||
*/
|
*/
|
||||||
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
public class KitMpServiceImpl<M extends KitMpMapper<T>, T> extends ServiceImpl<M, T> {
|
||||||
/**
|
|
||||||
* 快速插入
|
|
||||||
*
|
|
||||||
* @param resources /
|
|
||||||
*/
|
|
||||||
public <G> int saveFeatureClazz(G resources) {
|
|
||||||
return getBaseMapper().insert(BeanUtil.copyProperties(resources, this.getEntityClass()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速批量插入
|
* 快速插入
|
||||||
*
|
*
|
||||||
* @param resources /
|
* @param resources /
|
||||||
*/
|
*/
|
||||||
public <G> int saveFeatureClazzList(List<G> resources) {
|
public <G> int saveFeatureClazz(G resources) {
|
||||||
return getBaseMapper().insertBatchSomeColumn(BeanUtil.copyToList(resources, this.getEntityClass()));
|
return getBaseMapper().insert(BeanUtil.copyProperties(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速更新
|
* 快速批量插入
|
||||||
*
|
*
|
||||||
* @param resources /
|
* @param resources /
|
||||||
*/
|
*/
|
||||||
public <G> int modifyFeatureClazzById(G resources) {
|
public <G> int saveFeatureClazzList(List<G> resources) {
|
||||||
return getBaseMapper().updateById(BeanUtil.copyProperties(resources, this.getEntityClass()));
|
return getBaseMapper().insertBatchSomeColumn(BeanUtil.copyToList(resources, this.getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速批量更新
|
* 快速更新
|
||||||
*
|
*
|
||||||
* @param resources /
|
* @param resources /
|
||||||
* @param batchSize 每批数量
|
*/
|
||||||
*/
|
public <G> int modifyFeatureClazzById(G resources) {
|
||||||
public <G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize) {
|
return getBaseMapper().updateById(BeanUtil.copyProperties(resources, this.getEntityClass()));
|
||||||
String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
}
|
||||||
return this.executeBatch(resources, batchSize, (sqlSession, entity) -> {
|
|
||||||
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
|
||||||
param.put("et", BeanUtil.copyProperties(entity, this.getEntityClass()));
|
|
||||||
sqlSession.update(sqlStatement, param);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过id查询
|
* 快速批量更新
|
||||||
*
|
*
|
||||||
* @param id /
|
* @param resources /
|
||||||
* @param targetClazz 目标类型
|
* @param batchSize 每批数量
|
||||||
*/
|
*/
|
||||||
public <G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
public <G> boolean modifyFeatureClazzListById(Collection<G> resources, int batchSize) {
|
||||||
T entity = this.getById(id);
|
String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
||||||
return entity == null ? null : BeanUtil.copyProperties(entity, targetClazz);
|
return this.executeBatch(resources, batchSize, (sqlSession, entity) -> {
|
||||||
}
|
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
||||||
|
param.put("et", BeanUtil.copyProperties(entity, this.getEntityClass()));
|
||||||
|
sqlSession.update(sqlStatement, param);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查一条数据
|
* 通过id查询
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param id /
|
||||||
*/
|
* @param targetClazz 目标类型
|
||||||
public T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
*/
|
||||||
if (orderColumn == null) {
|
public <G> G queryFeatureClazzById(Serializable id, Class<G> targetClazz) {
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
T entity = this.getById(id);
|
||||||
}
|
return entity == null ? null : BeanUtil.copyProperties(entity, targetClazz);
|
||||||
if (wrapper == null) {
|
}
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
|
||||||
}
|
|
||||||
wrapper.last("limit 1");
|
|
||||||
wrapper.orderByDesc(orderColumn);
|
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
|
||||||
if (ts.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return ts.stream().findFirst().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public <G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz) {
|
/**
|
||||||
if (orderColumn == null) {
|
* 查一条数据
|
||||||
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
*
|
||||||
}
|
* @param wrapper 制造条件
|
||||||
if (wrapper == null) {
|
*/
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
public T queryClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn) {
|
||||||
}
|
if (orderColumn == null) {
|
||||||
wrapper.last("limit 1");
|
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
||||||
wrapper.orderByDesc(orderColumn);
|
|
||||||
List<T> ts = baseMapper.selectList(wrapper);
|
|
||||||
if (ts.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return BeanUtil.copyProperties(ts.stream().findFirst().get(), clazz);
|
|
||||||
}
|
}
|
||||||
|
if (wrapper == null) {
|
||||||
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
|
}
|
||||||
|
wrapper.last("limit 1");
|
||||||
|
wrapper.orderByDesc(orderColumn);
|
||||||
|
List<T> ts = baseMapper.selectList(wrapper);
|
||||||
|
if (ts.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ts.stream().findFirst().get();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public <G> G queryFeatureClazzByArgs(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> orderColumn, Class<G> clazz) {
|
||||||
* 通过id集合查询
|
if (orderColumn == null) {
|
||||||
*
|
throw new BadRequestException("参数orderColumn(排序的列)必填");
|
||||||
* @param ids /
|
|
||||||
* @param targetClazz 目标类型
|
|
||||||
*/
|
|
||||||
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
|
||||||
List<T> entitys = this.listByIds(ids);
|
|
||||||
return entitys == null || entitys.isEmpty() ? CollUtil.newArrayList()
|
|
||||||
: BeanUtil.copyToList(entitys, targetClazz);
|
|
||||||
}
|
}
|
||||||
|
if (wrapper == null) {
|
||||||
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
|
}
|
||||||
|
wrapper.last("limit 1");
|
||||||
|
wrapper.orderByDesc(orderColumn);
|
||||||
|
List<T> ts = baseMapper.selectList(wrapper);
|
||||||
|
if (ts.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return BeanUtil.copyProperties(ts.stream().findFirst().get(), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的数据
|
* 通过id集合查询
|
||||||
*
|
*
|
||||||
* @param criteria 动态条件
|
* @param ids /
|
||||||
*/
|
* @param targetClazz 目标类型
|
||||||
public <Q> List<T> queryClazzListByArgs(Q criteria) {
|
*/
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(criteria);
|
public <G> List<G> queryFeatureClazzListByIds(List<Serializable> ids, Class<G> targetClazz) {
|
||||||
return baseMapper.selectList(predicate);
|
List<T> entitys = this.listByIds(ids);
|
||||||
}
|
return entitys == null || entitys.isEmpty() ? CollUtil.newArrayList()
|
||||||
|
: BeanUtil.copyToList(entitys, targetClazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的数据
|
* 查有关的数据
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param criteria 动态条件
|
||||||
*/
|
*/
|
||||||
public List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper) {
|
public <Q> List<T> queryClazzListByArgs(Q criteria) {
|
||||||
if (wrapper == null) {
|
QueryWrapper<T> predicate = KitMpQUtil.build(criteria);
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
return baseMapper.selectList(predicate);
|
||||||
}
|
}
|
||||||
return baseMapper.selectList(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的数据并转成目标类型的数据
|
* 查有关的数据
|
||||||
*
|
*
|
||||||
* @param criteria 动态条件
|
* @param wrapper 制造条件
|
||||||
* @param targetClazz 目标类型
|
*/
|
||||||
*/
|
public List<T> queryClazzListByArgs(LambdaQueryWrapper<T> wrapper) {
|
||||||
public <G, Q> List<G> queryFeatureClazzListByArgs(Q criteria, Class<G> targetClazz) {
|
if (wrapper == null) {
|
||||||
QueryWrapper<T> predicate = KitMpQUtil.build(criteria);
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
List<T> data = baseMapper.selectList(predicate);
|
|
||||||
return BeanUtil.copyToList(data, targetClazz);
|
|
||||||
}
|
}
|
||||||
|
return baseMapper.selectList(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的数据并转换为目标类型
|
* 查有关的数据并转成目标类型的数据
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param criteria 动态条件
|
||||||
* @param targetClazz 目标类型
|
* @param targetClazz 目标类型
|
||||||
*/
|
*/
|
||||||
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
public <G, Q> List<G> queryFeatureClazzListByArgs(Q criteria, Class<G> targetClazz) {
|
||||||
if (wrapper == null) {
|
QueryWrapper<T> predicate = KitMpQUtil.build(criteria);
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
List<T> data = baseMapper.selectList(predicate);
|
||||||
}
|
return BeanUtil.copyToList(data, targetClazz);
|
||||||
return BeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的分页数据
|
* 查有关的数据并转换为目标类型
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param wrapper 制造条件
|
||||||
* @param pageable 分页参数
|
* @param targetClazz 目标类型
|
||||||
*/
|
*/
|
||||||
public KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
public <G> List<G> queryFeatureClazzListByArgs(LambdaQueryWrapper<T> wrapper, Class<G> targetClazz) {
|
||||||
if (wrapper == null) {
|
if (wrapper == null) {
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
|
||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
|
||||||
}
|
}
|
||||||
|
return BeanUtil.copyToList(baseMapper.selectList(wrapper), targetClazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查有关的分页数据
|
* 查有关的分页数据
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param wrapper 制造条件
|
||||||
* @param pageable 分页参数
|
* @param pageable 分页参数
|
||||||
* @param targetClazz 目标类型
|
*/
|
||||||
*/
|
public KitPageResult<T> queryClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable) {
|
||||||
public <G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
if (wrapper == null) {
|
||||||
Class<G> targetClazz) {
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
if (wrapper == null) {
|
|
||||||
wrapper = new LambdaQueryWrapper<>();
|
|
||||||
}
|
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
|
||||||
return new KitPageResult<>(BeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
|
||||||
originPageData.getTotal());
|
|
||||||
}
|
}
|
||||||
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
|
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
public <G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q criteria, IPage<T> pageable, Class<G> targetClazz) {
|
/**
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(criteria);
|
* 查有关的分页数据
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
*
|
||||||
return new KitPageResult<>(BeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
* @param wrapper 制造条件
|
||||||
originPageData.getTotal());
|
* @param pageable 分页参数
|
||||||
|
* @param targetClazz 目标类型
|
||||||
|
*/
|
||||||
|
public <G> KitPageResult<G> queryFeatureClazzPageByArgs(LambdaQueryWrapper<T> wrapper, IPage<T> pageable,
|
||||||
|
Class<G> targetClazz) {
|
||||||
|
if (wrapper == null) {
|
||||||
|
wrapper = new LambdaQueryWrapper<>();
|
||||||
}
|
}
|
||||||
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
|
return new KitPageResult<>(BeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
||||||
|
originPageData.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
public <Q> KitPageResult<T> queryClazzPageByArgs(Q criteria, IPage<T> pageable) {
|
public <G, Q> KitPageResult<G> queryFeatureClazzPageByArgs(Q criteria, IPage<T> pageable, Class<G> targetClazz) {
|
||||||
QueryWrapper<T> wrapper = KitMpQUtil.build(criteria);
|
QueryWrapper<T> wrapper = KitMpQUtil.build(criteria);
|
||||||
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
return new KitPageResult<>(BeanUtil.copyToList(originPageData.getRecords(), targetClazz),
|
||||||
}
|
originPageData.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public <Q> KitPageResult<T> queryClazzPageByArgs(Q criteria, IPage<T> pageable) {
|
||||||
* 条件更新
|
QueryWrapper<T> wrapper = KitMpQUtil.build(criteria);
|
||||||
*
|
IPage<T> originPageData = baseMapper.selectPage(pageable, wrapper);
|
||||||
* @param wrapper 制造条件
|
return new KitPageResult<>(originPageData.getRecords(), originPageData.getTotal());
|
||||||
* @param entity 更新内容
|
}
|
||||||
*/
|
|
||||||
public int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity) {
|
|
||||||
if (wrapper == null) {
|
|
||||||
throw new BadRequestException("参数wrapper必填");
|
|
||||||
}
|
|
||||||
return baseMapper.update(entity, wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件删除
|
* 条件更新
|
||||||
*
|
*
|
||||||
* @param wrapper 制造条件
|
* @param wrapper 制造条件
|
||||||
*/
|
* @param entity 更新内容
|
||||||
public int removeClazzByArgs(LambdaQueryWrapper<T> wrapper) {
|
*/
|
||||||
if (wrapper == null) {
|
public int modifyClazzByArgs(LambdaQueryWrapper<T> wrapper, T entity) {
|
||||||
throw new BadRequestException("参数wrapper必填");
|
if (wrapper == null) {
|
||||||
}
|
throw new BadRequestException("参数wrapper必填");
|
||||||
return baseMapper.delete(wrapper);
|
|
||||||
}
|
}
|
||||||
|
return baseMapper.update(entity, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件删除
|
||||||
|
*
|
||||||
|
* @param wrapper 制造条件
|
||||||
|
*/
|
||||||
|
public int removeClazzByArgs(LambdaQueryWrapper<T> wrapper) {
|
||||||
|
if (wrapper == null) {
|
||||||
|
throw new BadRequestException("参数wrapper必填");
|
||||||
|
}
|
||||||
|
return baseMapper.delete(wrapper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -36,12 +36,13 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
@ConfigurationProperties(prefix = "app")
|
@ConfigurationProperties(prefix = "app")
|
||||||
public class AppProperties {
|
public class AppProperties {
|
||||||
private ContentRsaEncodeSettingModel rsa;
|
|
||||||
private JwtAuthSettingModel jwt;
|
private ContentRsaEncodeSettingModel rsa;
|
||||||
private UserLoginSettingModel login;
|
private JwtAuthSettingModel jwt;
|
||||||
private ThreadPoolSettingModel asyncTaskPool;
|
private UserLoginSettingModel login;
|
||||||
private CaptchaModel captcha;
|
private ThreadPoolSettingModel asyncTaskPool;
|
||||||
private StorageOSSModel oss;
|
private CaptchaModel captcha;
|
||||||
|
private StorageOSSModel oss;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -23,8 +23,9 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class CaptchaModel extends KitObject {
|
public class CaptchaModel extends KitObject {
|
||||||
/**
|
|
||||||
* 验证码有效时间, 单位: 秒
|
/**
|
||||||
*/
|
* 验证码有效时间, 单位: 秒
|
||||||
private Long expireTime;
|
*/
|
||||||
|
private Long expireTime;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -26,5 +26,6 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class ContentRsaEncodeSettingModel extends KitObject {
|
public class ContentRsaEncodeSettingModel extends KitObject {
|
||||||
private String privateKey;
|
|
||||||
|
private String privateKey;
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-16
@@ -26,23 +26,24 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class JwtAuthSettingModel extends KitObject {
|
public class JwtAuthSettingModel extends KitObject {
|
||||||
/**
|
|
||||||
* 必须使用最少88位的Base64对该令牌进行编码
|
|
||||||
*/
|
|
||||||
private String base64Secret;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 令牌过期时间 此处单位/毫秒
|
* 必须使用最少88位的Base64对该令牌进行编码
|
||||||
*/
|
*/
|
||||||
private Long tokenValidityInSeconds;
|
private String base64Secret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token 续期检查
|
* 令牌过期时间 此处单位/毫秒
|
||||||
*/
|
*/
|
||||||
private Long detect;
|
private Long tokenValidityInSeconds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 续期时间
|
* token 续期检查
|
||||||
*/
|
*/
|
||||||
private Long renew;
|
private Long detect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 续期时间
|
||||||
|
*/
|
||||||
|
private Long renew;
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-16
@@ -23,20 +23,21 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class OSSConfigModel extends KitObject {
|
public class OSSConfigModel extends KitObject {
|
||||||
/**
|
|
||||||
* 服务地址
|
/**
|
||||||
*/
|
* 服务地址
|
||||||
private String endpoint;
|
*/
|
||||||
/**
|
private String endpoint;
|
||||||
* 存储桶名称
|
/**
|
||||||
*/
|
* 存储桶名称
|
||||||
private String bucketName;
|
*/
|
||||||
/**
|
private String bucketName;
|
||||||
* Access Key
|
/**
|
||||||
*/
|
* Access Key
|
||||||
private String accessKey;
|
*/
|
||||||
/**
|
private String accessKey;
|
||||||
* Secret Key
|
/**
|
||||||
*/
|
* Secret Key
|
||||||
private String secretKey;
|
*/
|
||||||
|
private String secretKey;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -22,9 +22,10 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class StorageOSSModel {
|
public class StorageOSSModel {
|
||||||
/**
|
|
||||||
* 上传大小, 单位: M
|
/**
|
||||||
*/
|
* 上传大小, 单位: M
|
||||||
private Long maxSize;
|
*/
|
||||||
private OSSConfigModel minio;
|
private Long maxSize;
|
||||||
|
private OSSConfigModel minio;
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-16
@@ -29,20 +29,21 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class ThreadPoolSettingModel extends KitObject {
|
public class ThreadPoolSettingModel extends KitObject {
|
||||||
/**
|
|
||||||
* 核心线程池大小
|
/**
|
||||||
*/
|
* 核心线程池大小
|
||||||
private int corePoolSize;
|
*/
|
||||||
/**
|
private int corePoolSize;
|
||||||
* 最大线程数
|
/**
|
||||||
*/
|
* 最大线程数
|
||||||
private int maxPoolSize;
|
*/
|
||||||
/**
|
private int maxPoolSize;
|
||||||
* 活跃时间
|
/**
|
||||||
*/
|
* 活跃时间
|
||||||
private int keepAliveSeconds;
|
*/
|
||||||
/**
|
private int keepAliveSeconds;
|
||||||
* 队列容量
|
/**
|
||||||
*/
|
* 队列容量
|
||||||
private int queueCapacity;
|
*/
|
||||||
|
private int queueCapacity;
|
||||||
}
|
}
|
||||||
|
|||||||
+81
-79
@@ -38,96 +38,98 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class UserLoginCaptchaSettingModel extends KitObject {
|
public class UserLoginCaptchaSettingModel extends KitObject {
|
||||||
private CaptchaCodeEnum codeType;
|
|
||||||
|
|
||||||
/**
|
private CaptchaCodeEnum codeType;
|
||||||
* 验证码有效期 分钟
|
|
||||||
*/
|
|
||||||
private Long expiration = 5L;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码内容长度
|
* 验证码有效期 分钟
|
||||||
*/
|
*/
|
||||||
private int length = 4;
|
private Long expiration = 5L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码宽度
|
* 验证码内容长度
|
||||||
*/
|
*/
|
||||||
private int width = 111;
|
private int length = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码高度
|
* 验证码宽度
|
||||||
*/
|
*/
|
||||||
private int height = 36;
|
private int width = 111;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码字体
|
* 验证码高度
|
||||||
*/
|
*/
|
||||||
private String fontName;
|
private int height = 36;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字体大小
|
* 验证码字体
|
||||||
*/
|
*/
|
||||||
private int fontSize = 25;
|
private String fontName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 依据配置信息生产验证码
|
* 字体大小
|
||||||
*
|
*/
|
||||||
* @return /
|
private int fontSize = 25;
|
||||||
*/
|
|
||||||
public Captcha getCaptcha() {
|
/**
|
||||||
Captcha captcha;
|
* 依据配置信息生产验证码
|
||||||
switch (codeType) {
|
*
|
||||||
case ARITHMETIC -> {
|
* @return /
|
||||||
// 算术类型 https://gitee.com/whvse/EasyCaptcha
|
*/
|
||||||
captcha = new FixedArithmeticCaptcha(width, height);
|
public Captcha getCaptcha() {
|
||||||
// 几位数运算, 默认是两位
|
Captcha captcha;
|
||||||
captcha.setLen(length);
|
switch (codeType) {
|
||||||
}
|
case ARITHMETIC -> {
|
||||||
case CHINESE -> {
|
// 算术类型 https://gitee.com/whvse/EasyCaptcha
|
||||||
captcha = new ChineseCaptcha(width, height);
|
captcha = new FixedArithmeticCaptcha(width, height);
|
||||||
captcha.setLen(length);
|
// 几位数运算, 默认是两位
|
||||||
}
|
captcha.setLen(length);
|
||||||
case CHINESE_GIF -> {
|
}
|
||||||
captcha = new ChineseGifCaptcha(width, height);
|
case CHINESE -> {
|
||||||
captcha.setLen(length);
|
captcha = new ChineseCaptcha(width, height);
|
||||||
}
|
captcha.setLen(length);
|
||||||
case GIF -> {
|
}
|
||||||
captcha = new GifCaptcha(width, height);
|
case CHINESE_GIF -> {
|
||||||
captcha.setLen(length);
|
captcha = new ChineseGifCaptcha(width, height);
|
||||||
}
|
captcha.setLen(length);
|
||||||
case SPEC -> {
|
}
|
||||||
captcha = new SpecCaptcha(width, height);
|
case GIF -> {
|
||||||
captcha.setLen(length);
|
captcha = new GifCaptcha(width, height);
|
||||||
}
|
captcha.setLen(length);
|
||||||
default -> throw new BadRequestException("验证码配置信息错误!正确配置查看 LoginCodeEnum ");
|
}
|
||||||
}
|
case SPEC -> {
|
||||||
if (StrUtil.isNotBlank(fontName)) {
|
captcha = new SpecCaptcha(width, height);
|
||||||
captcha.setFont(new Font(fontName, Font.PLAIN, fontSize));
|
captcha.setLen(length);
|
||||||
}
|
}
|
||||||
return captcha;
|
default -> throw new BadRequestException("验证码配置信息错误!正确配置查看 LoginCodeEnum ");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(fontName)) {
|
||||||
|
captcha.setFont(new Font(fontName, Font.PLAIN, fontSize));
|
||||||
|
}
|
||||||
|
return captcha;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class FixedArithmeticCaptcha extends ArithmeticCaptcha {
|
||||||
|
|
||||||
|
public FixedArithmeticCaptcha(int width, int height) {
|
||||||
|
super(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class FixedArithmeticCaptcha extends ArithmeticCaptcha {
|
@Override
|
||||||
public FixedArithmeticCaptcha(int width, int height) {
|
protected char[] alphas() {
|
||||||
super(width, height);
|
// 生成随机数字和运算符
|
||||||
}
|
int n1 = num(1, 10), n2 = num(1, 10);
|
||||||
|
int opt = num(3);
|
||||||
|
|
||||||
@Override
|
// 计算结果
|
||||||
protected char[] alphas() {
|
int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
|
||||||
// 生成随机数字和运算符
|
// 转换为字符运算符
|
||||||
int n1 = num(1, 10), n2 = num(1, 10);
|
char optChar = "+-x".charAt(opt);
|
||||||
int opt = num(3);
|
|
||||||
|
|
||||||
// 计算结果
|
this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
|
||||||
int res = new int[] {n1 + n2, n1 - n2, n1 * n2}[opt];
|
this.chars = String.valueOf(res);
|
||||||
// 转换为字符运算符
|
|
||||||
char optChar = "+-x".charAt(opt);
|
|
||||||
|
|
||||||
this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
|
return chars.toCharArray();
|
||||||
this.chars = String.valueOf(res);
|
|
||||||
|
|
||||||
return chars.toCharArray();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -28,9 +28,10 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class UserLoginSettingModel extends KitObject {
|
public class UserLoginSettingModel extends KitObject {
|
||||||
/**
|
|
||||||
* 账号单用户 登录
|
/**
|
||||||
*/
|
* 账号单用户 登录
|
||||||
private boolean single = false;
|
*/
|
||||||
private UserLoginCaptchaSettingModel captchaSetting;
|
private boolean single = false;
|
||||||
|
private UserLoginCaptchaSettingModel captchaSetting;
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-23
@@ -33,31 +33,32 @@ import org.springframework.stereotype.Component;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@Scope("singleton")
|
@Scope("singleton")
|
||||||
public class QuartzConfig {
|
public class QuartzConfig {
|
||||||
/**
|
|
||||||
* 解决Job中注入Spring Bean为null的问题
|
|
||||||
*/
|
|
||||||
@Component("quartzJobFactory")
|
|
||||||
public static class QuartzJobFactory extends AdaptableJobFactory {
|
|
||||||
|
|
||||||
private final AutowireCapableBeanFactory autowireCapableBeanFactory;
|
/**
|
||||||
|
* 解决Job中注入Spring Bean为null的问题
|
||||||
|
*/
|
||||||
|
@Component("quartzJobFactory")
|
||||||
|
public static class QuartzJobFactory extends AdaptableJobFactory {
|
||||||
|
|
||||||
@Autowired
|
private final AutowireCapableBeanFactory autowireCapableBeanFactory;
|
||||||
public QuartzJobFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
|
|
||||||
this.autowireCapableBeanFactory = autowireCapableBeanFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
@Autowired
|
||||||
@Override
|
public QuartzJobFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
|
||||||
protected Object createJobInstance(@NonNull TriggerFiredBundle triggerFiredBundle) throws Exception {
|
this.autowireCapableBeanFactory = autowireCapableBeanFactory;
|
||||||
try {
|
|
||||||
// 调用父类的方法, 把Job注入Spring中
|
|
||||||
Object jobInstance = super.createJobInstance(triggerFiredBundle);
|
|
||||||
autowireCapableBeanFactory.autowireBean(jobInstance);
|
|
||||||
return jobInstance;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Job注入Spring失败, {}", triggerFiredBundle, e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Object createJobInstance(@NonNull TriggerFiredBundle triggerFiredBundle) throws Exception {
|
||||||
|
try {
|
||||||
|
// 调用父类的方法, 把Job注入Spring中
|
||||||
|
Object jobInstance = super.createJobInstance(triggerFiredBundle);
|
||||||
|
autowireCapableBeanFactory.autowireBean(jobInstance);
|
||||||
|
return jobInstance;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Job注入Spring失败, {}", triggerFiredBundle, e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-22
@@ -28,29 +28,30 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class QuartzRunnable implements Callable<Object> {
|
public class QuartzRunnable implements Callable<Object> {
|
||||||
private final Object target;
|
|
||||||
private final Method method;
|
|
||||||
private final String params;
|
|
||||||
|
|
||||||
public QuartzRunnable(String beanName, String methodName, String params)
|
private final Object target;
|
||||||
throws NoSuchMethodException, SecurityException {
|
private final Method method;
|
||||||
this.target = KitSpringBeanHolder.getBean(beanName);
|
private final String params;
|
||||||
this.params = params;
|
|
||||||
if (StringUtils.isNotBlank(params)) {
|
|
||||||
this.method = target.getClass().getDeclaredMethod(methodName, String.class);
|
|
||||||
} else {
|
|
||||||
this.method = target.getClass().getDeclaredMethod(methodName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public QuartzRunnable(String beanName, String methodName, String params)
|
||||||
@SuppressWarnings({"unchecked", "all"})
|
throws NoSuchMethodException, SecurityException {
|
||||||
public Object call() throws Exception {
|
this.target = KitSpringBeanHolder.getBean(beanName);
|
||||||
ReflectionUtils.makeAccessible(method);
|
this.params = params;
|
||||||
if (StringUtils.isNotBlank(params)) {
|
if (StringUtils.isNotBlank(params)) {
|
||||||
return method.invoke(target, params);
|
this.method = target.getClass().getDeclaredMethod(methodName, String.class);
|
||||||
} else {
|
} else {
|
||||||
return method.invoke(target);
|
this.method = target.getClass().getDeclaredMethod(methodName);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings({"unchecked", "all"})
|
||||||
|
public Object call() throws Exception {
|
||||||
|
ReflectionUtils.makeAccessible(method);
|
||||||
|
if (StringUtils.isNotBlank(params)) {
|
||||||
|
return method.invoke(target, params);
|
||||||
|
} else {
|
||||||
|
return method.invoke(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+749
-748
File diff suppressed because it is too large
Load Diff
+133
-132
@@ -48,148 +48,149 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|||||||
@EnableCaching
|
@EnableCaching
|
||||||
@AutoConfigureBefore(RedisAutoConfiguration.class)
|
@AutoConfigureBefore(RedisAutoConfiguration.class)
|
||||||
public class RedisConfiguration {
|
public class RedisConfiguration {
|
||||||
/**
|
|
||||||
* 自动识别json对象白名单配置(仅允许解析的包名, 范围越小越安全)<br/> 未配置可能导致, 登录失败, 反复登录等问题
|
|
||||||
*/
|
|
||||||
private static final String[] WHITELIST_STR =
|
|
||||||
{"org.springframework", "cn.odboy.system.dal.dataobject", "cn.odboy.system.dal.model",
|
|
||||||
"cn.odboy.task.dal.dataobject", "cn.odboy.task.dal.model",};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置 redis 数据默认过期时间,默认2小时 设置@cacheable 序列化方式
|
* 自动识别json对象白名单配置(仅允许解析的包名, 范围越小越安全)<br/> 未配置可能导致, 登录失败, 反复登录等问题
|
||||||
*/
|
*/
|
||||||
@Bean
|
private static final String[] WHITELIST_STR =
|
||||||
public RedisCacheConfiguration redisCacheConfiguration() {
|
{"org.springframework", "cn.odboy.system.dal.dataobject", "cn.odboy.system.dal.model",
|
||||||
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
"cn.odboy.task.dal.dataobject", "cn.odboy.task.dal.model",};
|
||||||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
|
|
||||||
configuration = configuration.serializeValuesWith(
|
/**
|
||||||
RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
|
* 设置 redis 数据默认过期时间,默认2小时 设置@cacheable 序列化方式
|
||||||
.entryTtl(Duration.ofHours(2));
|
*/
|
||||||
return configuration;
|
@Bean
|
||||||
|
public RedisCacheConfiguration redisCacheConfiguration() {
|
||||||
|
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
||||||
|
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
|
||||||
|
configuration = configuration.serializeValuesWith(
|
||||||
|
RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
|
||||||
|
.entryTtl(Duration.ofHours(2));
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "redisTemplate")
|
||||||
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||||
|
// 指定 key 和 value 的序列化方案
|
||||||
|
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
||||||
|
// value值的序列化采用fastJsonRedisSerializer
|
||||||
|
template.setValueSerializer(fastJsonRedisSerializer);
|
||||||
|
template.setHashValueSerializer(fastJsonRedisSerializer);
|
||||||
|
// 设置fastJson的序列化白名单
|
||||||
|
for (String pack : WHITELIST_STR) {
|
||||||
|
JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept(pack);
|
||||||
}
|
}
|
||||||
|
// key的序列化采用StringRedisSerializer
|
||||||
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
|
template.setHashKeySerializer(new StringRedisSerializer());
|
||||||
|
template.setConnectionFactory(redisConnectionFactory);
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean(name = "redisTemplate")
|
/**
|
||||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
* 缓存管理器
|
||||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
*
|
||||||
// 指定 key 和 value 的序列化方案
|
* @param redisConnectionFactory /
|
||||||
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
* @return 缓存管理器
|
||||||
// value值的序列化采用fastJsonRedisSerializer
|
*/
|
||||||
template.setValueSerializer(fastJsonRedisSerializer);
|
@Bean
|
||||||
template.setHashValueSerializer(fastJsonRedisSerializer);
|
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
||||||
// 设置fastJson的序列化白名单
|
RedisCacheConfiguration config = redisCacheConfiguration();
|
||||||
for (String pack : WHITELIST_STR) {
|
return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
|
||||||
JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept(pack);
|
}
|
||||||
}
|
|
||||||
// key的序列化采用StringRedisSerializer
|
/**
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
* 自定义缓存key生成策略
|
||||||
template.setHashKeySerializer(new StringRedisSerializer());
|
*/
|
||||||
template.setConnectionFactory(redisConnectionFactory);
|
|
||||||
return template;
|
@Bean
|
||||||
|
public KeyGenerator keyGenerator() {
|
||||||
|
return (target, method, params) -> {
|
||||||
|
Map<String, Object> container = new HashMap<>(8);
|
||||||
|
Class<?> targetClassClass = target.getClass();
|
||||||
|
// 类地址
|
||||||
|
container.put("class", targetClassClass.toGenericString());
|
||||||
|
// 方法名称
|
||||||
|
container.put("methodName", method.getName());
|
||||||
|
// 包名称
|
||||||
|
container.put("package", targetClassClass.getPackage());
|
||||||
|
// 参数列表
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
container.put(String.valueOf(i), params[i]);
|
||||||
|
}
|
||||||
|
// 转为JSON字符串
|
||||||
|
String jsonString = JSON.toJSONString(container);
|
||||||
|
// 使用 MurmurHash 生成 hash
|
||||||
|
return Integer.toHexString(MurmurHash3.hash32x86(jsonString.getBytes()));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CacheErrorHandler errorHandler() {
|
||||||
|
return new SimpleCacheErrorHandler() {
|
||||||
|
@Override
|
||||||
|
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
|
||||||
|
// 处理缓存读取错误
|
||||||
|
log.error("Cache Get Error: {}", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
|
||||||
|
// 处理缓存写入错误
|
||||||
|
log.error("Cache Put Error: {}", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
|
||||||
|
// 处理缓存删除错误
|
||||||
|
log.error("Cache Evict Error: {}", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCacheClearError(RuntimeException exception, Cache cache) {
|
||||||
|
// 处理缓存清除错误
|
||||||
|
log.error("Cache Clear Error: {}", exception.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value 序列化
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
static class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
|
||||||
|
|
||||||
|
private final Class<T> clazz;
|
||||||
|
|
||||||
|
FastJsonRedisSerializer(Class<T> clazz) {
|
||||||
|
super();
|
||||||
|
this.clazz = clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存管理器
|
* 序列化
|
||||||
*
|
|
||||||
* @param redisConnectionFactory /
|
|
||||||
* @return 缓存管理器
|
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Override
|
||||||
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
public byte[] serialize(T t) throws SerializationException {
|
||||||
RedisCacheConfiguration config = redisCacheConfiguration();
|
if (t == null) {
|
||||||
return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
|
return new byte[0];
|
||||||
|
}
|
||||||
|
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义缓存key生成策略
|
* 反序列化
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
@Bean
|
public T deserialize(byte[] bytes) throws SerializationException {
|
||||||
public KeyGenerator keyGenerator() {
|
if (bytes == null || bytes.length == 0) {
|
||||||
return (target, method, params) -> {
|
return null;
|
||||||
Map<String, Object> container = new HashMap<>(8);
|
}
|
||||||
Class<?> targetClassClass = target.getClass();
|
String str = new String(bytes, StandardCharsets.UTF_8);
|
||||||
// 类地址
|
// 移除对 SupportAutoType 的使用
|
||||||
container.put("class", targetClassClass.toGenericString());
|
return JSON.parseObject(str, clazz);
|
||||||
// 方法名称
|
|
||||||
container.put("methodName", method.getName());
|
|
||||||
// 包名称
|
|
||||||
container.put("package", targetClassClass.getPackage());
|
|
||||||
// 参数列表
|
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
container.put(String.valueOf(i), params[i]);
|
|
||||||
}
|
|
||||||
// 转为JSON字符串
|
|
||||||
String jsonString = JSON.toJSONString(container);
|
|
||||||
// 使用 MurmurHash 生成 hash
|
|
||||||
return Integer.toHexString(MurmurHash3.hash32x86(jsonString.getBytes()));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public CacheErrorHandler errorHandler() {
|
|
||||||
return new SimpleCacheErrorHandler() {
|
|
||||||
@Override
|
|
||||||
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
|
|
||||||
// 处理缓存读取错误
|
|
||||||
log.error("Cache Get Error: {}", exception.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
|
|
||||||
// 处理缓存写入错误
|
|
||||||
log.error("Cache Put Error: {}", exception.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
|
|
||||||
// 处理缓存删除错误
|
|
||||||
log.error("Cache Evict Error: {}", exception.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleCacheClearError(RuntimeException exception, Cache cache) {
|
|
||||||
// 处理缓存清除错误
|
|
||||||
log.error("Cache Clear Error: {}", exception.getMessage());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Value 序列化
|
|
||||||
*
|
|
||||||
* @param <T>
|
|
||||||
*/
|
|
||||||
static class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
|
|
||||||
|
|
||||||
private final Class<T> clazz;
|
|
||||||
|
|
||||||
FastJsonRedisSerializer(Class<T> clazz) {
|
|
||||||
super();
|
|
||||||
this.clazz = clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 序列化
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public byte[] serialize(T t) throws SerializationException {
|
|
||||||
if (t == null) {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 反序列化
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public T deserialize(byte[] bytes) throws SerializationException {
|
|
||||||
if (bytes == null || bytes.length == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String str = new String(bytes, StandardCharsets.UTF_8);
|
|
||||||
// 移除对 SupportAutoType 的使用
|
|
||||||
return JSON.parseObject(str, clazz);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+57
-55
@@ -38,62 +38,64 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|||||||
@EnableAsync
|
@EnableAsync
|
||||||
@Configuration
|
@Configuration
|
||||||
public class AsyncTheadPoolConfig implements AsyncConfigurer {
|
public class AsyncTheadPoolConfig implements AsyncConfigurer {
|
||||||
@Autowired private AppProperties properties;
|
|
||||||
|
|
||||||
/**
|
@Autowired
|
||||||
* 自定义线程池, 用法 @Async
|
private AppProperties properties;
|
||||||
*
|
|
||||||
* @return Executor
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Executor getAsyncExecutor() {
|
|
||||||
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
|
||||||
// 自定义工厂
|
|
||||||
ThreadFactory factory = r -> new Thread(r, "DefaultAsync-" + new AtomicInteger(1).getAndIncrement());
|
|
||||||
// 自定义线程池
|
|
||||||
return new ThreadPoolExecutor(asyncTaskPool.getCorePoolSize(), asyncTaskPool.getMaxPoolSize(),
|
|
||||||
asyncTaskPool.getKeepAliveSeconds(), TimeUnit.SECONDS,
|
|
||||||
new ArrayBlockingQueue<>(asyncTaskPool.getQueueCapacity()), factory,
|
|
||||||
new ThreadPoolExecutor.CallerRunsPolicy());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义线程池, 用法, 注入到类中使用
|
* 自定义线程池, 用法 @Async
|
||||||
*/
|
*
|
||||||
@Bean("taskAsync")
|
* @return Executor
|
||||||
public ThreadPoolTaskExecutor taskAsync() {
|
*/
|
||||||
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
@Override
|
||||||
// 用法 private ThreadPoolTaskExecutor taskExecutor
|
public Executor getAsyncExecutor() {
|
||||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
||||||
executor.setCorePoolSize(asyncTaskPool.getCorePoolSize());
|
// 自定义工厂
|
||||||
executor.setMaxPoolSize(asyncTaskPool.getMaxPoolSize());
|
ThreadFactory factory = r -> new Thread(r, "DefaultAsync-" + new AtomicInteger(1).getAndIncrement());
|
||||||
executor.setQueueCapacity(asyncTaskPool.getQueueCapacity());
|
// 自定义线程池
|
||||||
executor.setThreadNamePrefix("TaskAsync-");
|
return new ThreadPoolExecutor(asyncTaskPool.getCorePoolSize(), asyncTaskPool.getMaxPoolSize(),
|
||||||
executor.setKeepAliveSeconds(asyncTaskPool.getKeepAliveSeconds());
|
asyncTaskPool.getKeepAliveSeconds(), TimeUnit.SECONDS,
|
||||||
// DiscardOldestPolicy,抛弃最早的任务,将新任务加入队列。
|
new ArrayBlockingQueue<>(asyncTaskPool.getQueueCapacity()), factory,
|
||||||
// AbortPolicy,拒绝执行新任务,并抛出异常。
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||||
// CallerRunsPolicy,交由调用者线程执行新任务,如果调用者线程已关闭,则抛弃任务。
|
}
|
||||||
// DiscardPolicy,直接抛弃新任务。
|
|
||||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
|
||||||
// 设置包装器
|
|
||||||
executor.setTaskDecorator(TtlRunnable::get);
|
|
||||||
executor.initialize();
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("longTimeTaskAsync")
|
/**
|
||||||
public ThreadPoolTaskExecutor longTimeTaskAsync() {
|
* 自定义线程池, 用法, 注入到类中使用
|
||||||
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
*/
|
||||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
@Bean("taskAsync")
|
||||||
executor.setCorePoolSize(asyncTaskPool.getCorePoolSize());
|
public ThreadPoolTaskExecutor taskAsync() {
|
||||||
executor.setMaxPoolSize(asyncTaskPool.getMaxPoolSize());
|
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
||||||
executor.setQueueCapacity(asyncTaskPool.getQueueCapacity());
|
// 用法 private ThreadPoolTaskExecutor taskExecutor
|
||||||
executor.setThreadNamePrefix("LongTimeTaskAsync-");
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
executor.setKeepAliveSeconds(asyncTaskPool.getKeepAliveSeconds());
|
executor.setCorePoolSize(asyncTaskPool.getCorePoolSize());
|
||||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
executor.setMaxPoolSize(asyncTaskPool.getMaxPoolSize());
|
||||||
// 设置包装器
|
executor.setQueueCapacity(asyncTaskPool.getQueueCapacity());
|
||||||
executor.setTaskDecorator(TtlRunnable::get);
|
executor.setThreadNamePrefix("TaskAsync-");
|
||||||
executor.initialize();
|
executor.setKeepAliveSeconds(asyncTaskPool.getKeepAliveSeconds());
|
||||||
return executor;
|
// DiscardOldestPolicy,抛弃最早的任务,将新任务加入队列。
|
||||||
}
|
// AbortPolicy,拒绝执行新任务,并抛出异常。
|
||||||
|
// CallerRunsPolicy,交由调用者线程执行新任务,如果调用者线程已关闭,则抛弃任务。
|
||||||
|
// DiscardPolicy,直接抛弃新任务。
|
||||||
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
||||||
|
// 设置包装器
|
||||||
|
executor.setTaskDecorator(TtlRunnable::get);
|
||||||
|
executor.initialize();
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("longTimeTaskAsync")
|
||||||
|
public ThreadPoolTaskExecutor longTimeTaskAsync() {
|
||||||
|
ThreadPoolSettingModel asyncTaskPool = properties.getAsyncTaskPool();
|
||||||
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
|
executor.setCorePoolSize(asyncTaskPool.getCorePoolSize());
|
||||||
|
executor.setMaxPoolSize(asyncTaskPool.getMaxPoolSize());
|
||||||
|
executor.setQueueCapacity(asyncTaskPool.getQueueCapacity());
|
||||||
|
executor.setThreadNamePrefix("LongTimeTaskAsync-");
|
||||||
|
executor.setKeepAliveSeconds(asyncTaskPool.getKeepAliveSeconds());
|
||||||
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
||||||
|
// 设置包装器
|
||||||
|
executor.setTaskDecorator(TtlRunnable::get);
|
||||||
|
executor.initialize();
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-14
@@ -27,20 +27,21 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DateStringArraysToListDateConverter implements Converter<String[], List<Date>> {
|
public class DateStringArraysToListDateConverter implements Converter<String[], List<Date>> {
|
||||||
private static final ThreadLocal<SimpleDateFormat> SDF =
|
|
||||||
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
||||||
|
|
||||||
@Override
|
private static final ThreadLocal<SimpleDateFormat> SDF =
|
||||||
public List<Date> convert(String[] source) {
|
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||||
if (source.length == 0) {
|
|
||||||
return Collections.emptyList();
|
@Override
|
||||||
}
|
public List<Date> convert(String[] source) {
|
||||||
return Arrays.stream(source).map(s -> {
|
if (source.length == 0) {
|
||||||
try {
|
return Collections.emptyList();
|
||||||
return SDF.get().parse(s);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new IllegalArgumentException("Invalid date string: " + s);
|
|
||||||
}
|
|
||||||
}).toList();
|
|
||||||
}
|
}
|
||||||
|
return Arrays.stream(source).map(s -> {
|
||||||
|
try {
|
||||||
|
return SDF.get().parse(s);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid date string: " + s);
|
||||||
|
}
|
||||||
|
}).toList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-12
@@ -25,18 +25,19 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DateStringToDateConverter implements Converter<String, Date> {
|
public class DateStringToDateConverter implements Converter<String, Date> {
|
||||||
private static final ThreadLocal<SimpleDateFormat> SDF =
|
|
||||||
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
||||||
|
|
||||||
@Override
|
private static final ThreadLocal<SimpleDateFormat> SDF =
|
||||||
public Date convert(String source) {
|
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||||
if (StrUtil.isBlank(source)) {
|
|
||||||
return null;
|
@Override
|
||||||
}
|
public Date convert(String source) {
|
||||||
try {
|
if (StrUtil.isBlank(source)) {
|
||||||
return SDF.get().parse(source);
|
return null;
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new IllegalArgumentException("Invalid date string: " + source);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
return SDF.get().parse(source);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid date string: " + source);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+68
-64
@@ -47,72 +47,76 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
public class ConfigurerAdapter implements WebMvcConfigurer {
|
public class ConfigurerAdapter implements WebMvcConfigurer {
|
||||||
@Autowired private DateStringArraysToListDateConverter dateStringArraysToListDateConverter;
|
|
||||||
@Autowired private DateStringToDateConverter dateStringToDateConverter;
|
|
||||||
@Autowired private KitFileLocalUploadHelper fileUploadPathHelper;
|
|
||||||
|
|
||||||
@Bean
|
@Autowired
|
||||||
public CorsFilter corsFilter() {
|
private DateStringArraysToListDateConverter dateStringArraysToListDateConverter;
|
||||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
@Autowired
|
||||||
CorsConfiguration config = new CorsConfiguration();
|
private DateStringToDateConverter dateStringToDateConverter;
|
||||||
config.setAllowCredentials(true);
|
@Autowired
|
||||||
config.addAllowedOriginPattern("*");
|
private KitFileLocalUploadHelper fileUploadPathHelper;
|
||||||
config.addAllowedHeader("*");
|
|
||||||
config.addAllowedMethod("*");
|
|
||||||
source.registerCorsConfiguration("/**", config);
|
|
||||||
return new CorsFilter(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public CorsFilter corsFilter() {
|
||||||
String path = fileUploadPathHelper.getPath();
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
String pathUtl = "file:" + path.replace("\\", "/");
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
registry.addResourceHandler("/avatar/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
config.setAllowCredentials(true);
|
||||||
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
config.addAllowedOriginPattern("*");
|
||||||
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
config.addAllowedHeader("*");
|
||||||
}
|
config.addAllowedMethod("*");
|
||||||
|
source.registerCorsConfiguration("/**", config);
|
||||||
|
return new CorsFilter(source);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Http消息转换器
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
*/
|
String path = fileUploadPathHelper.getPath();
|
||||||
@Override
|
String pathUtl = "file:" + path.replace("\\", "/");
|
||||||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
registry.addResourceHandler("/avatar/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
||||||
// 添加默认的 StringHttpMessageConverter
|
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
||||||
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
||||||
// 配置 FastJsonHttpMessageConverter
|
}
|
||||||
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
|
|
||||||
List<MediaType> supportMediaTypeList = new ArrayList<>();
|
|
||||||
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
|
|
||||||
FastJsonConfig config = new FastJsonConfig();
|
|
||||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
||||||
// 开启引用检测, 枚举支持
|
|
||||||
/// 是否输出值为null的字段
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue);
|
|
||||||
/// 字段如果为null,输出为false,而非null
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteNullBooleanAsFalse);
|
|
||||||
/// 字段如果为null,输出为[],而非null
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteNullListAsEmpty);
|
|
||||||
/// 字符类型字段如果为null,输出为"",而非null
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteNullStringAsEmpty);
|
|
||||||
/// 太具体的数值会直接影响逻辑本身, 所以不要这个
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteNullNumberAsZero);
|
|
||||||
/// 全局Long转字符串,会导致基础框架本身异常, 所以不要这个
|
|
||||||
// config.setWriterFeatures(JSONWriter.Feature.WriteLongAsString);
|
|
||||||
config.setWriterFeatures(JSONWriter.Feature.WriteEnumUsingToString);
|
|
||||||
config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection);
|
|
||||||
fastJsonConverter.setFastJsonConfig(config);
|
|
||||||
fastJsonConverter.setSupportedMediaTypes(supportMediaTypeList);
|
|
||||||
fastJsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
||||||
// 将 FastJsonHttpMessageConverter 添加到列表末尾
|
|
||||||
converters.add(fastJsonConverter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义时间转换器
|
* Http消息转换器
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void addFormatters(FormatterRegistry registry) {
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||||
registry.addConverter(dateStringArraysToListDateConverter);
|
// 添加默认的 StringHttpMessageConverter
|
||||||
registry.addConverter(dateStringToDateConverter);
|
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
||||||
}
|
// 配置 FastJsonHttpMessageConverter
|
||||||
|
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
|
||||||
|
List<MediaType> supportMediaTypeList = new ArrayList<>();
|
||||||
|
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
|
||||||
|
FastJsonConfig config = new FastJsonConfig();
|
||||||
|
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
// 开启引用检测, 枚举支持
|
||||||
|
/// 是否输出值为null的字段
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue);
|
||||||
|
/// 字段如果为null,输出为false,而非null
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteNullBooleanAsFalse);
|
||||||
|
/// 字段如果为null,输出为[],而非null
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteNullListAsEmpty);
|
||||||
|
/// 字符类型字段如果为null,输出为"",而非null
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteNullStringAsEmpty);
|
||||||
|
/// 太具体的数值会直接影响逻辑本身, 所以不要这个
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteNullNumberAsZero);
|
||||||
|
/// 全局Long转字符串,会导致基础框架本身异常, 所以不要这个
|
||||||
|
// config.setWriterFeatures(JSONWriter.Feature.WriteLongAsString);
|
||||||
|
config.setWriterFeatures(JSONWriter.Feature.WriteEnumUsingToString);
|
||||||
|
config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection);
|
||||||
|
fastJsonConverter.setFastJsonConfig(config);
|
||||||
|
fastJsonConverter.setSupportedMediaTypes(supportMediaTypeList);
|
||||||
|
fastJsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
|
||||||
|
// 将 FastJsonHttpMessageConverter 添加到列表末尾
|
||||||
|
converters.add(fastJsonConverter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义时间转换器
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addFormatters(FormatterRegistry registry) {
|
||||||
|
registry.addConverter(dateStringArraysToListDateConverter);
|
||||||
|
registry.addConverter(dateStringToDateConverter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-28
@@ -28,35 +28,37 @@ import org.springframework.stereotype.Component;
|
|||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class KitFileLocalUploadHelper {
|
public class KitFileLocalUploadHelper {
|
||||||
/**
|
|
||||||
* 单位MB
|
|
||||||
*/
|
|
||||||
private static final int AVATAR_MAX_SIZE = 5;
|
|
||||||
private static final String PATH_WINDOWS = "C:\\${appName}\\file\\";
|
|
||||||
private static final String PATH_MAC = "/Users${username}/${appName}/file/";
|
|
||||||
private static final String PATH_LINUX = "/home/${appName}/file/";
|
|
||||||
@Value("${spring.application.name}") private String appName;
|
|
||||||
|
|
||||||
public String getPath() {
|
/**
|
||||||
String os = System.getProperty("os.name");
|
* 单位MB
|
||||||
if (os == null) {
|
*/
|
||||||
// 默认使用 Linux 路径作为兜底方案
|
private static final int AVATAR_MAX_SIZE = 5;
|
||||||
return PATH_LINUX.replace("${appName}", appName);
|
private static final String PATH_WINDOWS = "C:\\${appName}\\file\\";
|
||||||
}
|
private static final String PATH_MAC = "/Users${username}/${appName}/file/";
|
||||||
String lowerOs = os.toLowerCase();
|
private static final String PATH_LINUX = "/home/${appName}/file/";
|
||||||
if (lowerOs.startsWith(SystemConst.WIN)) {
|
@Value("${spring.application.name}")
|
||||||
return PATH_WINDOWS.replace("${appName}", appName);
|
private String appName;
|
||||||
} else if (lowerOs.startsWith(SystemConst.MAC)) {
|
|
||||||
String currentUserName = KitSystemUtil.getCurrentUserName();
|
|
||||||
if (StrUtil.isBlank(currentUserName)) {
|
|
||||||
return PATH_MAC.replace("${appName}", appName).replace("${username}", "");
|
|
||||||
}
|
|
||||||
return PATH_MAC.replace("${appName}", appName).replace("${username}", currentUserName);
|
|
||||||
}
|
|
||||||
return PATH_LINUX.replace("${appName}", appName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAvatarMaxSize() {
|
public String getPath() {
|
||||||
return AVATAR_MAX_SIZE;
|
String os = System.getProperty("os.name");
|
||||||
|
if (os == null) {
|
||||||
|
// 默认使用 Linux 路径作为兜底方案
|
||||||
|
return PATH_LINUX.replace("${appName}", appName);
|
||||||
}
|
}
|
||||||
|
String lowerOs = os.toLowerCase();
|
||||||
|
if (lowerOs.startsWith(SystemConst.WIN)) {
|
||||||
|
return PATH_WINDOWS.replace("${appName}", appName);
|
||||||
|
} else if (lowerOs.startsWith(SystemConst.MAC)) {
|
||||||
|
String currentUserName = KitSystemUtil.getCurrentUserName();
|
||||||
|
if (StrUtil.isBlank(currentUserName)) {
|
||||||
|
return PATH_MAC.replace("${appName}", appName).replace("${username}", "");
|
||||||
|
}
|
||||||
|
return PATH_MAC.replace("${appName}", appName).replace("${username}", currentUserName);
|
||||||
|
}
|
||||||
|
return PATH_LINUX.replace("${appName}", appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAvatarMaxSize() {
|
||||||
|
return AVATAR_MAX_SIZE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-25
@@ -7,38 +7,38 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be
|
* 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used的警告
|
||||||
* used的警告
|
|
||||||
*
|
*
|
||||||
* @author odboy
|
* @author odboy
|
||||||
* @date 2025-10-30
|
* @date 2025-10-30
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class UndertowWebSocketConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
|
public class UndertowWebSocketConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
|
||||||
private int calculateThreadLocalCacheSize(int processorCount) {
|
|
||||||
// 每个I/O线程缓存2-4个缓冲区
|
|
||||||
return Math.max(2, processorCount / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int calculateMaxPoolSize(int processorCount) {
|
private int calculateThreadLocalCacheSize(int processorCount) {
|
||||||
// 基础值 + 根据CPU核心数调整
|
// 每个I/O线程缓存2-4个缓冲区
|
||||||
int baseSize = 100;
|
return Math.max(2, processorCount / 2);
|
||||||
return baseSize + (processorCount * 20);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private int calculateMaxPoolSize(int processorCount) {
|
||||||
public void customize(UndertowServletWebServerFactory factory) {
|
// 基础值 + 根据CPU核心数调整
|
||||||
int processorCount = Runtime.getRuntime().availableProcessors();
|
int baseSize = 100;
|
||||||
int bufferSize = 1024;
|
return baseSize + (processorCount * 20);
|
||||||
int maxPoolSize = calculateMaxPoolSize(processorCount);
|
}
|
||||||
int threadLocalCacheSize = calculateThreadLocalCacheSize(processorCount);
|
|
||||||
|
|
||||||
factory.addDeploymentInfoCustomizers(deploymentInfo -> {
|
@Override
|
||||||
WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
|
public void customize(UndertowServletWebServerFactory factory) {
|
||||||
webSocketDeploymentInfo.setBuffers(
|
int processorCount = Runtime.getRuntime().availableProcessors();
|
||||||
new DefaultByteBufferPool(false, bufferSize, maxPoolSize, threadLocalCacheSize));
|
int bufferSize = 1024;
|
||||||
deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo",
|
int maxPoolSize = calculateMaxPoolSize(processorCount);
|
||||||
webSocketDeploymentInfo);
|
int threadLocalCacheSize = calculateThreadLocalCacheSize(processorCount);
|
||||||
});
|
|
||||||
}
|
factory.addDeploymentInfoCustomizers(deploymentInfo -> {
|
||||||
|
WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
|
||||||
|
webSocketDeploymentInfo.setBuffers(
|
||||||
|
new DefaultByteBufferPool(false, bufferSize, maxPoolSize, threadLocalCacheSize));
|
||||||
|
deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo",
|
||||||
|
webSocketDeploymentInfo);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+4
-4
@@ -29,8 +29,8 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class WebSocketEndpointConfig {
|
public class WebSocketEndpointConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ServerEndpointExporter serverEndpointExporter() {
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
return new ServerEndpointExporter();
|
return new ServerEndpointExporter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-26
@@ -29,35 +29,36 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class KitWsClientManager {
|
public class KitWsClientManager {
|
||||||
/**
|
|
||||||
* concurrent包的线程安全Map, 用来存放每个客户端对应的MyWebSocket对象。(分布式必出问题)
|
|
||||||
*/
|
|
||||||
private static final Map<String, KitWsServer> clientMap = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
public static void addClient(String sid, KitWsServer wsServer) {
|
/**
|
||||||
// 如果存在就先删除一个, 防止重复推送消息
|
* concurrent包的线程安全Map, 用来存放每个客户端对应的MyWebSocket对象。(分布式必出问题)
|
||||||
removeClient(sid);
|
*/
|
||||||
clientMap.put(sid, wsServer);
|
private static final Map<String, KitWsServer> clientMap = new ConcurrentHashMap<>();
|
||||||
}
|
|
||||||
|
|
||||||
public static void removeClient(String sid) {
|
public static void addClient(String sid, KitWsServer wsServer) {
|
||||||
KitWsServer wsServer = clientMap.get(sid);
|
// 如果存在就先删除一个, 防止重复推送消息
|
||||||
if (wsServer != null) {
|
removeClient(sid);
|
||||||
try {
|
clientMap.put(sid, wsServer);
|
||||||
log.info("关闭session, sid={}", sid);
|
}
|
||||||
wsServer.getSession().close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Close session failed, sid={}", sid, e);
|
|
||||||
}
|
|
||||||
clientMap.remove(sid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Collection<KitWsServer> getAllClient() {
|
public static void removeClient(String sid) {
|
||||||
return clientMap.values();
|
KitWsServer wsServer = clientMap.get(sid);
|
||||||
|
if (wsServer != null) {
|
||||||
|
try {
|
||||||
|
log.info("关闭session, sid={}", sid);
|
||||||
|
wsServer.getSession().close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Close session failed, sid={}", sid, e);
|
||||||
|
}
|
||||||
|
clientMap.remove(sid);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static KitWsServer getClientBySid(String sid) {
|
public static Collection<KitWsServer> getAllClient() {
|
||||||
return clientMap.get(sid);
|
return clientMap.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static KitWsServer getClientBySid(String sid) {
|
||||||
|
return clientMap.get(sid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -23,11 +23,12 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class KitWsMessage extends KitObject {
|
public class KitWsMessage extends KitObject {
|
||||||
private String bizCode;
|
|
||||||
private String data;
|
|
||||||
|
|
||||||
public KitWsMessage(String bizCode, String data) {
|
private String bizCode;
|
||||||
this.bizCode = bizCode;
|
private String data;
|
||||||
this.data = data;
|
|
||||||
}
|
public KitWsMessage(String bizCode, String data) {
|
||||||
|
this.bizCode = bizCode;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+101
-100
@@ -35,116 +35,117 @@ import org.springframework.stereotype.Component;
|
|||||||
@ServerEndpoint("/websocket/{sid}")
|
@ServerEndpoint("/websocket/{sid}")
|
||||||
@Getter
|
@Getter
|
||||||
public class KitWsServer {
|
public class KitWsServer {
|
||||||
/**
|
|
||||||
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
|
||||||
*/
|
|
||||||
private Session session;
|
|
||||||
/**
|
|
||||||
* 接收sid
|
|
||||||
*/
|
|
||||||
private String sid = "";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接建立成功调用的方法
|
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
||||||
*/
|
*/
|
||||||
@OnOpen
|
private Session session;
|
||||||
public void onOpen(Session session, @PathParam("sid") String sid) {
|
/**
|
||||||
this.session = session;
|
* 接收sid
|
||||||
// {username}#{bizCode}#{contextParams}
|
*/
|
||||||
this.sid = sid;
|
private String sid = "";
|
||||||
KitWsClientManager.addClient(sid, this);
|
|
||||||
|
/**
|
||||||
|
* 连接建立成功调用的方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session, @PathParam("sid") String sid) {
|
||||||
|
this.session = session;
|
||||||
|
// {username}#{bizCode}#{contextParams}
|
||||||
|
this.sid = sid;
|
||||||
|
KitWsClientManager.addClient(sid, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收到客户端消息后调用的方法
|
||||||
|
*
|
||||||
|
* @param message 客户端发送过来的消息
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, Session session) {
|
||||||
|
KitWsMessage wsMessage = JSON.parseObject(message, KitWsMessage.class);
|
||||||
|
String bizCode = wsMessage.getBizCode();
|
||||||
|
Object data = wsMessage.getData();
|
||||||
|
log.info("收到来 sid={} 的信息: message={}, bizCode={}, data={}", sid, message, bizCode,
|
||||||
|
JSON.toJSONString(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接关闭调用的方法
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose() {
|
||||||
|
try {
|
||||||
|
KitWsClientManager.removeClient(this.sid);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("WebSocket onClose error, sid={}", this.sid, e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@OnError
|
||||||
* 收到客户端消息后调用的方法
|
public void onError(Session session, Throwable error) {
|
||||||
*
|
// log.error("WebSocket sid={} 发生错误", this.sid, error);
|
||||||
* @param message 客户端发送过来的消息
|
try {
|
||||||
*/
|
KitWsClientManager.removeClient(this.sid);
|
||||||
@OnMessage
|
} catch (Exception e) {
|
||||||
public void onMessage(String message, Session session) {
|
log.error("WebSocket onClose error, sid={}", this.sid, e);
|
||||||
KitWsMessage wsMessage = JSON.parseObject(message, KitWsMessage.class);
|
|
||||||
String bizCode = wsMessage.getBizCode();
|
|
||||||
Object data = wsMessage.getData();
|
|
||||||
log.info("收到来 sid={} 的信息: message={}, bizCode={}, data={}", sid, message, bizCode,
|
|
||||||
JSON.toJSONString(data));
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接关闭调用的方法
|
* 实现服务器主动推送
|
||||||
*/
|
*/
|
||||||
@OnClose
|
private void innerSendMessage(String message) throws IOException {
|
||||||
public void onClose() {
|
this.session.getBasicRemote().sendText(message);
|
||||||
try {
|
}
|
||||||
KitWsClientManager.removeClient(this.sid);
|
|
||||||
} catch (Exception e) {
|
/**
|
||||||
log.error("WebSocket onClose error, sid={}", this.sid, e);
|
* 精准推送消息
|
||||||
|
*/
|
||||||
|
public void sendMessage(KitWsMessage message, @PathParam("sid") String sid) {
|
||||||
|
String body = JSON.toJSONString(message);
|
||||||
|
// log.info("推送消息到{}, 推送内容:{}", sid, message);
|
||||||
|
try {
|
||||||
|
if (sid == null) {
|
||||||
|
sendToAll(body);
|
||||||
|
} else {
|
||||||
|
KitWsServer wsClient = KitWsClientManager.getClientBySid(sid);
|
||||||
|
if (wsClient != null) {
|
||||||
|
wsClient.innerSendMessage(body);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@OnError
|
/**
|
||||||
public void onError(Session session, Throwable error) {
|
* 群发消息
|
||||||
// log.error("WebSocket sid={} 发生错误", this.sid, error);
|
*/
|
||||||
try {
|
public void sendToAll(String message) {
|
||||||
KitWsClientManager.removeClient(this.sid);
|
for (KitWsServer item : KitWsClientManager.getAllClient()) {
|
||||||
} catch (Exception e) {
|
try {
|
||||||
log.error("WebSocket onClose error, sid={}", this.sid, e);
|
item.innerSendMessage(message);
|
||||||
}
|
} catch (IOException e) {
|
||||||
|
log.error("发送消息给 sid={} 失败", item.sid, e);
|
||||||
|
KitWsClientManager.removeClient(item.sid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 实现服务器主动推送
|
public boolean equals(Object o) {
|
||||||
*/
|
if (this == o) {
|
||||||
private void innerSendMessage(String message) throws IOException {
|
return true;
|
||||||
this.session.getBasicRemote().sendText(message);
|
|
||||||
}
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
KitWsServer that = (KitWsServer) o;
|
||||||
|
return Objects.equals(session, that.session) && Objects.equals(sid, that.sid);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 精准推送消息
|
public int hashCode() {
|
||||||
*/
|
return Objects.hash(session, sid);
|
||||||
public void sendMessage(KitWsMessage message, @PathParam("sid") String sid) {
|
}
|
||||||
String body = JSON.toJSONString(message);
|
|
||||||
// log.info("推送消息到{}, 推送内容:{}", sid, message);
|
|
||||||
try {
|
|
||||||
if (sid == null) {
|
|
||||||
sendToAll(body);
|
|
||||||
} else {
|
|
||||||
KitWsServer wsClient = KitWsClientManager.getClientBySid(sid);
|
|
||||||
if (wsClient != null) {
|
|
||||||
wsClient.innerSendMessage(body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 群发消息
|
|
||||||
*/
|
|
||||||
public void sendToAll(String message) {
|
|
||||||
for (KitWsServer item : KitWsClientManager.getAllClient()) {
|
|
||||||
try {
|
|
||||||
item.innerSendMessage(message);
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("发送消息给 sid={} 失败", item.sid, e);
|
|
||||||
KitWsClientManager.removeClient(item.sid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (o == null || getClass() != o.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
KitWsServer that = (KitWsServer)o;
|
|
||||||
return Objects.equals(session, that.session) && Objects.equals(sid, that.sid);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(session, sid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -23,7 +23,8 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class WsSidVo extends KitObject {
|
public class WsSidVo extends KitObject {
|
||||||
private String username;
|
|
||||||
private String bizCode;
|
private String username;
|
||||||
private String param;
|
private String bizCode;
|
||||||
|
private String param;
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-11
@@ -28,16 +28,17 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class WsMessageUtil {
|
public class WsMessageUtil {
|
||||||
public static WsSidVo parseSid(String sid) {
|
|
||||||
// sid {username}_{bizCode}_{param}
|
public static WsSidVo parseSid(String sid) {
|
||||||
String[] sids = sid.split("_");
|
// sid {username}_{bizCode}_{param}
|
||||||
if (sids.length != 3) {
|
String[] sids = sid.split("_");
|
||||||
throw new BadRequestException("sid格式异常");
|
if (sids.length != 3) {
|
||||||
}
|
throw new BadRequestException("sid格式异常");
|
||||||
WsSidVo sidVo = new WsSidVo();
|
|
||||||
sidVo.setUsername(sids[0]);
|
|
||||||
sidVo.setBizCode(sids[1]);
|
|
||||||
sidVo.setParam(sids[2]);
|
|
||||||
return sidVo;
|
|
||||||
}
|
}
|
||||||
|
WsSidVo sidVo = new WsSidVo();
|
||||||
|
sidVo.setUsername(sids[0]);
|
||||||
|
sidVo.setBizCode(sids[1]);
|
||||||
|
sidVo.setParam(sids[2]);
|
||||||
|
return sidVo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-58
@@ -40,66 +40,66 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class KitAnonTagUtil {
|
public class KitAnonTagUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取匿名标记的URL
|
* 获取匿名标记的URL
|
||||||
*
|
*
|
||||||
* @param applicationContext /
|
* @param applicationContext /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static Map<String, Set<String>> getAnonymousUrl(final ApplicationContext applicationContext) {
|
public static Map<String, Set<String>> getAnonymousUrl(final ApplicationContext applicationContext) {
|
||||||
RequestMappingHandlerMapping requestMappingHandlerMapping =
|
RequestMappingHandlerMapping requestMappingHandlerMapping =
|
||||||
applicationContext.getBean(RequestMappingHandlerMapping.class);
|
applicationContext.getBean(RequestMappingHandlerMapping.class);
|
||||||
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
|
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
|
||||||
Map<String, java.util.Set<String>> anonymousUrls = new HashMap<>(8);
|
Map<String, java.util.Set<String>> anonymousUrls = new HashMap<>(8);
|
||||||
// 获取匿名标记
|
// 获取匿名标记
|
||||||
Set<String> get = new HashSet<>();
|
Set<String> get = new HashSet<>();
|
||||||
Set<String> post = new HashSet<>();
|
Set<String> post = new HashSet<>();
|
||||||
Set<String> put = new HashSet<>();
|
Set<String> put = new HashSet<>();
|
||||||
Set<String> patch = new HashSet<>();
|
Set<String> patch = new HashSet<>();
|
||||||
Set<String> delete = new HashSet<>();
|
Set<String> delete = new HashSet<>();
|
||||||
Set<String> all = new HashSet<>();
|
Set<String> all = new HashSet<>();
|
||||||
for (Map.Entry<RequestMappingInfo, HandlerMethod> infoEntry : handlerMethodMap.entrySet()) {
|
for (Map.Entry<RequestMappingInfo, HandlerMethod> infoEntry : handlerMethodMap.entrySet()) {
|
||||||
HandlerMethod handlerMethod = infoEntry.getValue();
|
HandlerMethod handlerMethod = infoEntry.getValue();
|
||||||
AnonymousAccess anonymousAccess = handlerMethod.getMethodAnnotation(AnonymousAccess.class);
|
AnonymousAccess anonymousAccess = handlerMethod.getMethodAnnotation(AnonymousAccess.class);
|
||||||
if (null != anonymousAccess && infoEntry.getKey() != null) {
|
if (null != anonymousAccess && infoEntry.getKey() != null) {
|
||||||
List<RequestMethod> requestMethods =
|
List<RequestMethod> requestMethods =
|
||||||
new ArrayList<>(infoEntry.getKey().getMethodsCondition().getMethods());
|
new ArrayList<>(infoEntry.getKey().getMethodsCondition().getMethods());
|
||||||
RequestMethodEnum request = RequestMethodEnum.find(
|
RequestMethodEnum request = RequestMethodEnum.find(
|
||||||
requestMethods.isEmpty() ? RequestMethodEnum.ALL.getType() : requestMethods.get(0).name());
|
requestMethods.isEmpty() ? RequestMethodEnum.ALL.getType() : requestMethods.get(0).name());
|
||||||
PathPatternsRequestCondition pathPatternsCondition = infoEntry.getKey().getPathPatternsCondition();
|
PathPatternsRequestCondition pathPatternsCondition = infoEntry.getKey().getPathPatternsCondition();
|
||||||
if (pathPatternsCondition != null) {
|
if (pathPatternsCondition != null) {
|
||||||
switch (Objects.requireNonNull(request)) {
|
switch (Objects.requireNonNull(request)) {
|
||||||
case GET -> get.addAll(pathPatternsCondition.getPatternValues());
|
case GET -> get.addAll(pathPatternsCondition.getPatternValues());
|
||||||
case POST -> post.addAll(pathPatternsCondition.getPatternValues());
|
case POST -> post.addAll(pathPatternsCondition.getPatternValues());
|
||||||
case PUT -> put.addAll(pathPatternsCondition.getPatternValues());
|
case PUT -> put.addAll(pathPatternsCondition.getPatternValues());
|
||||||
case PATCH -> patch.addAll(pathPatternsCondition.getPatternValues());
|
case PATCH -> patch.addAll(pathPatternsCondition.getPatternValues());
|
||||||
case DELETE -> delete.addAll(pathPatternsCondition.getPatternValues());
|
case DELETE -> delete.addAll(pathPatternsCondition.getPatternValues());
|
||||||
default -> all.addAll(pathPatternsCondition.getPatternValues());
|
default -> all.addAll(pathPatternsCondition.getPatternValues());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
anonymousUrls.put(RequestMethodEnum.GET.getType(), get);
|
}
|
||||||
anonymousUrls.put(RequestMethodEnum.POST.getType(), post);
|
|
||||||
anonymousUrls.put(RequestMethodEnum.PUT.getType(), put);
|
|
||||||
anonymousUrls.put(RequestMethodEnum.PATCH.getType(), patch);
|
|
||||||
anonymousUrls.put(RequestMethodEnum.DELETE.getType(), delete);
|
|
||||||
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
|
|
||||||
return anonymousUrls;
|
|
||||||
}
|
}
|
||||||
|
anonymousUrls.put(RequestMethodEnum.GET.getType(), get);
|
||||||
|
anonymousUrls.put(RequestMethodEnum.POST.getType(), post);
|
||||||
|
anonymousUrls.put(RequestMethodEnum.PUT.getType(), put);
|
||||||
|
anonymousUrls.put(RequestMethodEnum.PATCH.getType(), patch);
|
||||||
|
anonymousUrls.put(RequestMethodEnum.DELETE.getType(), delete);
|
||||||
|
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
|
||||||
|
return anonymousUrls;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有匿名标记的URL
|
* 获取所有匿名标记的URL
|
||||||
*
|
*
|
||||||
* @param applicationContext /
|
* @param applicationContext /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static Set<String> getAllAnonymousUrl(final ApplicationContext applicationContext) {
|
public static Set<String> getAllAnonymousUrl(final ApplicationContext applicationContext) {
|
||||||
Set<String> allUrl = new HashSet<>();
|
Set<String> allUrl = new HashSet<>();
|
||||||
Map<String, Set<String>> anonymousUrls = getAnonymousUrl(applicationContext);
|
Map<String, Set<String>> anonymousUrls = getAnonymousUrl(applicationContext);
|
||||||
for (Map.Entry<String, Set<String>> entry : anonymousUrls.entrySet()) {
|
for (Map.Entry<String, Set<String>> entry : anonymousUrls.entrySet()) {
|
||||||
allUrl.addAll(anonymousUrls.get(entry.getKey()));
|
allUrl.addAll(anonymousUrls.get(entry.getKey()));
|
||||||
}
|
|
||||||
return allUrl;
|
|
||||||
}
|
}
|
||||||
|
return allUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -30,20 +30,20 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitBase64SecretUtil {
|
public final class KitBase64SecretUtil {
|
||||||
|
|
||||||
public static String build() {
|
public static String build() {
|
||||||
// 128位uuid
|
// 128位uuid
|
||||||
return Base64Encoder.encode(getLongUuid(4).getBytes(StandardCharsets.UTF_8));
|
return Base64Encoder.encode(getLongUuid(4).getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getLongUuid(int bit) {
|
public static String getLongUuid(int bit) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
for (int i = 0; i < bit; i++) {
|
for (int i = 0; i < bit; i++) {
|
||||||
result.append(IdUtil.simpleUUID());
|
result.append(IdUtil.simpleUUID());
|
||||||
}
|
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(build());
|
System.out.println(build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+111
-111
@@ -27,127 +27,127 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitBigDecimalUtil {
|
public final class KitBigDecimalUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象转换为 BigDecimal
|
* 将对象转换为 BigDecimal
|
||||||
*
|
*
|
||||||
* @param obj 输入对象
|
* @param obj 输入对象
|
||||||
* @return 转换后的 BigDecimal
|
* @return 转换后的 BigDecimal
|
||||||
*/
|
*/
|
||||||
private static BigDecimal toBigDecimal(Object obj) {
|
private static BigDecimal toBigDecimal(Object obj) {
|
||||||
if (obj instanceof BigDecimal b) {
|
if (obj instanceof BigDecimal b) {
|
||||||
return b;
|
return b;
|
||||||
} else if (obj instanceof Long l) {
|
} else if (obj instanceof Long l) {
|
||||||
return BigDecimal.valueOf(l);
|
return BigDecimal.valueOf(l);
|
||||||
} else if (obj instanceof Integer i) {
|
} else if (obj instanceof Integer i) {
|
||||||
return BigDecimal.valueOf(i);
|
return BigDecimal.valueOf(i);
|
||||||
} else if (obj instanceof Double d) {
|
} else if (obj instanceof Double d) {
|
||||||
return new BigDecimal(d);
|
return new BigDecimal(d);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unsupported type");
|
throw new IllegalArgumentException("Unsupported type");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加法
|
* 加法
|
||||||
*
|
*
|
||||||
* @param a 加数
|
* @param a 加数
|
||||||
* @param b 加数
|
* @param b 加数
|
||||||
* @return 两个加数的和, 保留两位小数
|
* @return 两个加数的和, 保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal add(Object a, Object b) {
|
public static BigDecimal add(Object a, Object b) {
|
||||||
BigDecimal bdA = toBigDecimal(a);
|
BigDecimal bdA = toBigDecimal(a);
|
||||||
BigDecimal bdB = toBigDecimal(b);
|
BigDecimal bdB = toBigDecimal(b);
|
||||||
return bdA.add(bdB).setScale(2, RoundingMode.HALF_UP);
|
return bdA.add(bdB).setScale(2, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 减法
|
* 减法
|
||||||
*
|
*
|
||||||
* @param a 被减数
|
* @param a 被减数
|
||||||
* @param b 减数
|
* @param b 减数
|
||||||
* @return 两数的差, 保留两位小数
|
* @return 两数的差, 保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal subtract(Object a, Object b) {
|
public static BigDecimal subtract(Object a, Object b) {
|
||||||
BigDecimal bdA = toBigDecimal(a);
|
BigDecimal bdA = toBigDecimal(a);
|
||||||
BigDecimal bdB = toBigDecimal(b);
|
BigDecimal bdB = toBigDecimal(b);
|
||||||
return bdA.subtract(bdB).setScale(2, RoundingMode.HALF_UP);
|
return bdA.subtract(bdB).setScale(2, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 乘法
|
* 乘法
|
||||||
*
|
*
|
||||||
* @param a 乘数
|
* @param a 乘数
|
||||||
* @param b 乘数
|
* @param b 乘数
|
||||||
* @return 两个乘数的积, 保留两位小数
|
* @return 两个乘数的积, 保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal multiply(Object a, Object b) {
|
public static BigDecimal multiply(Object a, Object b) {
|
||||||
BigDecimal bdA = toBigDecimal(a);
|
BigDecimal bdA = toBigDecimal(a);
|
||||||
BigDecimal bdB = toBigDecimal(b);
|
BigDecimal bdB = toBigDecimal(b);
|
||||||
return bdA.multiply(bdB).setScale(2, RoundingMode.HALF_UP);
|
return bdA.multiply(bdB).setScale(2, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 除法
|
* 除法
|
||||||
*
|
*
|
||||||
* @param a 被除数
|
* @param a 被除数
|
||||||
* @param b 除数
|
* @param b 除数
|
||||||
* @return 两数的商,保留两位小数
|
* @return 两数的商,保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal divide(Object a, Object b) {
|
public static BigDecimal divide(Object a, Object b) {
|
||||||
BigDecimal bdA = toBigDecimal(a);
|
BigDecimal bdA = toBigDecimal(a);
|
||||||
BigDecimal bdB = toBigDecimal(b);
|
BigDecimal bdB = toBigDecimal(b);
|
||||||
return bdA.divide(bdB, 2, RoundingMode.HALF_UP);
|
return bdA.divide(bdB, 2, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 除法
|
* 除法
|
||||||
*
|
*
|
||||||
* @param a 被除数
|
* @param a 被除数
|
||||||
* @param b 除数
|
* @param b 除数
|
||||||
* @param scale 保留小数位数
|
* @param scale 保留小数位数
|
||||||
* @return 两数的商,保留两位小数
|
* @return 两数的商,保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal divide(Object a, Object b, int scale) {
|
public static BigDecimal divide(Object a, Object b, int scale) {
|
||||||
BigDecimal bdA = toBigDecimal(a);
|
BigDecimal bdA = toBigDecimal(a);
|
||||||
BigDecimal bdB = toBigDecimal(b);
|
BigDecimal bdB = toBigDecimal(b);
|
||||||
return bdA.divide(bdB, scale, RoundingMode.HALF_UP);
|
return bdA.divide(bdB, scale, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分转元
|
* 分转元
|
||||||
*
|
*
|
||||||
* @param obj 分的金额
|
* @param obj 分的金额
|
||||||
* @return 转换后的元,保留两位小数
|
* @return 转换后的元,保留两位小数
|
||||||
*/
|
*/
|
||||||
public static BigDecimal centsToYuan(Object obj) {
|
public static BigDecimal centsToYuan(Object obj) {
|
||||||
BigDecimal cents = toBigDecimal(obj);
|
BigDecimal cents = toBigDecimal(obj);
|
||||||
return cents.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
|
return cents.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 元转分
|
* 元转分
|
||||||
*
|
*
|
||||||
* @param obj 元的金额
|
* @param obj 元的金额
|
||||||
* @return 转换后的分
|
* @return 转换后的分
|
||||||
*/
|
*/
|
||||||
public static Long yuanToCents(Object obj) {
|
public static Long yuanToCents(Object obj) {
|
||||||
BigDecimal yuan = toBigDecimal(obj);
|
BigDecimal yuan = toBigDecimal(obj);
|
||||||
return yuan.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_UP).longValue();
|
return yuan.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_UP).longValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
BigDecimal num1 = new BigDecimal("10.123");
|
BigDecimal num1 = new BigDecimal("10.123");
|
||||||
BigDecimal num2 = new BigDecimal("2.456");
|
BigDecimal num2 = new BigDecimal("2.456");
|
||||||
|
|
||||||
System.out.println("加法结果: " + add(num1, num2));
|
System.out.println("加法结果: " + add(num1, num2));
|
||||||
System.out.println("减法结果: " + subtract(num1, num2));
|
System.out.println("减法结果: " + subtract(num1, num2));
|
||||||
System.out.println("乘法结果: " + multiply(num1, num2));
|
System.out.println("乘法结果: " + multiply(num1, num2));
|
||||||
System.out.println("除法结果: " + divide(num1, num2));
|
System.out.println("除法结果: " + divide(num1, num2));
|
||||||
|
|
||||||
Long cents = 12345L;
|
Long cents = 12345L;
|
||||||
System.out.println("分转元结果: " + centsToYuan(cents));
|
System.out.println("分转元结果: " + centsToYuan(cents));
|
||||||
|
|
||||||
BigDecimal yuan = new BigDecimal("123.45");
|
BigDecimal yuan = new BigDecimal("123.45");
|
||||||
System.out.println("元转分结果: " + yuanToCents(yuan));
|
System.out.println("元转分结果: " + yuanToCents(yuan));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-37
@@ -30,44 +30,45 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitBrowserUtil {
|
public final class KitBrowserUtil {
|
||||||
private static final String UNKNOWN = "unknown";
|
|
||||||
|
|
||||||
/**
|
private static final String UNKNOWN = "unknown";
|
||||||
* 获取ip地址
|
|
||||||
*/
|
|
||||||
public static String getIp(HttpServletRequest request) {
|
|
||||||
String ip = request.getHeader("x-forwarded-for");
|
|
||||||
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
||||||
ip = request.getHeader("Proxy-Client-IP");
|
|
||||||
}
|
|
||||||
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
||||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
|
||||||
}
|
|
||||||
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
|
||||||
ip = request.getRemoteAddr();
|
|
||||||
}
|
|
||||||
String comma = ",";
|
|
||||||
String localhost = "127.0.0.1";
|
|
||||||
if (ip.contains(comma)) {
|
|
||||||
ip = ip.split(",")[0];
|
|
||||||
}
|
|
||||||
if (localhost.equals(ip)) {
|
|
||||||
// 获取本机真正的ip地址
|
|
||||||
try {
|
|
||||||
ip = InetAddress.getLocalHost().getHostAddress();
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
log.error("获取本机地址失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取浏览器
|
* 获取ip地址
|
||||||
*/
|
*/
|
||||||
public static String getVersion(HttpServletRequest request) {
|
public static String getIp(HttpServletRequest request) {
|
||||||
UserAgent ua = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
String ip = request.getHeader("x-forwarded-for");
|
||||||
String browser = ua.getBrowser().toString() + " " + ua.getVersion();
|
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
||||||
return browser.replace(".0.0.0", "");
|
ip = request.getHeader("Proxy-Client-IP");
|
||||||
}
|
}
|
||||||
|
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||||
|
}
|
||||||
|
if (ip == null || ip.isEmpty() || UNKNOWN.equalsIgnoreCase(ip)) {
|
||||||
|
ip = request.getRemoteAddr();
|
||||||
|
}
|
||||||
|
String comma = ",";
|
||||||
|
String localhost = "127.0.0.1";
|
||||||
|
if (ip.contains(comma)) {
|
||||||
|
ip = ip.split(",")[0];
|
||||||
|
}
|
||||||
|
if (localhost.equals(ip)) {
|
||||||
|
// 获取本机真正的ip地址
|
||||||
|
try {
|
||||||
|
ip = InetAddress.getLocalHost().getHostAddress();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
log.error("获取本机地址失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取浏览器
|
||||||
|
*/
|
||||||
|
public static String getVersion(HttpServletRequest request) {
|
||||||
|
UserAgent ua = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
||||||
|
String browser = ua.getBrowser().toString() + " " + ua.getVersion();
|
||||||
|
return browser.replace(".0.0.0", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-7
@@ -27,12 +27,13 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitClassUtil {
|
public final class KitClassUtil {
|
||||||
@SuppressWarnings({"unchecked", "all"})
|
|
||||||
public static List<Field> getAllFields(Class clazz, List<Field> fields) {
|
@SuppressWarnings({"unchecked", "all"})
|
||||||
if (clazz != null) {
|
public static List<Field> getAllFields(Class clazz, List<Field> fields) {
|
||||||
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
if (clazz != null) {
|
||||||
getAllFields(clazz.getSuperclass(), fields);
|
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
||||||
}
|
getAllFields(clazz.getSuperclass(), fields);
|
||||||
return fields;
|
|
||||||
}
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-16
@@ -26,23 +26,23 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitCloseUtil {
|
public final class KitCloseUtil {
|
||||||
|
|
||||||
public static void close(Closeable closeable) {
|
public static void close(Closeable closeable) {
|
||||||
if (null != closeable) {
|
if (null != closeable) {
|
||||||
try {
|
try {
|
||||||
closeable.close();
|
closeable.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 静默关闭
|
// 静默关闭
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void close(AutoCloseable closeable) {
|
public static void close(AutoCloseable closeable) {
|
||||||
if (null != closeable) {
|
if (null != closeable) {
|
||||||
try {
|
try {
|
||||||
closeable.close();
|
closeable.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 静默关闭
|
// 静默关闭
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+167
-166
@@ -40,183 +40,184 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitCollUtil extends cn.hutool.core.collection.CollUtil {
|
public final class KitCollUtil extends cn.hutool.core.collection.CollUtil {
|
||||||
private static <T, K> Predicate<T> distinctPredicate(Function<? super T, ? extends K> keyMapper) {
|
|
||||||
Map<Object, Boolean> map = new HashMap<>(3);
|
|
||||||
return (t) -> null == map.putIfAbsent(keyMapper.apply(t), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private static <T, K> Predicate<T> distinctPredicate(Function<? super T, ? extends K> keyMapper) {
|
||||||
* 流根据对象属性去重
|
Map<Object, Boolean> map = new HashMap<>(3);
|
||||||
* <p>
|
return (t) -> null == map.putIfAbsent(keyMapper.apply(t), true);
|
||||||
* 使用方式: list = CollUtil.streamDistinct(list, Object::getXXX)
|
}
|
||||||
*/
|
|
||||||
public static <T, K> List<T> streamDistinct(List<T> data, Function<? super T, ? extends K> keyMapper) {
|
|
||||||
return data.stream().filter(distinctPredicate(keyMapper)).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流根据对象属性自然排序
|
* 流根据对象属性去重
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: list = CollUtil.streamAscSort(list, Object::getXXX)
|
* 使用方式: list = CollUtil.streamDistinct(list, Object::getXXX)
|
||||||
*
|
*/
|
||||||
* @param data /
|
public static <T, K> List<T> streamDistinct(List<T> data, Function<? super T, ? extends K> keyMapper) {
|
||||||
* @param keyExtractor /
|
return data.stream().filter(distinctPredicate(keyMapper)).toList();
|
||||||
* @param <T> /
|
}
|
||||||
* @param <U> /
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
public static <T, U extends Comparable<? super U>> List<T> streamAscSort(List<T> data,
|
|
||||||
Function<? super T, ? extends U> keyExtractor) {
|
|
||||||
return data.stream().sorted(Comparator.comparing(keyExtractor)).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流根据对象属性倒转排序
|
* 流根据对象属性自然排序
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: list = CollUtil.streamDescSort(list, Object::getXXX)
|
* 使用方式: list = CollUtil.streamAscSort(list, Object::getXXX)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @param keyExtractor /
|
* @param keyExtractor /
|
||||||
* @param <T> /
|
* @param <T> /
|
||||||
* @param <U> /
|
* @param <U> /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static <T, U extends Comparable<? super U>> List<T> streamDescSort(List<T> data,
|
public static <T, U extends Comparable<? super U>> List<T> streamAscSort(List<T> data,
|
||||||
Function<? super T, ? extends U> keyExtractor) {
|
Function<? super T, ? extends U> keyExtractor) {
|
||||||
return data.stream().sorted(Comparator.comparing(keyExtractor).reversed()).toList();
|
return data.stream().sorted(Comparator.comparing(keyExtractor)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流拆分对象为键值对
|
* 流根据对象属性倒转排序
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: map = CollUtil.streamToMap(list, Object::getXXKey, Object::getXXValue)
|
* 使用方式: list = CollUtil.streamDescSort(list, Object::getXXX)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @param keyMapper /
|
* @param keyExtractor /
|
||||||
* @param valueMapper /
|
* @param <T> /
|
||||||
* @param <T> /
|
* @param <U> /
|
||||||
* @param <K> /
|
* @return /
|
||||||
* @param <U> /
|
*/
|
||||||
* @return /
|
public static <T, U extends Comparable<? super U>> List<T> streamDescSort(List<T> data,
|
||||||
*/
|
Function<? super T, ? extends U> keyExtractor) {
|
||||||
public static <T, K, U> Map<K, U> streamToMap(List<T> data, Function<? super T, ? extends K> keyMapper,
|
return data.stream().sorted(Comparator.comparing(keyExtractor).reversed()).toList();
|
||||||
Function<? super T, ? extends U> valueMapper) {
|
}
|
||||||
// 根据key去重再并流
|
|
||||||
return streamDistinct(data, keyMapper).stream().collect(Collectors.toMap(keyMapper, valueMapper));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<Double>求和
|
* 流拆分对象为键值对
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: total = CollUtil.streamDoubleAdd(doubleList)
|
* 使用方式: map = CollUtil.streamToMap(list, Object::getXXKey, Object::getXXValue)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @param keyMapper /
|
||||||
*/
|
* @param valueMapper /
|
||||||
public static Double streamDoubleAdd(List<Double> data) {
|
* @param <T> /
|
||||||
if (KitCollUtil.isNotEmpty(data)) {
|
* @param <K> /
|
||||||
return data.stream().filter(Objects::nonNull).reduce(Double::sum).orElse(0.0);
|
* @param <U> /
|
||||||
}
|
* @return /
|
||||||
return 0.0;
|
*/
|
||||||
}
|
public static <T, K, U> Map<K, U> streamToMap(List<T> data, Function<? super T, ? extends K> keyMapper,
|
||||||
|
Function<? super T, ? extends U> valueMapper) {
|
||||||
|
// 根据key去重再并流
|
||||||
|
return streamDistinct(data, keyMapper).stream().collect(Collectors.toMap(keyMapper, valueMapper));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<BigDecimal>求和
|
* 流对List<Double>求和
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: total = CollUtil.streamBigDecimalAdd(doubleList)
|
* 使用方式: total = CollUtil.streamDoubleAdd(doubleList)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static BigDecimal streamBigDecimalAdd(List<BigDecimal> data) {
|
public static Double streamDoubleAdd(List<Double> data) {
|
||||||
if (KitCollUtil.isNotEmpty(data)) {
|
if (KitCollUtil.isNotEmpty(data)) {
|
||||||
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
return data.stream().filter(Objects::nonNull).reduce(Double::sum).orElse(0.0);
|
||||||
}
|
|
||||||
return BigDecimal.ZERO;
|
|
||||||
}
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<Object>中的某一BigDecimal属性求和
|
* 流对List<BigDecimal>求和
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: total = CollUtil.streamBigDecimalAdd(list, XXX)
|
* 使用方式: total = CollUtil.streamBigDecimalAdd(doubleList)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static <T> BigDecimal streamBigDecimalAdd(List<T> data, String elementName) {
|
public static BigDecimal streamBigDecimalAdd(List<BigDecimal> data) {
|
||||||
return data.stream().filter(Objects::nonNull).map(m -> {
|
if (KitCollUtil.isNotEmpty(data)) {
|
||||||
// 反射, 取集合中各元素的某属性
|
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
||||||
BigDecimal that;
|
|
||||||
try {
|
|
||||||
Field field = m.getClass().getDeclaredField(elementName);
|
|
||||||
field.setAccessible(true);
|
|
||||||
that = (BigDecimal)field.get(m);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 忽略异常
|
|
||||||
that = BigDecimal.ZERO;
|
|
||||||
}
|
|
||||||
return that;
|
|
||||||
}).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
|
||||||
}
|
}
|
||||||
|
return BigDecimal.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<BigDecimal>取最大值
|
* 流对List<Object>中的某一BigDecimal属性求和
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: max = CollUtil.streamBigDecimalMax(list)
|
* 使用方式: total = CollUtil.streamBigDecimalAdd(list, XXX)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static BigDecimal streamBigDecimalMax(List<BigDecimal> data) {
|
public static <T> BigDecimal streamBigDecimalAdd(List<T> data, String elementName) {
|
||||||
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::max).orElse(null);
|
return data.stream().filter(Objects::nonNull).map(m -> {
|
||||||
}
|
// 反射, 取集合中各元素的某属性
|
||||||
|
BigDecimal that;
|
||||||
|
try {
|
||||||
|
Field field = m.getClass().getDeclaredField(elementName);
|
||||||
|
field.setAccessible(true);
|
||||||
|
that = (BigDecimal) field.get(m);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 忽略异常
|
||||||
|
that = BigDecimal.ZERO;
|
||||||
|
}
|
||||||
|
return that;
|
||||||
|
}).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<BigDecimal>取最小值
|
* 流对List<BigDecimal>取最大值
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: max = CollUtil.streamBigDecimalMin(list)
|
* 使用方式: max = CollUtil.streamBigDecimalMax(list)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static BigDecimal streamBigDecimalMin(List<BigDecimal> data) {
|
public static BigDecimal streamBigDecimalMax(List<BigDecimal> data) {
|
||||||
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::min).orElse(null);
|
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::max).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流对List<BigDecimal>取平均值
|
* 流对List<BigDecimal>取最小值
|
||||||
* <p>
|
* <p>
|
||||||
* 使用方式: avg = CollUtil.streamBigDecimalAvg(list)
|
* 使用方式: max = CollUtil.streamBigDecimalMin(list)
|
||||||
*
|
*
|
||||||
* @param data /
|
* @param data /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static BigDecimal streamBigDecimalAvg(List<BigDecimal> data) {
|
public static BigDecimal streamBigDecimalMin(List<BigDecimal> data) {
|
||||||
return data.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)
|
return data.stream().filter(Objects::nonNull).reduce(BigDecimal::min).orElse(null);
|
||||||
.divide(BigDecimal.valueOf(data.size()), 6, RoundingMode.FLOOR);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
List<KitSelectOptionVo> testData = new ArrayList<>();
|
* 流对List<BigDecimal>取平均值
|
||||||
KitSelectOptionVo selectOption1 = KitSelectOptionVo.builder().label("苹果").value("apple").build();
|
* <p>
|
||||||
KitSelectOptionVo selectOption2 = KitSelectOptionVo.builder().label("苹果").value("apple").build();
|
* 使用方式: avg = CollUtil.streamBigDecimalAvg(list)
|
||||||
KitSelectOptionVo selectOption3 = KitSelectOptionVo.builder().label("香蕉").value("banana").build();
|
*
|
||||||
KitSelectOptionVo selectOption4 = KitSelectOptionVo.builder().label("猫").value("cat").build();
|
* @param data /
|
||||||
testData.add(selectOption1);
|
* @return /
|
||||||
testData.add(selectOption2);
|
*/
|
||||||
testData.add(selectOption3);
|
public static BigDecimal streamBigDecimalAvg(List<BigDecimal> data) {
|
||||||
testData.add(selectOption4);
|
return data.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||||
List<KitSelectOptionVo> selectOptions1 = KitCollUtil.streamAscSort(testData, KitSelectOptionVo::getValue);
|
.divide(BigDecimal.valueOf(data.size()), 6, RoundingMode.FLOOR);
|
||||||
System.out.println(JSON.toJSONString(selectOptions1));
|
}
|
||||||
List<KitSelectOptionVo> selectOptions2 = KitCollUtil.streamDescSort(testData, KitSelectOptionVo::getValue);
|
|
||||||
System.out.println(JSON.toJSONString(selectOptions2));
|
public static void main(String[] args) {
|
||||||
List<KitSelectOptionVo> selectOptions3 = KitCollUtil.streamDistinct(testData, KitSelectOptionVo::getValue);
|
List<KitSelectOptionVo> testData = new ArrayList<>();
|
||||||
System.out.println(JSON.toJSONString(selectOptions3));
|
KitSelectOptionVo selectOption1 = KitSelectOptionVo.builder().label("苹果").value("apple").build();
|
||||||
Map<String, String> selectMap1 =
|
KitSelectOptionVo selectOption2 = KitSelectOptionVo.builder().label("苹果").value("apple").build();
|
||||||
KitCollUtil.streamToMap(testData, KitSelectOptionVo::getValue, KitSelectOptionVo::getLabel);
|
KitSelectOptionVo selectOption3 = KitSelectOptionVo.builder().label("香蕉").value("banana").build();
|
||||||
System.out.println(JSON.toJSONString(selectMap1));
|
KitSelectOptionVo selectOption4 = KitSelectOptionVo.builder().label("猫").value("cat").build();
|
||||||
// 这只是一个示范
|
testData.add(selectOption1);
|
||||||
BigDecimal total1 = KitCollUtil.streamBigDecimalAdd(testData, "value");
|
testData.add(selectOption2);
|
||||||
System.out.println(total1);
|
testData.add(selectOption3);
|
||||||
}
|
testData.add(selectOption4);
|
||||||
|
List<KitSelectOptionVo> selectOptions1 = KitCollUtil.streamAscSort(testData, KitSelectOptionVo::getValue);
|
||||||
|
System.out.println(JSON.toJSONString(selectOptions1));
|
||||||
|
List<KitSelectOptionVo> selectOptions2 = KitCollUtil.streamDescSort(testData, KitSelectOptionVo::getValue);
|
||||||
|
System.out.println(JSON.toJSONString(selectOptions2));
|
||||||
|
List<KitSelectOptionVo> selectOptions3 = KitCollUtil.streamDistinct(testData, KitSelectOptionVo::getValue);
|
||||||
|
System.out.println(JSON.toJSONString(selectOptions3));
|
||||||
|
Map<String, String> selectMap1 =
|
||||||
|
KitCollUtil.streamToMap(testData, KitSelectOptionVo::getValue, KitSelectOptionVo::getLabel);
|
||||||
|
System.out.println(JSON.toJSONString(selectMap1));
|
||||||
|
// 这只是一个示范
|
||||||
|
BigDecimal total1 = KitCollUtil.streamBigDecimalAdd(testData, "value");
|
||||||
|
System.out.println(total1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+109
-108
@@ -29,121 +29,122 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class KitDateUtil {
|
public class KitDateUtil {
|
||||||
/**
|
|
||||||
* 获取当前时间毫秒数
|
/**
|
||||||
*
|
* 获取当前时间毫秒数
|
||||||
* @return /
|
*
|
||||||
*/
|
* @return /
|
||||||
public static String getNowDateTimeMsStr() {
|
*/
|
||||||
return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
|
public static String getNowDateTimeMsStr() {
|
||||||
|
return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前时间,但不包括毫秒数
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static String getNowDateTimeStr() {
|
||||||
|
return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前时间,但不包括毫秒数
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static String getNowDateStr() {
|
||||||
|
return DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化到毫秒
|
||||||
|
*
|
||||||
|
* @param startTime 开始时间
|
||||||
|
* @param endTime 结束时间
|
||||||
|
* @return 格式化后的时间,X天X时X分X秒X毫秒
|
||||||
|
*/
|
||||||
|
public static String formatMillsDuration(Date startTime, Date endTime) {
|
||||||
|
if (startTime == null || endTime == null) {
|
||||||
|
// 什么都不返回
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
long betweenSeconds = DateUtil.between(startTime, endTime, cn.hutool.core.date.DateUnit.SECOND);
|
||||||
* 获取当前时间,但不包括毫秒数
|
|
||||||
*
|
long days = betweenSeconds / (24 * 60 * 60);
|
||||||
* @return /
|
long hours = (betweenSeconds % (24 * 60 * 60)) / (60 * 60);
|
||||||
*/
|
long minutes = (betweenSeconds % (60 * 60)) / 60;
|
||||||
public static String getNowDateTimeStr() {
|
long seconds = betweenSeconds % 60;
|
||||||
return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
|
if (days > 0) {
|
||||||
|
result.append(days).append("天");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (hours > 0) {
|
||||||
* 获取当前时间,但不包括毫秒数
|
result.append(hours).append("时");
|
||||||
*
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
public static String getNowDateStr() {
|
|
||||||
return DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (minutes > 0) {
|
||||||
* 格式化到毫秒
|
result.append(minutes).append("分");
|
||||||
*
|
|
||||||
* @param startTime 开始时间
|
|
||||||
* @param endTime 结束时间
|
|
||||||
* @return 格式化后的时间,X天X时X分X秒X毫秒
|
|
||||||
*/
|
|
||||||
public static String formatMillsDuration(Date startTime, Date endTime) {
|
|
||||||
if (startTime == null || endTime == null) {
|
|
||||||
// 什么都不返回
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
long betweenSeconds = DateUtil.between(startTime, endTime, cn.hutool.core.date.DateUnit.SECOND);
|
|
||||||
|
|
||||||
long days = betweenSeconds / (24 * 60 * 60);
|
|
||||||
long hours = (betweenSeconds % (24 * 60 * 60)) / (60 * 60);
|
|
||||||
long minutes = (betweenSeconds % (60 * 60)) / 60;
|
|
||||||
long seconds = betweenSeconds % 60;
|
|
||||||
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
|
|
||||||
if (days > 0) {
|
|
||||||
result.append(days).append("天");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hours > 0) {
|
|
||||||
result.append(hours).append("时");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minutes > 0) {
|
|
||||||
result.append(minutes).append("分");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seconds > 0 || result.isEmpty()) {
|
|
||||||
result.append(seconds).append("秒");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (seconds > 0 || result.isEmpty()) {
|
||||||
* 格式化到秒
|
result.append(seconds).append("秒");
|
||||||
*
|
|
||||||
* @param startTime 开始时间
|
|
||||||
* @param endTime 结束时间
|
|
||||||
* @return 格式化后的时间,X天X时X分X秒
|
|
||||||
*/
|
|
||||||
public static String formatSecondsDuration(Date startTime, Date endTime) {
|
|
||||||
if (startTime == null || endTime == null) {
|
|
||||||
// 什么都不返回
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
long betweenMs = DateUtil.betweenMs(startTime, endTime);
|
|
||||||
long days = betweenMs / (24 * 60 * 60 * 1000);
|
|
||||||
long hours = (betweenMs % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000);
|
|
||||||
long minutes = (betweenMs % (60 * 60 * 1000)) / (60 * 1000);
|
|
||||||
long seconds = (betweenMs % (60 * 1000)) / 1000;
|
|
||||||
long milliseconds = betweenMs % 1000;
|
|
||||||
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
|
|
||||||
if (days > 0) {
|
|
||||||
result.append(days).append("天");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hours > 0) {
|
|
||||||
result.append(hours).append("时");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minutes > 0) {
|
|
||||||
result.append(minutes).append("分");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seconds > 0) {
|
|
||||||
result.append(seconds).append("秒");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (milliseconds > 0) {
|
|
||||||
result.append(milliseconds).append("毫秒");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果所有单位都是0,则显示0毫秒
|
|
||||||
if (result.isEmpty()) {
|
|
||||||
result.append("0毫秒");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化到秒
|
||||||
|
*
|
||||||
|
* @param startTime 开始时间
|
||||||
|
* @param endTime 结束时间
|
||||||
|
* @return 格式化后的时间,X天X时X分X秒
|
||||||
|
*/
|
||||||
|
public static String formatSecondsDuration(Date startTime, Date endTime) {
|
||||||
|
if (startTime == null || endTime == null) {
|
||||||
|
// 什么都不返回
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
long betweenMs = DateUtil.betweenMs(startTime, endTime);
|
||||||
|
long days = betweenMs / (24 * 60 * 60 * 1000);
|
||||||
|
long hours = (betweenMs % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000);
|
||||||
|
long minutes = (betweenMs % (60 * 60 * 1000)) / (60 * 1000);
|
||||||
|
long seconds = (betweenMs % (60 * 1000)) / 1000;
|
||||||
|
long milliseconds = betweenMs % 1000;
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
|
if (days > 0) {
|
||||||
|
result.append(days).append("天");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hours > 0) {
|
||||||
|
result.append(hours).append("时");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minutes > 0) {
|
||||||
|
result.append(minutes).append("分");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seconds > 0) {
|
||||||
|
result.append(seconds).append("秒");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (milliseconds > 0) {
|
||||||
|
result.append(milliseconds).append("毫秒");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果所有单位都是0,则显示0毫秒
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
result.append("0毫秒");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-54
@@ -30,67 +30,67 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitDesEncryptUtil {
|
public final class KitDesEncryptUtil {
|
||||||
|
|
||||||
private static final String STR_PARAM = "Passw0rd";
|
private static final String STR_PARAM = "Passw0rd";
|
||||||
private static final IvParameterSpec IV = new IvParameterSpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
|
private static final IvParameterSpec IV = new IvParameterSpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
private static DESKeySpec getDesKeySpec(String source) throws Exception {
|
private static DESKeySpec getDesKeySpec(String source) throws Exception {
|
||||||
if (source == null || source.isEmpty()) {
|
if (source == null || source.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
return new DESKeySpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
|
|
||||||
}
|
}
|
||||||
|
return new DESKeySpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对称加密
|
* 对称加密
|
||||||
*/
|
*/
|
||||||
public static String desEncrypt(String source) throws Exception {
|
public static String desEncrypt(String source) throws Exception {
|
||||||
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
||||||
DESKeySpec desKeySpec = getDesKeySpec(source);
|
DESKeySpec desKeySpec = getDesKeySpec(source);
|
||||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||||
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV);
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV);
|
||||||
return byte2hex(cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
|
return byte2hex(cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对称解密
|
* 对称解密
|
||||||
*/
|
*/
|
||||||
public static String desDecrypt(String source) throws Exception {
|
public static String desDecrypt(String source) throws Exception {
|
||||||
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
||||||
byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8));
|
byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8));
|
||||||
DESKeySpec desKeySpec = getDesKeySpec(source);
|
DESKeySpec desKeySpec = getDesKeySpec(source);
|
||||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||||
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, IV);
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, IV);
|
||||||
byte[] retByte = cipher.doFinal(src);
|
byte[] retByte = cipher.doFinal(src);
|
||||||
return new String(retByte);
|
return new String(retByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String byte2hex(byte[] inStr) {
|
private static String byte2hex(byte[] inStr) {
|
||||||
String stmp;
|
String stmp;
|
||||||
StringBuilder out = new StringBuilder(inStr.length * 2);
|
StringBuilder out = new StringBuilder(inStr.length * 2);
|
||||||
for (byte b : inStr) {
|
for (byte b : inStr) {
|
||||||
stmp = Integer.toHexString(b & 0xFF);
|
stmp = Integer.toHexString(b & 0xFF);
|
||||||
if (stmp.length() == 1) {
|
if (stmp.length() == 1) {
|
||||||
out.append("0").append(stmp);
|
out.append("0").append(stmp);
|
||||||
} else {
|
} else {
|
||||||
out.append(stmp);
|
out.append(stmp);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return out.toString();
|
|
||||||
}
|
}
|
||||||
|
return out.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] hex2byte(byte[] b) {
|
private static byte[] hex2byte(byte[] b) {
|
||||||
int size = 2;
|
int size = 2;
|
||||||
if ((b.length % size) != 0) {
|
if ((b.length % size) != 0) {
|
||||||
throw new IllegalArgumentException("长度不是偶数");
|
throw new IllegalArgumentException("长度不是偶数");
|
||||||
}
|
|
||||||
byte[] b2 = new byte[b.length / 2];
|
|
||||||
for (int n = 0; n < b.length; n += size) {
|
|
||||||
String item = new String(b, n, 2);
|
|
||||||
b2[n / 2] = (byte)Integer.parseInt(item, 16);
|
|
||||||
}
|
|
||||||
return b2;
|
|
||||||
}
|
}
|
||||||
|
byte[] b2 = new byte[b.length / 2];
|
||||||
|
for (int n = 0; n < b.length; n += size) {
|
||||||
|
String item = new String(b, n, 2);
|
||||||
|
b2[n / 2] = (byte) Integer.parseInt(item, 16);
|
||||||
|
}
|
||||||
|
return b2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+160
-158
@@ -26,177 +26,179 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
* @date 2025-04-05
|
* @date 2025-04-05
|
||||||
*/
|
*/
|
||||||
public final class KitDistributedIdUtil {
|
public final class KitDistributedIdUtil {
|
||||||
/**
|
|
||||||
* 起始毫秒时间戳, 2020-12-30
|
|
||||||
*/
|
|
||||||
private static final long START_TIMESTAMP = 1609314517000L;
|
|
||||||
/**
|
|
||||||
* 数据中心 ID 所占位数
|
|
||||||
*/
|
|
||||||
private final long dataCenterIdBits;
|
|
||||||
/**
|
|
||||||
* 机器 ID 所占位数
|
|
||||||
*/
|
|
||||||
private final long workerIdBits;
|
|
||||||
/**
|
|
||||||
* 序列号所占位数
|
|
||||||
*/
|
|
||||||
private final long sequenceBits;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据中心 ID 最大值
|
* 起始毫秒时间戳, 2020-12-30
|
||||||
*/
|
*/
|
||||||
private final long maxDataCenterId;
|
private static final long START_TIMESTAMP = 1609314517000L;
|
||||||
/**
|
/**
|
||||||
* 机器 ID 最大值
|
* 数据中心 ID 所占位数
|
||||||
*/
|
*/
|
||||||
private final long maxWorkerId;
|
private final long dataCenterIdBits;
|
||||||
/**
|
/**
|
||||||
* 序列号最大值
|
* 机器 ID 所占位数
|
||||||
*/
|
*/
|
||||||
private final long sequenceMask;
|
private final long workerIdBits;
|
||||||
|
/**
|
||||||
|
* 序列号所占位数
|
||||||
|
*/
|
||||||
|
private final long sequenceBits;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 机器 ID 向左移位数
|
* 数据中心 ID 最大值
|
||||||
*/
|
*/
|
||||||
private final long workerIdShift;
|
private final long maxDataCenterId;
|
||||||
/**
|
/**
|
||||||
* 数据中心 ID 向左移位数
|
* 机器 ID 最大值
|
||||||
*/
|
*/
|
||||||
private final long dataCenterIdShift;
|
private final long maxWorkerId;
|
||||||
/**
|
/**
|
||||||
* 时间戳向左移位数
|
* 序列号最大值
|
||||||
*/
|
*/
|
||||||
private final long timestampLeftShift;
|
private final long sequenceMask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据中心 ID
|
* 机器 ID 向左移位数
|
||||||
*/
|
*/
|
||||||
private final long dataCenterId;
|
private final long workerIdShift;
|
||||||
/**
|
/**
|
||||||
* 机器 ID
|
* 数据中心 ID 向左移位数
|
||||||
*/
|
*/
|
||||||
private final long workerId;
|
private final long dataCenterIdShift;
|
||||||
/**
|
/**
|
||||||
* 序列号
|
* 时间戳向左移位数
|
||||||
*/
|
*/
|
||||||
private final AtomicLong sequence = new AtomicLong(0L);
|
private final long timestampLeftShift;
|
||||||
/**
|
|
||||||
* 上一次生成 ID 的时间戳
|
|
||||||
*/
|
|
||||||
private final AtomicLong lastTimestamp = new AtomicLong(-1L);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数, 初始化数据中心 ID 和机器 ID
|
* 数据中心 ID
|
||||||
*
|
*/
|
||||||
* @param dataCenterId 数据中心 ID
|
private final long dataCenterId;
|
||||||
* @param workerId 机器 ID
|
/**
|
||||||
* @param dataCenterIdBits 数据中心 ID 所占位数
|
* 机器 ID
|
||||||
* @param workerIdBits 机器 ID 所占位数
|
*/
|
||||||
* @param sequenceBits 序列号所占位数
|
private final long workerId;
|
||||||
*/
|
/**
|
||||||
public KitDistributedIdUtil(long dataCenterId, long workerId, long dataCenterIdBits, long workerIdBits,
|
* 序列号
|
||||||
long sequenceBits) {
|
*/
|
||||||
this.dataCenterIdBits = dataCenterIdBits;
|
private final AtomicLong sequence = new AtomicLong(0L);
|
||||||
this.workerIdBits = workerIdBits;
|
/**
|
||||||
this.sequenceBits = sequenceBits;
|
* 上一次生成 ID 的时间戳
|
||||||
|
*/
|
||||||
|
private final AtomicLong lastTimestamp = new AtomicLong(-1L);
|
||||||
|
|
||||||
this.maxDataCenterId = ~(-1L << dataCenterIdBits);
|
/**
|
||||||
this.maxWorkerId = ~(-1L << workerIdBits);
|
* 构造函数, 初始化数据中心 ID 和机器 ID
|
||||||
this.sequenceMask = ~(-1L << sequenceBits);
|
*
|
||||||
|
* @param dataCenterId 数据中心 ID
|
||||||
|
* @param workerId 机器 ID
|
||||||
|
* @param dataCenterIdBits 数据中心 ID 所占位数
|
||||||
|
* @param workerIdBits 机器 ID 所占位数
|
||||||
|
* @param sequenceBits 序列号所占位数
|
||||||
|
*/
|
||||||
|
public KitDistributedIdUtil(long dataCenterId, long workerId, long dataCenterIdBits, long workerIdBits,
|
||||||
|
long sequenceBits) {
|
||||||
|
this.dataCenterIdBits = dataCenterIdBits;
|
||||||
|
this.workerIdBits = workerIdBits;
|
||||||
|
this.sequenceBits = sequenceBits;
|
||||||
|
|
||||||
this.workerIdShift = sequenceBits;
|
this.maxDataCenterId = ~(-1L << dataCenterIdBits);
|
||||||
this.dataCenterIdShift = sequenceBits + workerIdBits;
|
this.maxWorkerId = ~(-1L << workerIdBits);
|
||||||
this.timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
|
this.sequenceMask = ~(-1L << sequenceBits);
|
||||||
|
|
||||||
if (dataCenterId > maxDataCenterId || dataCenterId < 0) {
|
this.workerIdShift = sequenceBits;
|
||||||
throw new IllegalArgumentException(
|
this.dataCenterIdShift = sequenceBits + workerIdBits;
|
||||||
"Data center ID can't be greater than " + maxDataCenterId + " or less than 0");
|
this.timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
|
||||||
|
|
||||||
|
if (dataCenterId > maxDataCenterId || dataCenterId < 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Data center ID can't be greater than " + maxDataCenterId + " or less than 0");
|
||||||
|
}
|
||||||
|
if (workerId > maxWorkerId || workerId < 0) {
|
||||||
|
throw new IllegalArgumentException("Worker ID can't be greater than " + maxWorkerId + " or less than 0");
|
||||||
|
}
|
||||||
|
this.dataCenterId = dataCenterId;
|
||||||
|
this.workerId = workerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例实例的下一个分布式 ID
|
||||||
|
*
|
||||||
|
* @return 生成的分布式 ID
|
||||||
|
*/
|
||||||
|
public static long fastNextId() {
|
||||||
|
return InstanceHolder.INSTANCE.nextId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主方法,用于测试分布式 ID 生成
|
||||||
|
*
|
||||||
|
* @param args 命令行参数
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
long id = Thread.currentThread().getId();
|
||||||
|
for (int j = 0; j < 20; j++) {
|
||||||
|
System.out.println(id + " - " + KitDistributedIdUtil.fastNextId());
|
||||||
}
|
}
|
||||||
if (workerId > maxWorkerId || workerId < 0) {
|
}).start();
|
||||||
throw new IllegalArgumentException("Worker ID can't be greater than " + maxWorkerId + " or less than 0");
|
}
|
||||||
}
|
}
|
||||||
this.dataCenterId = dataCenterId;
|
|
||||||
this.workerId = workerId;
|
/**
|
||||||
|
* 生成下一个分布式 ID
|
||||||
|
*
|
||||||
|
* @return 生成的分布式 ID
|
||||||
|
*/
|
||||||
|
public long nextId() {
|
||||||
|
long currentTimestamp = System.currentTimeMillis();
|
||||||
|
|
||||||
|
// 如果当前时间戳小于上一次生成 ID 的时间戳, 说明时钟回拨,抛出异常
|
||||||
|
if (currentTimestamp < lastTimestamp.get()) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
"Clock moved backwards. Refusing to generate id for " + (lastTimestamp.get() - currentTimestamp) +
|
||||||
|
" milliseconds");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
long lastTimestampValue = lastTimestamp.get();
|
||||||
* 获取单例实例的下一个分布式 ID
|
if (currentTimestamp == lastTimestampValue) {
|
||||||
*
|
// 同一毫秒内,序列号自增
|
||||||
* @return 生成的分布式 ID
|
long seq = sequence.incrementAndGet() & sequenceMask;
|
||||||
*/
|
if (seq == 0) {
|
||||||
public static long fastNextId() {
|
// 序列号溢出,等待下一毫秒
|
||||||
return InstanceHolder.INSTANCE.nextId();
|
currentTimestamp = waitNextMillis(lastTimestampValue);
|
||||||
|
}
|
||||||
|
return ((currentTimestamp - START_TIMESTAMP) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) |
|
||||||
|
(workerId << workerIdShift) | seq;
|
||||||
|
} else {
|
||||||
|
// 时间戳改变,重置序列号
|
||||||
|
sequence.set(0L);
|
||||||
|
lastTimestamp.set(currentTimestamp);
|
||||||
|
return ((currentTimestamp - START_TIMESTAMP) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) |
|
||||||
|
(workerId << workerIdShift);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主方法,用于测试分布式 ID 生成
|
* 等待下一毫秒
|
||||||
*
|
*
|
||||||
* @param args 命令行参数
|
* @param lastTimestamp 上一次生成 ID 的时间戳
|
||||||
*/
|
* @return 当前时间戳
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
for (int i = 0; i < 10; i++) {
|
private long waitNextMillis(long lastTimestamp) {
|
||||||
new Thread(() -> {
|
long timestamp = System.currentTimeMillis();
|
||||||
long id = Thread.currentThread().getId();
|
while (timestamp <= lastTimestamp) {
|
||||||
for (int j = 0; j < 20; j++) {
|
timestamp = System.currentTimeMillis();
|
||||||
System.out.println(id + " - " + KitDistributedIdUtil.fastNextId());
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成下一个分布式 ID
|
* 单例实例持有者类
|
||||||
*
|
*/
|
||||||
* @return 生成的分布式 ID
|
private static class InstanceHolder {
|
||||||
*/
|
|
||||||
public long nextId() {
|
|
||||||
long currentTimestamp = System.currentTimeMillis();
|
|
||||||
|
|
||||||
// 如果当前时间戳小于上一次生成 ID 的时间戳, 说明时钟回拨,抛出异常
|
private static final KitDistributedIdUtil INSTANCE = new KitDistributedIdUtil(20, 12, 5, 5, 12);
|
||||||
if (currentTimestamp < lastTimestamp.get()) {
|
}
|
||||||
throw new BadRequestException(
|
|
||||||
"Clock moved backwards. Refusing to generate id for " + (lastTimestamp.get() - currentTimestamp) +
|
|
||||||
" milliseconds");
|
|
||||||
}
|
|
||||||
|
|
||||||
long lastTimestampValue = lastTimestamp.get();
|
|
||||||
if (currentTimestamp == lastTimestampValue) {
|
|
||||||
// 同一毫秒内,序列号自增
|
|
||||||
long seq = sequence.incrementAndGet() & sequenceMask;
|
|
||||||
if (seq == 0) {
|
|
||||||
// 序列号溢出,等待下一毫秒
|
|
||||||
currentTimestamp = waitNextMillis(lastTimestampValue);
|
|
||||||
}
|
|
||||||
return ((currentTimestamp - START_TIMESTAMP) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) |
|
|
||||||
(workerId << workerIdShift) | seq;
|
|
||||||
} else {
|
|
||||||
// 时间戳改变,重置序列号
|
|
||||||
sequence.set(0L);
|
|
||||||
lastTimestamp.set(currentTimestamp);
|
|
||||||
return ((currentTimestamp - START_TIMESTAMP) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) |
|
|
||||||
(workerId << workerIdShift);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 等待下一毫秒
|
|
||||||
*
|
|
||||||
* @param lastTimestamp 上一次生成 ID 的时间戳
|
|
||||||
* @return 当前时间戳
|
|
||||||
*/
|
|
||||||
private long waitNextMillis(long lastTimestamp) {
|
|
||||||
long timestamp = System.currentTimeMillis();
|
|
||||||
while (timestamp <= lastTimestamp) {
|
|
||||||
timestamp = System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
return timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单例实例持有者类
|
|
||||||
*/
|
|
||||||
private static class InstanceHolder {
|
|
||||||
private static final KitDistributedIdUtil INSTANCE = new KitDistributedIdUtil(20, 12, 5, 5, 12);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+331
-331
@@ -16,6 +16,11 @@
|
|||||||
|
|
||||||
package cn.odboy.util;
|
package cn.odboy.util;
|
||||||
|
|
||||||
|
import static cn.odboy.constant.SystemConst.SYMBOL_ADD;
|
||||||
|
import static cn.odboy.constant.SystemConst.SYMBOL_AT;
|
||||||
|
import static cn.odboy.constant.SystemConst.SYMBOL_EQUAL;
|
||||||
|
import static cn.odboy.constant.SystemConst.SYMBOL_SUBTRACT;
|
||||||
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.poi.excel.BigExcelWriter;
|
import cn.hutool.poi.excel.BigExcelWriter;
|
||||||
@@ -45,11 +50,6 @@ import org.apache.poi.util.IOUtils;
|
|||||||
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import static cn.odboy.constant.SystemConst.SYMBOL_ADD;
|
|
||||||
import static cn.odboy.constant.SystemConst.SYMBOL_AT;
|
|
||||||
import static cn.odboy.constant.SystemConst.SYMBOL_EQUAL;
|
|
||||||
import static cn.odboy.constant.SystemConst.SYMBOL_SUBTRACT;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* File工具类, 扩展 hutool 工具包
|
* File工具类, 扩展 hutool 工具包
|
||||||
*/
|
*/
|
||||||
@@ -57,342 +57,342 @@ import static cn.odboy.constant.SystemConst.SYMBOL_SUBTRACT;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitFileUtil extends cn.hutool.core.io.FileUtil {
|
public final class KitFileUtil extends cn.hutool.core.io.FileUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 系统临时目录
|
* 系统临时目录
|
||||||
* <br>
|
* <br>
|
||||||
* windows 包含路径分割符, 但Linux 不包含, 在windows \\==\ 前提下, 为安全起见 同意拼装 路径分割符,
|
* windows 包含路径分割符, 但Linux 不包含, 在windows \\==\ 前提下, 为安全起见 同意拼装 路径分割符,
|
||||||
* <pre>
|
* <pre>
|
||||||
* java.io.tmpdir
|
* java.io.tmpdir
|
||||||
* windows : C:\Users/xxx\AppData\Local\Temp\
|
* windows : C:\Users/xxx\AppData\Local\Temp\
|
||||||
* linux: /temp
|
* linux: /temp
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static final String SYS_TEM_DIR = System.getProperty("java.io.tmpdir") + File.separator;
|
public static final String SYS_TEM_DIR = System.getProperty("java.io.tmpdir") + File.separator;
|
||||||
/**
|
/**
|
||||||
* 定义GB的计算常量
|
* 定义GB的计算常量
|
||||||
*/
|
*/
|
||||||
private static final int GB = 1024 * 1024 * 1024;
|
private static final int GB = 1024 * 1024 * 1024;
|
||||||
/**
|
/**
|
||||||
* 定义MB的计算常量
|
* 定义MB的计算常量
|
||||||
*/
|
*/
|
||||||
private static final int MB = 1024 * 1024;
|
private static final int MB = 1024 * 1024;
|
||||||
/**
|
/**
|
||||||
* 定义KB的计算常量
|
* 定义KB的计算常量
|
||||||
*/
|
*/
|
||||||
private static final int KB = 1024;
|
private static final int KB = 1024;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化小数
|
* 格式化小数
|
||||||
*/
|
*/
|
||||||
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MultipartFile转File
|
* MultipartFile转File
|
||||||
*/
|
*/
|
||||||
public static File toFile(MultipartFile multipartFile) {
|
public static File toFile(MultipartFile multipartFile) {
|
||||||
// 获取文件名
|
// 获取文件名
|
||||||
String fileName = multipartFile.getOriginalFilename();
|
String fileName = multipartFile.getOriginalFilename();
|
||||||
// 获取文件后缀
|
// 获取文件后缀
|
||||||
String prefix = "." + getSuffix(fileName);
|
String prefix = "." + getSuffix(fileName);
|
||||||
File file = null;
|
File file = null;
|
||||||
|
try {
|
||||||
|
// 用uuid作为文件名, 防止生成的临时文件重复
|
||||||
|
file = new File(SYS_TEM_DIR + IdUtil.simpleUUID() + prefix);
|
||||||
|
// MultipartFile to File
|
||||||
|
multipartFile.transferTo(file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("保存临时文件失败", e);
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件大小转换
|
||||||
|
*/
|
||||||
|
public static String getSize(long size) {
|
||||||
|
String resultSize;
|
||||||
|
if (size / GB >= 1) {
|
||||||
|
//如果当前Byte的值大于等于1GB
|
||||||
|
resultSize = DF.format(size / (float) GB) + " GB";
|
||||||
|
} else if (size / MB >= 1) {
|
||||||
|
//如果当前Byte的值大于等于1MB
|
||||||
|
resultSize = DF.format(size / (float) MB) + " MB";
|
||||||
|
} else if (size / KB >= 1) {
|
||||||
|
//如果当前Byte的值大于等于1KB
|
||||||
|
resultSize = DF.format(size / (float) KB) + " KB";
|
||||||
|
} else {
|
||||||
|
resultSize = size + " B";
|
||||||
|
}
|
||||||
|
return resultSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inputStream 转 File
|
||||||
|
*/
|
||||||
|
public static File inputStreamToFile(InputStream ins, String name) {
|
||||||
|
File file = new File(SYS_TEM_DIR + name);
|
||||||
|
if (file.exists()) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
OutputStream os = null;
|
||||||
|
try {
|
||||||
|
os = Files.newOutputStream(file.toPath());
|
||||||
|
int bytesRead;
|
||||||
|
int len = 8192;
|
||||||
|
byte[] buffer = new byte[len];
|
||||||
|
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
|
||||||
|
os.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
KitCloseUtil.close(os);
|
||||||
|
KitCloseUtil.close(ins);
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将文件名解析成文件的上传路径
|
||||||
|
*/
|
||||||
|
public static File upload(MultipartFile file, String filePath) {
|
||||||
|
Date date = new Date();
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
|
||||||
|
// 过滤非法文件名
|
||||||
|
String name = getPrefix(verifyFilename(file.getOriginalFilename()));
|
||||||
|
String suffix = getSuffix(file.getOriginalFilename());
|
||||||
|
String nowStr = "-" + format.format(date);
|
||||||
|
try {
|
||||||
|
String fileName = name + nowStr + "." + suffix;
|
||||||
|
String path = filePath + fileName;
|
||||||
|
// getCanonicalFile 可解析正确各种路径
|
||||||
|
File dest = new File(path).getCanonicalFile();
|
||||||
|
// 检测是否存在目录
|
||||||
|
if (!dest.getParentFile().exists()) {
|
||||||
|
if (!dest.getParentFile().mkdirs()) {
|
||||||
|
log.error("创建目录失败, {}", dest.getParentFile().getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 文件写入
|
||||||
|
file.transferTo(dest);
|
||||||
|
return dest;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("文件上传失败", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出excel
|
||||||
|
*/
|
||||||
|
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
|
||||||
|
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
|
||||||
|
File file = new File(tempPath);
|
||||||
|
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
|
||||||
|
// 处理数据以防止CSV注入
|
||||||
|
List<Map<String, Object>> sanitizedList = list.parallelStream().map(map -> {
|
||||||
|
Map<String, Object> sanitizedMap = new LinkedHashMap<>();
|
||||||
|
map.forEach((key, value) -> {
|
||||||
|
if (value instanceof String strValue) {
|
||||||
|
// 检查并处理以特殊字符开头的值
|
||||||
|
if (strValue.startsWith(SYMBOL_EQUAL) || strValue.startsWith(SYMBOL_ADD) ||
|
||||||
|
strValue.startsWith(SYMBOL_SUBTRACT) || strValue.startsWith(SYMBOL_AT)) {
|
||||||
|
// 添加单引号前缀
|
||||||
|
strValue = "'" + strValue;
|
||||||
|
}
|
||||||
|
sanitizedMap.put(key, strValue);
|
||||||
|
} else {
|
||||||
|
sanitizedMap.put(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return sanitizedMap;
|
||||||
|
}).toList();
|
||||||
|
// 一次性写出内容, 使用默认样式,强制输出标题
|
||||||
|
writer.write(sanitizedList, true);
|
||||||
|
SXSSFSheet sheet = (SXSSFSheet) writer.getSheet();
|
||||||
|
//上面需要强转SXSSFSheet 不然没有trackAllColumnsForAutoSizing方法
|
||||||
|
sheet.trackAllColumnsForAutoSizing();
|
||||||
|
//列宽自适应
|
||||||
|
writer.autoSizeColumnAll();
|
||||||
|
//response为HttpServletResponse对象
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
||||||
|
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
|
||||||
|
ServletOutputStream out = response.getOutputStream();
|
||||||
|
// 终止后删除临时文件
|
||||||
|
file.deleteOnExit();
|
||||||
|
writer.flush(out, true);
|
||||||
|
//此处记得关闭输出Servlet流
|
||||||
|
IoUtil.close(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件类型
|
||||||
|
*/
|
||||||
|
public static String getFileType(String type) {
|
||||||
|
String documents = "txt doc pdf ppt pps xlsx xls docx";
|
||||||
|
String voice = "mp3 wav wma mpa ram ra aac aif m4a";
|
||||||
|
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
|
||||||
|
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
|
||||||
|
if (image.contains(type)) {
|
||||||
|
return FileTypeEnum.IMAGE.getCode();
|
||||||
|
} else if (documents.contains(type)) {
|
||||||
|
return FileTypeEnum.DOC.getCode();
|
||||||
|
} else if (voice.contains(type)) {
|
||||||
|
return FileTypeEnum.VOICE.getCode();
|
||||||
|
} else if (video.contains(type)) {
|
||||||
|
return FileTypeEnum.VIDEO.getCode();
|
||||||
|
} else {
|
||||||
|
return FileTypeEnum.OTHER.getCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查文件大小是否超出最大值
|
||||||
|
*/
|
||||||
|
public static void checkSize(long maxSize, long size) {
|
||||||
|
// 1M
|
||||||
|
int len = 1024 * 1024;
|
||||||
|
if (size > (maxSize * len)) {
|
||||||
|
throw new BadRequestException("文件超出规定大小:" + maxSize + "MB");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断两个文件是否相同
|
||||||
|
*/
|
||||||
|
public static boolean check(File file1, File file2) {
|
||||||
|
String img1Md5 = getMd5(file1);
|
||||||
|
String img2Md5 = getMd5(file2);
|
||||||
|
if (img1Md5 != null) {
|
||||||
|
return img1Md5.equals(img2Md5);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断两个文件是否相同
|
||||||
|
*/
|
||||||
|
public static boolean check(String file1Md5, String file2Md5) {
|
||||||
|
return file1Md5.equals(file2Md5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件字节数组
|
||||||
|
*/
|
||||||
|
private static byte[] getByte(File file) {
|
||||||
|
byte[] b = new byte[(int) file.length()];
|
||||||
|
InputStream in = null;
|
||||||
|
try {
|
||||||
|
in = Files.newInputStream(file.toPath());
|
||||||
|
// 忽略read值
|
||||||
|
int read = in.read(b);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
KitCloseUtil.close(in);
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件MD5值
|
||||||
|
*/
|
||||||
|
private static String getMd5(byte[] bytes) {
|
||||||
|
// 16进制字符
|
||||||
|
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||||
|
try {
|
||||||
|
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
|
||||||
|
mdTemp.update(bytes);
|
||||||
|
byte[] md = mdTemp.digest();
|
||||||
|
int j = md.length;
|
||||||
|
char[] str = new char[j * 2];
|
||||||
|
int k = 0;
|
||||||
|
// 移位 输出字符串
|
||||||
|
for (byte byte0 : md) {
|
||||||
|
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
|
||||||
|
str[k++] = hexDigits[byte0 & 0xf];
|
||||||
|
}
|
||||||
|
return new String(str);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取文件MD5值失败", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
*
|
||||||
|
* @param request /
|
||||||
|
* @param response /
|
||||||
|
* @param file /
|
||||||
|
*/
|
||||||
|
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file,
|
||||||
|
boolean deleteOnExit) {
|
||||||
|
response.setCharacterEncoding(request.getCharacterEncoding());
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
FileInputStream fis = null;
|
||||||
|
try {
|
||||||
|
fis = new FileInputStream(file);
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
|
||||||
|
IOUtils.copy(fis, response.getOutputStream());
|
||||||
|
response.flushBuffer();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("下载文件失败", e);
|
||||||
|
} finally {
|
||||||
|
if (fis != null) {
|
||||||
try {
|
try {
|
||||||
// 用uuid作为文件名, 防止生成的临时文件重复
|
fis.close();
|
||||||
file = new File(SYS_TEM_DIR + IdUtil.simpleUUID() + prefix);
|
if (deleteOnExit) {
|
||||||
// MultipartFile to File
|
file.deleteOnExit();
|
||||||
multipartFile.transferTo(file);
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("保存临时文件失败", e);
|
log.error("关闭IO流失败", e);
|
||||||
}
|
}
|
||||||
return file;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证并过滤非法的文件名
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return 文件名
|
||||||
|
*/
|
||||||
|
public static String verifyFilename(String fileName) {
|
||||||
|
// 过滤掉特殊字符
|
||||||
|
fileName = fileName.replaceAll("[\\\\/:*?\"<>|~\\s]", "");
|
||||||
|
|
||||||
|
// 去掉文件名开头和结尾的空格和点
|
||||||
|
fileName = fileName.trim().replaceAll("^[. ]+|[. ]+$", "");
|
||||||
|
|
||||||
|
// 不允许文件名超过255(在Mac和Linux中)或260(在Windows中)个字符
|
||||||
|
int maxFileNameLength = 255;
|
||||||
|
if (System.getProperty(SystemConst.PROPERTY_OS_NAME).startsWith(SystemConst.OS_NAME_WINDOWS)) {
|
||||||
|
maxFileNameLength = 260;
|
||||||
|
}
|
||||||
|
if (fileName.length() > maxFileNameLength) {
|
||||||
|
fileName = fileName.substring(0, maxFileNameLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 过滤掉控制字符
|
||||||
* 文件大小转换
|
fileName = fileName.replaceAll("[\\p{Cntrl}]", "");
|
||||||
*/
|
|
||||||
public static String getSize(long size) {
|
|
||||||
String resultSize;
|
|
||||||
if (size / GB >= 1) {
|
|
||||||
//如果当前Byte的值大于等于1GB
|
|
||||||
resultSize = DF.format(size / (float)GB) + " GB";
|
|
||||||
} else if (size / MB >= 1) {
|
|
||||||
//如果当前Byte的值大于等于1MB
|
|
||||||
resultSize = DF.format(size / (float)MB) + " MB";
|
|
||||||
} else if (size / KB >= 1) {
|
|
||||||
//如果当前Byte的值大于等于1KB
|
|
||||||
resultSize = DF.format(size / (float)KB) + " KB";
|
|
||||||
} else {
|
|
||||||
resultSize = size + " B";
|
|
||||||
}
|
|
||||||
return resultSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 过滤掉 ".." 路径
|
||||||
* inputStream 转 File
|
fileName = fileName.replaceAll("\\.{2,}", "");
|
||||||
*/
|
|
||||||
public static File inputStreamToFile(InputStream ins, String name) {
|
|
||||||
File file = new File(SYS_TEM_DIR + name);
|
|
||||||
if (file.exists()) {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
OutputStream os = null;
|
|
||||||
try {
|
|
||||||
os = Files.newOutputStream(file.toPath());
|
|
||||||
int bytesRead;
|
|
||||||
int len = 8192;
|
|
||||||
byte[] buffer = new byte[len];
|
|
||||||
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
|
|
||||||
os.write(buffer, 0, bytesRead);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
} finally {
|
|
||||||
KitCloseUtil.close(os);
|
|
||||||
KitCloseUtil.close(ins);
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 去掉文件名开头的 ".."
|
||||||
* 将文件名解析成文件的上传路径
|
fileName = fileName.replaceAll("^\\.+/", "");
|
||||||
*/
|
|
||||||
public static File upload(MultipartFile file, String filePath) {
|
|
||||||
Date date = new Date();
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
|
|
||||||
// 过滤非法文件名
|
|
||||||
String name = getPrefix(verifyFilename(file.getOriginalFilename()));
|
|
||||||
String suffix = getSuffix(file.getOriginalFilename());
|
|
||||||
String nowStr = "-" + format.format(date);
|
|
||||||
try {
|
|
||||||
String fileName = name + nowStr + "." + suffix;
|
|
||||||
String path = filePath + fileName;
|
|
||||||
// getCanonicalFile 可解析正确各种路径
|
|
||||||
File dest = new File(path).getCanonicalFile();
|
|
||||||
// 检测是否存在目录
|
|
||||||
if (!dest.getParentFile().exists()) {
|
|
||||||
if (!dest.getParentFile().mkdirs()) {
|
|
||||||
log.error("创建目录失败, {}", dest.getParentFile().getAbsolutePath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 文件写入
|
|
||||||
file.transferTo(dest);
|
|
||||||
return dest;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("文件上传失败", e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 保留文件名中最后一个 "." 字符,过滤掉其他 "."
|
||||||
* 导出excel
|
fileName = fileName.replaceAll("^(.*)(\\.[^.]*)$", "$1").replaceAll("\\.", "") +
|
||||||
*/
|
fileName.replaceAll("^(.*)(\\.[^.]*)$", "$2");
|
||||||
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
|
|
||||||
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
|
|
||||||
File file = new File(tempPath);
|
|
||||||
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
|
|
||||||
// 处理数据以防止CSV注入
|
|
||||||
List<Map<String, Object>> sanitizedList = list.parallelStream().map(map -> {
|
|
||||||
Map<String, Object> sanitizedMap = new LinkedHashMap<>();
|
|
||||||
map.forEach((key, value) -> {
|
|
||||||
if (value instanceof String strValue) {
|
|
||||||
// 检查并处理以特殊字符开头的值
|
|
||||||
if (strValue.startsWith(SYMBOL_EQUAL) || strValue.startsWith(SYMBOL_ADD) ||
|
|
||||||
strValue.startsWith(SYMBOL_SUBTRACT) || strValue.startsWith(SYMBOL_AT)) {
|
|
||||||
// 添加单引号前缀
|
|
||||||
strValue = "'" + strValue;
|
|
||||||
}
|
|
||||||
sanitizedMap.put(key, strValue);
|
|
||||||
} else {
|
|
||||||
sanitizedMap.put(key, value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return sanitizedMap;
|
|
||||||
}).toList();
|
|
||||||
// 一次性写出内容, 使用默认样式,强制输出标题
|
|
||||||
writer.write(sanitizedList, true);
|
|
||||||
SXSSFSheet sheet = (SXSSFSheet)writer.getSheet();
|
|
||||||
//上面需要强转SXSSFSheet 不然没有trackAllColumnsForAutoSizing方法
|
|
||||||
sheet.trackAllColumnsForAutoSizing();
|
|
||||||
//列宽自适应
|
|
||||||
writer.autoSizeColumnAll();
|
|
||||||
//response为HttpServletResponse对象
|
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|
||||||
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
|
||||||
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
|
|
||||||
ServletOutputStream out = response.getOutputStream();
|
|
||||||
// 终止后删除临时文件
|
|
||||||
file.deleteOnExit();
|
|
||||||
writer.flush(out, true);
|
|
||||||
//此处记得关闭输出Servlet流
|
|
||||||
IoUtil.close(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return fileName;
|
||||||
* 获取文件类型
|
}
|
||||||
*/
|
|
||||||
public static String getFileType(String type) {
|
|
||||||
String documents = "txt doc pdf ppt pps xlsx xls docx";
|
|
||||||
String voice = "mp3 wav wma mpa ram ra aac aif m4a";
|
|
||||||
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
|
|
||||||
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
|
|
||||||
if (image.contains(type)) {
|
|
||||||
return FileTypeEnum.IMAGE.getCode();
|
|
||||||
} else if (documents.contains(type)) {
|
|
||||||
return FileTypeEnum.DOC.getCode();
|
|
||||||
} else if (voice.contains(type)) {
|
|
||||||
return FileTypeEnum.VOICE.getCode();
|
|
||||||
} else if (video.contains(type)) {
|
|
||||||
return FileTypeEnum.VIDEO.getCode();
|
|
||||||
} else {
|
|
||||||
return FileTypeEnum.OTHER.getCode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static String getMd5(File file) {
|
||||||
* 检查文件大小是否超出最大值
|
return getMd5(getByte(file));
|
||||||
*/
|
}
|
||||||
public static void checkSize(long maxSize, long size) {
|
|
||||||
// 1M
|
|
||||||
int len = 1024 * 1024;
|
|
||||||
if (size > (maxSize * len)) {
|
|
||||||
throw new BadRequestException("文件超出规定大小:" + maxSize + "MB");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断两个文件是否相同
|
|
||||||
*/
|
|
||||||
public static boolean check(File file1, File file2) {
|
|
||||||
String img1Md5 = getMd5(file1);
|
|
||||||
String img2Md5 = getMd5(file2);
|
|
||||||
if (img1Md5 != null) {
|
|
||||||
return img1Md5.equals(img2Md5);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断两个文件是否相同
|
|
||||||
*/
|
|
||||||
public static boolean check(String file1Md5, String file2Md5) {
|
|
||||||
return file1Md5.equals(file2Md5);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件字节数组
|
|
||||||
*/
|
|
||||||
private static byte[] getByte(File file) {
|
|
||||||
byte[] b = new byte[(int)file.length()];
|
|
||||||
InputStream in = null;
|
|
||||||
try {
|
|
||||||
in = Files.newInputStream(file.toPath());
|
|
||||||
// 忽略read值
|
|
||||||
int read = in.read(b);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
KitCloseUtil.close(in);
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件MD5值
|
|
||||||
*/
|
|
||||||
private static String getMd5(byte[] bytes) {
|
|
||||||
// 16进制字符
|
|
||||||
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
||||||
try {
|
|
||||||
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
|
|
||||||
mdTemp.update(bytes);
|
|
||||||
byte[] md = mdTemp.digest();
|
|
||||||
int j = md.length;
|
|
||||||
char[] str = new char[j * 2];
|
|
||||||
int k = 0;
|
|
||||||
// 移位 输出字符串
|
|
||||||
for (byte byte0 : md) {
|
|
||||||
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
|
|
||||||
str[k++] = hexDigits[byte0 & 0xf];
|
|
||||||
}
|
|
||||||
return new String(str);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("获取文件MD5值失败", e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载文件
|
|
||||||
*
|
|
||||||
* @param request /
|
|
||||||
* @param response /
|
|
||||||
* @param file /
|
|
||||||
*/
|
|
||||||
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file,
|
|
||||||
boolean deleteOnExit) {
|
|
||||||
response.setCharacterEncoding(request.getCharacterEncoding());
|
|
||||||
response.setContentType("application/octet-stream");
|
|
||||||
FileInputStream fis = null;
|
|
||||||
try {
|
|
||||||
fis = new FileInputStream(file);
|
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
|
|
||||||
IOUtils.copy(fis, response.getOutputStream());
|
|
||||||
response.flushBuffer();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("下载文件失败", e);
|
|
||||||
} finally {
|
|
||||||
if (fis != null) {
|
|
||||||
try {
|
|
||||||
fis.close();
|
|
||||||
if (deleteOnExit) {
|
|
||||||
file.deleteOnExit();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("关闭IO流失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证并过滤非法的文件名
|
|
||||||
*
|
|
||||||
* @param fileName 文件名
|
|
||||||
* @return 文件名
|
|
||||||
*/
|
|
||||||
public static String verifyFilename(String fileName) {
|
|
||||||
// 过滤掉特殊字符
|
|
||||||
fileName = fileName.replaceAll("[\\\\/:*?\"<>|~\\s]", "");
|
|
||||||
|
|
||||||
// 去掉文件名开头和结尾的空格和点
|
|
||||||
fileName = fileName.trim().replaceAll("^[. ]+|[. ]+$", "");
|
|
||||||
|
|
||||||
// 不允许文件名超过255(在Mac和Linux中)或260(在Windows中)个字符
|
|
||||||
int maxFileNameLength = 255;
|
|
||||||
if (System.getProperty(SystemConst.PROPERTY_OS_NAME).startsWith(SystemConst.OS_NAME_WINDOWS)) {
|
|
||||||
maxFileNameLength = 260;
|
|
||||||
}
|
|
||||||
if (fileName.length() > maxFileNameLength) {
|
|
||||||
fileName = fileName.substring(0, maxFileNameLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 过滤掉控制字符
|
|
||||||
fileName = fileName.replaceAll("[\\p{Cntrl}]", "");
|
|
||||||
|
|
||||||
// 过滤掉 ".." 路径
|
|
||||||
fileName = fileName.replaceAll("\\.{2,}", "");
|
|
||||||
|
|
||||||
// 去掉文件名开头的 ".."
|
|
||||||
fileName = fileName.replaceAll("^\\.+/", "");
|
|
||||||
|
|
||||||
// 保留文件名中最后一个 "." 字符,过滤掉其他 "."
|
|
||||||
fileName = fileName.replaceAll("^(.*)(\\.[^.]*)$", "$1").replaceAll("\\.", "") +
|
|
||||||
fileName.replaceAll("^(.*)(\\.[^.]*)$", "$2");
|
|
||||||
|
|
||||||
return fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getMd5(File file) {
|
|
||||||
return getMd5(getByte(file));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-54
@@ -29,61 +29,62 @@ import net.dreamlu.mica.ip2region.core.IpInfo;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitIPUtil {
|
public final class KitIPUtil {
|
||||||
/**
|
|
||||||
* 注入bean
|
|
||||||
*/
|
|
||||||
private final static Ip2regionSearcher IP_SEARCHER = KitSpringBeanHolder.getBean(Ip2regionSearcher.class);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ip获取详细地址
|
* 注入bean
|
||||||
*/
|
*/
|
||||||
public static String getCityInfo(String ip) {
|
private final static Ip2regionSearcher IP_SEARCHER = KitSpringBeanHolder.getBean(Ip2regionSearcher.class);
|
||||||
IpInfo ipInfo = IP_SEARCHER.memorySearch(ip);
|
|
||||||
if (ipInfo != null) {
|
|
||||||
return ipInfo.getAddress();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前机器的IP
|
* 根据ip获取详细地址
|
||||||
*
|
*/
|
||||||
* @return /
|
public static String getCityInfo(String ip) {
|
||||||
*/
|
IpInfo ipInfo = IP_SEARCHER.memorySearch(ip);
|
||||||
public static String getLocalIp() {
|
if (ipInfo != null) {
|
||||||
try {
|
return ipInfo.getAddress();
|
||||||
InetAddress candidateAddress = null;
|
|
||||||
// 遍历所有的网络接口
|
|
||||||
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
|
||||||
interfaces.hasMoreElements(); ) {
|
|
||||||
NetworkInterface anInterface = interfaces.nextElement();
|
|
||||||
// 在所有的接口下再遍历IP
|
|
||||||
for (Enumeration<InetAddress> inetAddresses = anInterface.getInetAddresses();
|
|
||||||
inetAddresses.hasMoreElements(); ) {
|
|
||||||
InetAddress inetAddr = inetAddresses.nextElement();
|
|
||||||
// 排除loopback类型地址
|
|
||||||
if (!inetAddr.isLoopbackAddress()) {
|
|
||||||
if (inetAddr.isSiteLocalAddress()) {
|
|
||||||
// 如果是site-local地址, 就是它了
|
|
||||||
return inetAddr.getHostAddress();
|
|
||||||
} else if (candidateAddress == null) {
|
|
||||||
// site-local类型的地址未被发现, 先记录候选地址
|
|
||||||
candidateAddress = inetAddr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (candidateAddress != null) {
|
|
||||||
return candidateAddress.getHostAddress();
|
|
||||||
}
|
|
||||||
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
|
||||||
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
|
||||||
if (jdkSuppliedAddress == null) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return jdkSuppliedAddress.getHostAddress();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前机器的IP
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static String getLocalIp() {
|
||||||
|
try {
|
||||||
|
InetAddress candidateAddress = null;
|
||||||
|
// 遍历所有的网络接口
|
||||||
|
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
|
interfaces.hasMoreElements(); ) {
|
||||||
|
NetworkInterface anInterface = interfaces.nextElement();
|
||||||
|
// 在所有的接口下再遍历IP
|
||||||
|
for (Enumeration<InetAddress> inetAddresses = anInterface.getInetAddresses();
|
||||||
|
inetAddresses.hasMoreElements(); ) {
|
||||||
|
InetAddress inetAddr = inetAddresses.nextElement();
|
||||||
|
// 排除loopback类型地址
|
||||||
|
if (!inetAddr.isLoopbackAddress()) {
|
||||||
|
if (inetAddr.isSiteLocalAddress()) {
|
||||||
|
// 如果是site-local地址, 就是它了
|
||||||
|
return inetAddr.getHostAddress();
|
||||||
|
} else if (candidateAddress == null) {
|
||||||
|
// site-local类型的地址未被发现, 先记录候选地址
|
||||||
|
candidateAddress = inetAddr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (candidateAddress != null) {
|
||||||
|
return candidateAddress.getHostAddress();
|
||||||
|
}
|
||||||
|
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
||||||
|
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
||||||
|
if (jdkSuppliedAddress == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return jdkSuppliedAddress.getHostAddress();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-12
@@ -30,18 +30,21 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitJsonUtil {
|
public final class KitJsonUtil {
|
||||||
public static <K, V> Map<K, V> toMap(String jsonStr, Class<K> kClass, Class<V> vClass) {
|
|
||||||
return JSON.parseObject(jsonStr, new TypeReference<>() {});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> List<V> toList(String jsonStr, Class<V> vClass) {
|
public static <K, V> Map<K, V> toMap(String jsonStr, Class<K> kClass, Class<V> vClass) {
|
||||||
return JSON.parseObject(jsonStr, new TypeReference<>() {});
|
return JSON.parseObject(jsonStr, new TypeReference<>() {
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static <V> List<V> toList(String jsonStr, Class<V> vClass) {
|
||||||
String mapStr = "{\"key1\":\"value1\", \"key2\":\"value2\"}";
|
return JSON.parseObject(jsonStr, new TypeReference<>() {
|
||||||
System.err.println(KitJsonUtil.toMap(mapStr, String.class, String.class));
|
});
|
||||||
String listStr = "[\"value1\", \"value2\"]";
|
}
|
||||||
System.err.println(KitJsonUtil.toList(listStr, String.class));
|
|
||||||
}
|
public static void main(String[] args) {
|
||||||
|
String mapStr = "{\"key1\":\"value1\", \"key2\":\"value2\"}";
|
||||||
|
System.err.println(KitJsonUtil.toMap(mapStr, String.class, String.class));
|
||||||
|
String listStr = "[\"value1\", \"value2\"]";
|
||||||
|
System.err.println(KitJsonUtil.toList(listStr, String.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-32
@@ -32,38 +32,38 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitListUtil {
|
public final class KitListUtil {
|
||||||
/**
|
|
||||||
* 根据对象T的属性去重
|
|
||||||
*
|
|
||||||
* @param items 对象T集合
|
|
||||||
* @param classifier 对象T属性表达式, 比如: User::getName
|
|
||||||
*/
|
|
||||||
public static <T, K> List<T> distinctByArgs(List<T> items, Function<? super T, ? extends K> classifier) {
|
|
||||||
return items.stream().collect(Collectors.groupingBy(classifier)).values().stream()
|
|
||||||
.map(monitorItem -> monitorItem.iterator().next()).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* list转map, 保留新值 -> Map<String, User> userMap = items.stream().collect(Collectors.toMap(User::getName, user ->
|
* 根据对象T的属性去重
|
||||||
* user)) <br> list转map, 保留旧值 -> Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getName,
|
*
|
||||||
* user -> user, (existing, replacement) -> existing));
|
* @param items 对象T集合
|
||||||
*/
|
* @param classifier 对象T属性表达式, 比如: User::getName
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
List<KitBaseUserTimeTb> entities = new ArrayList<>();
|
public static <T, K> List<T> distinctByArgs(List<T> items, Function<? super T, ? extends K> classifier) {
|
||||||
int total = 50;
|
return items.stream().collect(Collectors.groupingBy(classifier)).values().stream()
|
||||||
for (int i = 0; i < total; i++) {
|
.map(monitorItem -> monitorItem.iterator().next()).toList();
|
||||||
KitBaseUserTimeTb entity = new KitBaseUserTimeTb();
|
}
|
||||||
if (i % 2 == 0) {
|
|
||||||
entity.setCreateBy("odboy");
|
/**
|
||||||
} else {
|
* list转map, 保留新值 -> Map<String, User> userMap = items.stream().collect(Collectors.toMap(User::getName, user -> user)) <br> list转map, 保留旧值 -> Map<String,
|
||||||
entity.setCreateBy("admin");
|
* User> userMap = users.stream().collect(Collectors.toMap(User::getName, user -> user, (existing, replacement) -> existing));
|
||||||
}
|
*/
|
||||||
entity.setUpdateBy("admin");
|
public static void main(String[] args) {
|
||||||
entity.setCreateTime(DateTime.now().toTimestamp());
|
List<KitBaseUserTimeTb> entities = new ArrayList<>();
|
||||||
entity.setUpdateTime(DateTime.now().toTimestamp());
|
int total = 50;
|
||||||
entities.add(entity);
|
for (int i = 0; i < total; i++) {
|
||||||
}
|
KitBaseUserTimeTb entity = new KitBaseUserTimeTb();
|
||||||
List<KitBaseUserTimeTb> entities1 = KitListUtil.distinctByArgs(entities, KitBaseUserTimeTb::getCreateBy);
|
if (i % 2 == 0) {
|
||||||
System.err.println(entities1);
|
entity.setCreateBy("odboy");
|
||||||
|
} else {
|
||||||
|
entity.setCreateBy("admin");
|
||||||
|
}
|
||||||
|
entity.setUpdateBy("admin");
|
||||||
|
entity.setCreateTime(DateTime.now().toTimestamp());
|
||||||
|
entity.setUpdateTime(DateTime.now().toTimestamp());
|
||||||
|
entities.add(entity);
|
||||||
}
|
}
|
||||||
|
List<KitBaseUserTimeTb> entities1 = KitListUtil.distinctByArgs(entities, KitBaseUserTimeTb::getCreateBy);
|
||||||
|
System.err.println(entities1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-20
@@ -26,26 +26,27 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitLogFmtUtil {
|
public final class KitLogFmtUtil {
|
||||||
/**
|
|
||||||
* 替换字符串中的占位符 {} 为传入的参数
|
|
||||||
*
|
|
||||||
* @param message 消息模板
|
|
||||||
* @param args 替换占位符的参数
|
|
||||||
* @return 替换后的消息
|
|
||||||
*/
|
|
||||||
public static String format(String message, Object... args) {
|
|
||||||
if (message == null || args == null || args.length == 0) {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
// 使用 String.format 替换占位符
|
|
||||||
return String.format(message.replace("{}", "%s"), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
// 示例用法
|
* 替换字符串中的占位符 {} 为传入的参数
|
||||||
String messageTemplate = "获取dingtalk accessToken失败, code={}, message={}";
|
*
|
||||||
String formattedMessage = format(messageTemplate, 400, "Bad Request");
|
* @param message 消息模板
|
||||||
// 输出: 获取dingtalk accessToken失败, code=400, message=Bad Request
|
* @param args 替换占位符的参数
|
||||||
System.out.println(formattedMessage);
|
* @return 替换后的消息
|
||||||
|
*/
|
||||||
|
public static String format(String message, Object... args) {
|
||||||
|
if (message == null || args == null || args.length == 0) {
|
||||||
|
return message;
|
||||||
}
|
}
|
||||||
|
// 使用 String.format 替换占位符
|
||||||
|
return String.format(message.replace("{}", "%s"), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 示例用法
|
||||||
|
String messageTemplate = "获取dingtalk accessToken失败, code={}, message={}";
|
||||||
|
String formattedMessage = format(messageTemplate, 400, "Bad Request");
|
||||||
|
// 输出: 获取dingtalk accessToken失败, code=400, message=Bad Request
|
||||||
|
System.out.println(formattedMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+47
-47
@@ -33,58 +33,58 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitPageUtil extends cn.hutool.core.util.PageUtil {
|
public final class KitPageUtil extends cn.hutool.core.util.PageUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List 分页
|
* List 分页
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> softPaging(long page, long size, List<T> list) {
|
public static <T> List<T> softPaging(long page, long size, List<T> list) {
|
||||||
int pageIndex = Math.toIntExact(page - 1);
|
int pageIndex = Math.toIntExact(page - 1);
|
||||||
int fromIndex = Math.toIntExact(pageIndex * size);
|
int fromIndex = Math.toIntExact(pageIndex * size);
|
||||||
int toIndex = Math.toIntExact(pageIndex * size + size);
|
int toIndex = Math.toIntExact(pageIndex * size + size);
|
||||||
if (fromIndex > list.size()) {
|
if (fromIndex > list.size()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
} else if (toIndex >= list.size()) {
|
} else if (toIndex >= list.size()) {
|
||||||
return list.subList(fromIndex, list.size());
|
return list.subList(fromIndex, list.size());
|
||||||
} else {
|
} else {
|
||||||
return list.subList(fromIndex, toIndex);
|
return list.subList(fromIndex, toIndex);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page 数据处理
|
* Page 数据处理
|
||||||
*/
|
*/
|
||||||
public static <T> KitPageResult<T> toPage(IPage<T> page) {
|
public static <T> KitPageResult<T> toPage(IPage<T> page) {
|
||||||
return new KitPageResult<>(page.getRecords(), page.getTotal());
|
return new KitPageResult<>(page.getRecords(), page.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义分页
|
* 自定义分页
|
||||||
*/
|
*/
|
||||||
public static <T> KitPageResult<T> toPage(List<T> list) {
|
public static <T> KitPageResult<T> toPage(List<T> list) {
|
||||||
return new KitPageResult<>(list, list.size());
|
return new KitPageResult<>(list, list.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回空数据
|
* 返回空数据
|
||||||
*/
|
*/
|
||||||
public static <T> KitPageResult<T> emptyListData() {
|
public static <T> KitPageResult<T> emptyListData() {
|
||||||
return new KitPageResult<>(new ArrayList<>(), 0);
|
return new KitPageResult<>(new ArrayList<>(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回空数据
|
* 返回空数据
|
||||||
*/
|
*/
|
||||||
public static <T> KitPageResult<T> emptyData() {
|
public static <T> KitPageResult<T> emptyData() {
|
||||||
return new KitPageResult<>(null, 0);
|
return new KitPageResult<>(null, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义分页
|
* 自定义分页
|
||||||
*/
|
*/
|
||||||
public static <T> KitPageResult<T> toPage(List<T> list, long totalElements) {
|
public static <T> KitPageResult<T> toPage(List<T> list, long totalElements) {
|
||||||
return new KitPageResult<>(list, totalElements);
|
return new KitPageResult<>(list, totalElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Page<T> getCount(BaseMapper<T> baseMapper) {
|
public static <T> Page<T> getCount(BaseMapper<T> baseMapper) {
|
||||||
return new Page<>(1, baseMapper.selectCount(null));
|
return new Page<>(1, baseMapper.selectCount(null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-7
@@ -32,11 +32,12 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class KitResourceTemplateUtil {
|
public class KitResourceTemplateUtil {
|
||||||
public static String render(String moduleName, String filename, Dict params) {
|
|
||||||
String path = StrUtil.isBlank(moduleName) ? "template" : "template/" + moduleName;
|
public static String render(String moduleName, String filename, Dict params) {
|
||||||
TemplateEngine engine =
|
String path = StrUtil.isBlank(moduleName) ? "template" : "template/" + moduleName;
|
||||||
TemplateUtil.createEngine(new TemplateConfig(path, TemplateConfig.ResourceMode.CLASSPATH));
|
TemplateEngine engine =
|
||||||
Template template = engine.getTemplate(filename);
|
TemplateUtil.createEngine(new TemplateConfig(path, TemplateConfig.ResourceMode.CLASSPATH));
|
||||||
return template.render(params);
|
Template template = engine.getTemplate(filename);
|
||||||
}
|
return template.render(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-35
@@ -32,41 +32,42 @@ import org.aspectj.lang.reflect.MethodSignature;
|
|||||||
* @date 2025-05-08
|
* @date 2025-05-08
|
||||||
*/
|
*/
|
||||||
public class KitReturnValueHandleUtil {
|
public class KitReturnValueHandleUtil {
|
||||||
public static Object getDefaultValue(ProceedingJoinPoint joinPoint) {
|
|
||||||
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
|
|
||||||
Method method = signature.getMethod();
|
|
||||||
// 获取目标方法的返回值类型
|
|
||||||
Class<?> returnType = method.getReturnType();
|
|
||||||
if (returnType.isArray()) {
|
|
||||||
return getEmptyArray(returnType);
|
|
||||||
} else if (List.class.isAssignableFrom(returnType)) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
} else if (Set.class.isAssignableFrom(returnType)) {
|
|
||||||
return Collections.emptySet();
|
|
||||||
} else if (Collection.class.isAssignableFrom(returnType)) {
|
|
||||||
// 默认返回空 List
|
|
||||||
return Collections.emptyList();
|
|
||||||
} else {
|
|
||||||
// 普通对象返回 null
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static Object getDefaultValue(ProceedingJoinPoint joinPoint) {
|
||||||
* 根据数组类型返回空数组
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
*/
|
Method method = signature.getMethod();
|
||||||
private static Object getEmptyArray(Class<?> arrayType) {
|
// 获取目标方法的返回值类型
|
||||||
if (arrayType == int[].class) {
|
Class<?> returnType = method.getReturnType();
|
||||||
return new int[0];
|
if (returnType.isArray()) {
|
||||||
} else if (arrayType == long[].class) {
|
return getEmptyArray(returnType);
|
||||||
return new long[0];
|
} else if (List.class.isAssignableFrom(returnType)) {
|
||||||
} else if (arrayType == double[].class) {
|
return Collections.emptyList();
|
||||||
return new double[0];
|
} else if (Set.class.isAssignableFrom(returnType)) {
|
||||||
} else if (arrayType == String[].class) {
|
return Collections.emptySet();
|
||||||
return new String[0];
|
} else if (Collection.class.isAssignableFrom(returnType)) {
|
||||||
} else {
|
// 默认返回空 List
|
||||||
// 其他引用类型数组
|
return Collections.emptyList();
|
||||||
return java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
|
} else {
|
||||||
}
|
// 普通对象返回 null
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据数组类型返回空数组
|
||||||
|
*/
|
||||||
|
private static Object getEmptyArray(Class<?> arrayType) {
|
||||||
|
if (arrayType == int[].class) {
|
||||||
|
return new int[0];
|
||||||
|
} else if (arrayType == long[].class) {
|
||||||
|
return new long[0];
|
||||||
|
} else if (arrayType == double[].class) {
|
||||||
|
return new double[0];
|
||||||
|
} else if (arrayType == String[].class) {
|
||||||
|
return new String[0];
|
||||||
|
} else {
|
||||||
|
// 其他引用类型数组
|
||||||
|
return java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+172
-172
@@ -37,182 +37,182 @@ import org.apache.commons.codec.binary.Base64;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitRsaEncryptUtil {
|
public final class KitRsaEncryptUtil {
|
||||||
|
|
||||||
private static final String SRC = "123456";
|
private static final String SRC = "123456";
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
RsaKeyPair keyPair = generateKeyPair();
|
RsaKeyPair keyPair = generateKeyPair();
|
||||||
System.out.println("公钥:" + keyPair.getPublicKey());
|
System.out.println("公钥:" + keyPair.getPublicKey());
|
||||||
System.out.println("私钥:" + keyPair.getPrivateKey());
|
System.out.println("私钥:" + keyPair.getPrivateKey());
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
test1(keyPair);
|
test1(keyPair);
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
test2(keyPair);
|
test2(keyPair);
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公钥加密私钥解密
|
||||||
|
*/
|
||||||
|
private static void test1(RsaKeyPair keyPair) throws Exception {
|
||||||
|
System.out.println("***************** 公钥加密私钥解密开始 *****************");
|
||||||
|
String text1 = encryptByPublicKey(keyPair.getPublicKey(), KitRsaEncryptUtil.SRC);
|
||||||
|
String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
|
||||||
|
System.out.println("加密前:" + KitRsaEncryptUtil.SRC);
|
||||||
|
System.out.println("加密后:" + text1);
|
||||||
|
System.out.println("解密后:" + text2);
|
||||||
|
if (KitRsaEncryptUtil.SRC.equals(text2)) {
|
||||||
|
System.out.println("解密字符串和原始字符串一致, 解密成功");
|
||||||
|
} else {
|
||||||
|
System.out.println("解密字符串和原始字符串不一致,解密失败");
|
||||||
|
}
|
||||||
|
System.out.println("***************** 公钥加密私钥解密结束 *****************");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私钥加密公钥解密
|
||||||
|
*
|
||||||
|
* @throws Exception /
|
||||||
|
*/
|
||||||
|
private static void test2(RsaKeyPair keyPair) throws Exception {
|
||||||
|
System.out.println("***************** 私钥加密公钥解密开始 *****************");
|
||||||
|
String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), KitRsaEncryptUtil.SRC);
|
||||||
|
String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1);
|
||||||
|
System.out.println("加密前:" + KitRsaEncryptUtil.SRC);
|
||||||
|
System.out.println("加密后:" + text1);
|
||||||
|
System.out.println("解密后:" + text2);
|
||||||
|
if (KitRsaEncryptUtil.SRC.equals(text2)) {
|
||||||
|
System.out.println("解密字符串和原始字符串一致,解密成功");
|
||||||
|
} else {
|
||||||
|
System.out.println("解密字符串和原始字符串不一致,解密失败");
|
||||||
|
}
|
||||||
|
System.out.println("***************** 私钥加密公钥解密结束 *****************");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公钥解密
|
||||||
|
*
|
||||||
|
* @param publicKeyText 公钥
|
||||||
|
* @param text 待解密的信息
|
||||||
|
* @return /
|
||||||
|
* @throws Exception /
|
||||||
|
*/
|
||||||
|
public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
|
||||||
|
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
||||||
|
byte[] result = doLongerCipherFinal(Cipher.DECRYPT_MODE, cipher, Base64.decodeBase64(text));
|
||||||
|
return new String(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私钥加密
|
||||||
|
*
|
||||||
|
* @param privateKeyText 私钥
|
||||||
|
* @param text 待加密的信息
|
||||||
|
* @return /
|
||||||
|
* @throws Exception /
|
||||||
|
*/
|
||||||
|
public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
|
||||||
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
||||||
|
byte[] result = doLongerCipherFinal(Cipher.ENCRYPT_MODE, cipher, text.getBytes());
|
||||||
|
return Base64.encodeBase64String(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私钥解密
|
||||||
|
*
|
||||||
|
* @param privateKeyText 私钥
|
||||||
|
* @param text 待解密的文本
|
||||||
|
* @return /
|
||||||
|
* @throws Exception /
|
||||||
|
*/
|
||||||
|
public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
|
||||||
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
byte[] result = doLongerCipherFinal(Cipher.DECRYPT_MODE, cipher, Base64.decodeBase64(text));
|
||||||
|
return new String(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公钥加密
|
||||||
|
*
|
||||||
|
* @param publicKeyText 公钥
|
||||||
|
* @param text 待加密的文本
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
|
||||||
|
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
byte[] result = doLongerCipherFinal(Cipher.ENCRYPT_MODE, cipher, text.getBytes());
|
||||||
|
return Base64.encodeBase64String(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] doLongerCipherFinal(int opMode, Cipher cipher, byte[] source) throws Exception {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
if (opMode == Cipher.DECRYPT_MODE) {
|
||||||
|
out.write(cipher.doFinal(source));
|
||||||
|
} else {
|
||||||
|
int offset = 0;
|
||||||
|
int totalSize = source.length;
|
||||||
|
while (totalSize - offset > 0) {
|
||||||
|
int size = Math.min(cipher.getOutputSize(0) - 11, totalSize - offset);
|
||||||
|
out.write(cipher.doFinal(source, offset, size));
|
||||||
|
offset += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
return out.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建RSA密钥对
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
* @throws NoSuchAlgorithmException /
|
||||||
|
*/
|
||||||
|
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
|
||||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
keyPairGenerator.initialize(1024);
|
||||||
|
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||||
|
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
|
||||||
|
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||||||
|
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
|
||||||
|
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
|
||||||
|
return new RsaKeyPair(publicKeyString, privateKeyString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA密钥对对象
|
||||||
|
*/
|
||||||
|
public static class RsaKeyPair {
|
||||||
|
|
||||||
|
private final String publicKey;
|
||||||
|
private final String privateKey;
|
||||||
|
|
||||||
|
public RsaKeyPair(String publicKey, String privateKey) {
|
||||||
|
this.publicKey = publicKey;
|
||||||
|
this.privateKey = privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public String getPublicKey() {
|
||||||
* 公钥加密私钥解密
|
return publicKey;
|
||||||
*/
|
|
||||||
private static void test1(RsaKeyPair keyPair) throws Exception {
|
|
||||||
System.out.println("***************** 公钥加密私钥解密开始 *****************");
|
|
||||||
String text1 = encryptByPublicKey(keyPair.getPublicKey(), KitRsaEncryptUtil.SRC);
|
|
||||||
String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
|
|
||||||
System.out.println("加密前:" + KitRsaEncryptUtil.SRC);
|
|
||||||
System.out.println("加密后:" + text1);
|
|
||||||
System.out.println("解密后:" + text2);
|
|
||||||
if (KitRsaEncryptUtil.SRC.equals(text2)) {
|
|
||||||
System.out.println("解密字符串和原始字符串一致, 解密成功");
|
|
||||||
} else {
|
|
||||||
System.out.println("解密字符串和原始字符串不一致,解密失败");
|
|
||||||
}
|
|
||||||
System.out.println("***************** 公钥加密私钥解密结束 *****************");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public String getPrivateKey() {
|
||||||
* 私钥加密公钥解密
|
return privateKey;
|
||||||
*
|
|
||||||
* @throws Exception /
|
|
||||||
*/
|
|
||||||
private static void test2(RsaKeyPair keyPair) throws Exception {
|
|
||||||
System.out.println("***************** 私钥加密公钥解密开始 *****************");
|
|
||||||
String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), KitRsaEncryptUtil.SRC);
|
|
||||||
String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1);
|
|
||||||
System.out.println("加密前:" + KitRsaEncryptUtil.SRC);
|
|
||||||
System.out.println("加密后:" + text1);
|
|
||||||
System.out.println("解密后:" + text2);
|
|
||||||
if (KitRsaEncryptUtil.SRC.equals(text2)) {
|
|
||||||
System.out.println("解密字符串和原始字符串一致,解密成功");
|
|
||||||
} else {
|
|
||||||
System.out.println("解密字符串和原始字符串不一致,解密失败");
|
|
||||||
}
|
|
||||||
System.out.println("***************** 私钥加密公钥解密结束 *****************");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 公钥解密
|
|
||||||
*
|
|
||||||
* @param publicKeyText 公钥
|
|
||||||
* @param text 待解密的信息
|
|
||||||
* @return /
|
|
||||||
* @throws Exception /
|
|
||||||
*/
|
|
||||||
public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
|
|
||||||
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
||||||
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
|
|
||||||
Cipher cipher = Cipher.getInstance("RSA");
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
||||||
byte[] result = doLongerCipherFinal(Cipher.DECRYPT_MODE, cipher, Base64.decodeBase64(text));
|
|
||||||
return new String(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 私钥加密
|
|
||||||
*
|
|
||||||
* @param privateKeyText 私钥
|
|
||||||
* @param text 待加密的信息
|
|
||||||
* @return /
|
|
||||||
* @throws Exception /
|
|
||||||
*/
|
|
||||||
public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
|
|
||||||
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
||||||
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
|
|
||||||
Cipher cipher = Cipher.getInstance("RSA");
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
|
||||||
byte[] result = doLongerCipherFinal(Cipher.ENCRYPT_MODE, cipher, text.getBytes());
|
|
||||||
return Base64.encodeBase64String(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 私钥解密
|
|
||||||
*
|
|
||||||
* @param privateKeyText 私钥
|
|
||||||
* @param text 待解密的文本
|
|
||||||
* @return /
|
|
||||||
* @throws Exception /
|
|
||||||
*/
|
|
||||||
public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
|
|
||||||
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
||||||
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
|
|
||||||
Cipher cipher = Cipher.getInstance("RSA");
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
||||||
byte[] result = doLongerCipherFinal(Cipher.DECRYPT_MODE, cipher, Base64.decodeBase64(text));
|
|
||||||
return new String(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 公钥加密
|
|
||||||
*
|
|
||||||
* @param publicKeyText 公钥
|
|
||||||
* @param text 待加密的文本
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
|
|
||||||
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
||||||
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
|
|
||||||
Cipher cipher = Cipher.getInstance("RSA");
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
||||||
byte[] result = doLongerCipherFinal(Cipher.ENCRYPT_MODE, cipher, text.getBytes());
|
|
||||||
return Base64.encodeBase64String(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] doLongerCipherFinal(int opMode, Cipher cipher, byte[] source) throws Exception {
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
if (opMode == Cipher.DECRYPT_MODE) {
|
|
||||||
out.write(cipher.doFinal(source));
|
|
||||||
} else {
|
|
||||||
int offset = 0;
|
|
||||||
int totalSize = source.length;
|
|
||||||
while (totalSize - offset > 0) {
|
|
||||||
int size = Math.min(cipher.getOutputSize(0) - 11, totalSize - offset);
|
|
||||||
out.write(cipher.doFinal(source, offset, size));
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.close();
|
|
||||||
return out.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建RSA密钥对
|
|
||||||
*
|
|
||||||
* @return /
|
|
||||||
* @throws NoSuchAlgorithmException /
|
|
||||||
*/
|
|
||||||
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
|
|
||||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
|
||||||
keyPairGenerator.initialize(1024);
|
|
||||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
|
||||||
RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
|
|
||||||
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
|
|
||||||
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
|
|
||||||
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
|
|
||||||
return new RsaKeyPair(publicKeyString, privateKeyString);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* RSA密钥对对象
|
|
||||||
*/
|
|
||||||
public static class RsaKeyPair {
|
|
||||||
|
|
||||||
private final String publicKey;
|
|
||||||
private final String privateKey;
|
|
||||||
|
|
||||||
public RsaKeyPair(String publicKey, String privateKey) {
|
|
||||||
this.publicKey = publicKey;
|
|
||||||
this.privateKey = privateKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPublicKey() {
|
|
||||||
return publicKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPrivateKey() {
|
|
||||||
return privateKey;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+84
-87
@@ -29,102 +29,99 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitStringUtil {
|
public final class KitStringUtil {
|
||||||
|
|
||||||
private static final char SEPARATOR = '_';
|
private static final char SEPARATOR = '_';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 驼峰命名法工具
|
* 驼峰命名法工具
|
||||||
*
|
*
|
||||||
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world"
|
||||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
*/
|
||||||
*/
|
public static String toCamelCase(String s) {
|
||||||
public static String toCamelCase(String s) {
|
if (s == null) {
|
||||||
if (s == null) {
|
return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
s = s.toLowerCase();
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(s.length());
|
|
||||||
boolean upperCase = false;
|
|
||||||
for (int i = 0; i < s.length(); i++) {
|
|
||||||
char c = s.charAt(i);
|
|
||||||
|
|
||||||
if (c == SEPARATOR) {
|
|
||||||
upperCase = true;
|
|
||||||
} else if (upperCase) {
|
|
||||||
sb.append(Character.toUpperCase(c));
|
|
||||||
upperCase = false;
|
|
||||||
} else {
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
s = s.toLowerCase();
|
||||||
* 驼峰命名法工具
|
|
||||||
*
|
StringBuilder sb = new StringBuilder(s.length());
|
||||||
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
boolean upperCase = false;
|
||||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
for (int i = 0; i < s.length(); i++) {
|
||||||
*/
|
char c = s.charAt(i);
|
||||||
public static String toCapitalizeCamelCase(String s) {
|
|
||||||
if (s == null) {
|
if (c == SEPARATOR) {
|
||||||
return null;
|
upperCase = true;
|
||||||
}
|
} else if (upperCase) {
|
||||||
s = toCamelCase(s);
|
sb.append(Character.toUpperCase(c));
|
||||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
upperCase = false;
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return sb.toString();
|
||||||
* 驼峰命名法工具
|
}
|
||||||
*
|
|
||||||
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
|
||||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
|
||||||
*/
|
|
||||||
public static String toUnderScoreCase(String s) {
|
|
||||||
if (s == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
/**
|
||||||
boolean upperCase = false;
|
* 驼峰命名法工具
|
||||||
for (int i = 0; i < s.length(); i++) {
|
*
|
||||||
char c = s.charAt(i);
|
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world"
|
||||||
|
*/
|
||||||
|
public static String toCapitalizeCamelCase(String s) {
|
||||||
|
if (s == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
s = toCamelCase(s);
|
||||||
|
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
boolean nextUpperCase = true;
|
/**
|
||||||
|
* 驼峰命名法工具
|
||||||
if (i < (s.length() - 1)) {
|
*
|
||||||
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
* @return toCamelCase(" hello_world ") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world"
|
||||||
}
|
*/
|
||||||
|
public static String toUnderScoreCase(String s) {
|
||||||
if ((i > 0) && Character.isUpperCase(c)) {
|
if (s == null) {
|
||||||
if (!upperCase || !nextUpperCase) {
|
return null;
|
||||||
sb.append(SEPARATOR);
|
|
||||||
}
|
|
||||||
upperCase = true;
|
|
||||||
} else {
|
|
||||||
upperCase = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.append(Character.toLowerCase(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
StringBuilder sb = new StringBuilder();
|
||||||
* 获得当天是周几
|
boolean upperCase = false;
|
||||||
*/
|
for (int i = 0; i < s.length(); i++) {
|
||||||
public static String getWeekDay() {
|
char c = s.charAt(i);
|
||||||
// String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
||||||
String[] weekDays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"};
|
boolean nextUpperCase = true;
|
||||||
Calendar cal = Calendar.getInstance();
|
|
||||||
cal.setTime(new Date());
|
if (i < (s.length() - 1)) {
|
||||||
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
||||||
if (w < 0) {
|
}
|
||||||
w = 0;
|
|
||||||
|
if ((i > 0) && Character.isUpperCase(c)) {
|
||||||
|
if (!upperCase || !nextUpperCase) {
|
||||||
|
sb.append(SEPARATOR);
|
||||||
}
|
}
|
||||||
return weekDays[w];
|
upperCase = true;
|
||||||
|
} else {
|
||||||
|
upperCase = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append(Character.toLowerCase(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得当天是周几
|
||||||
|
*/
|
||||||
|
public static String getWeekDay() {
|
||||||
|
// String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||||
|
String[] weekDays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"};
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(new Date());
|
||||||
|
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
||||||
|
if (w < 0) {
|
||||||
|
w = 0;
|
||||||
|
}
|
||||||
|
return weekDays[w];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-11
@@ -27,16 +27,17 @@ import oshi.hardware.CentralProcessor;
|
|||||||
* @date 2025-08-11
|
* @date 2025-08-11
|
||||||
*/
|
*/
|
||||||
public class KitSystemUtil {
|
public class KitSystemUtil {
|
||||||
public static int getCpuCount() {
|
|
||||||
SystemInfo systemInfo = new SystemInfo();
|
|
||||||
CentralProcessor processor = systemInfo.getHardware().getProcessor();
|
|
||||||
return processor.getPhysicalProcessorCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static int getCpuCount() {
|
||||||
* 获取当前系统的用户名
|
SystemInfo systemInfo = new SystemInfo();
|
||||||
*/
|
CentralProcessor processor = systemInfo.getHardware().getProcessor();
|
||||||
public static String getCurrentUserName() {
|
return processor.getPhysicalProcessorCount();
|
||||||
return System.getProperty("user.name");
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 获取当前系统的用户名
|
||||||
|
*/
|
||||||
|
public static String getCurrentUserName() {
|
||||||
|
return System.getProperty("user.name");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-12
@@ -34,19 +34,20 @@ import lombok.experimental.UtilityClass;
|
|||||||
*/
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class KitValidUtil {
|
public final class KitValidUtil {
|
||||||
private static final Validator VALIDATOR;
|
|
||||||
|
|
||||||
static {
|
private static final Validator VALIDATOR;
|
||||||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
|
||||||
VALIDATOR = factory.getValidator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> void validate(T object) {
|
static {
|
||||||
Set<ConstraintViolation<T>> violations = VALIDATOR.validate(object, Default.class);
|
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||||
if (!violations.isEmpty()) {
|
VALIDATOR = factory.getValidator();
|
||||||
throw new BadRequestException(
|
|
||||||
violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(",")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void validate(T object) {
|
||||||
|
Set<ConstraintViolation<T>> violations = VALIDATOR.validate(object, Default.class);
|
||||||
|
if (!violations.isEmpty()) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(",")));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,25 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>cutejava</artifactId>
|
<artifactId>cutejava</artifactId>
|
||||||
<groupId>cn.odboy</groupId>
|
<groupId>cn.odboy</groupId>
|
||||||
<version>1.4.1</version>
|
<version>1.4.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>cutejava-module-system</artifactId>
|
<artifactId>cutejava-module-system</artifactId>
|
||||||
<name>核心模块</name>
|
<name>核心模块</name>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.odboy</groupId>
|
<groupId>cn.odboy</groupId>
|
||||||
<artifactId>cutejava-framework</artifactId>
|
<artifactId>cutejava-framework</artifactId>
|
||||||
<version>1.4.1</version>
|
<version>1.4.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/io.minio/minio -->
|
<!-- https://mvnrepository.com/artifact/io.minio/minio -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.minio</groupId>
|
<groupId>io.minio</groupId>
|
||||||
<artifactId>minio</artifactId>
|
<artifactId>minio</artifactId>
|
||||||
<version>8.5.17</version>
|
<version>8.5.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+31
-31
@@ -24,37 +24,37 @@ import lombok.Getter;
|
|||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum SystemCaptchaBizEnum {
|
public enum SystemCaptchaBizEnum {
|
||||||
/* 通过手机号码重置邮箱 */
|
/* 通过手机号码重置邮箱 */
|
||||||
PHONE_RESET_EMAIL_CODE("10000", "phone_reset_email_code_", "SystemEmailCaptchaTemplate.ftl",
|
PHONE_RESET_EMAIL_CODE("10000", "phone_reset_email_code_", "SystemEmailCaptchaTemplate.ftl",
|
||||||
"通过手机号码重置邮箱"), /* 通过旧邮箱重置邮箱 */
|
"通过手机号码重置邮箱"), /* 通过旧邮箱重置邮箱 */
|
||||||
EMAIL_RESET_EMAIL_CODE("10001", "email_reset_email_code_", "SystemEmailCaptchaTemplate.ftl", "通过旧邮箱重置邮箱"),
|
EMAIL_RESET_EMAIL_CODE("10001", "email_reset_email_code_", "SystemEmailCaptchaTemplate.ftl", "通过旧邮箱重置邮箱"),
|
||||||
/* 通过手机号码重置密码 */
|
/* 通过手机号码重置密码 */
|
||||||
PHONE_RESET_PWD_CODE("10002", "phone_reset_pwd_code_", "SystemEmailCaptchaTemplate.ftl", "通过手机号码重置密码"),
|
PHONE_RESET_PWD_CODE("10002", "phone_reset_pwd_code_", "SystemEmailCaptchaTemplate.ftl", "通过手机号码重置密码"),
|
||||||
/* 通过邮箱重置密码 */
|
/* 通过邮箱重置密码 */
|
||||||
EMAIL_RESET_PWD_CODE("10003", "email_reset_pwd_code_", "SystemEmailCaptchaTemplate.ftl", "通过邮箱重置密码");
|
EMAIL_RESET_PWD_CODE("10003", "email_reset_pwd_code_", "SystemEmailCaptchaTemplate.ftl", "通过邮箱重置密码");
|
||||||
/**
|
/**
|
||||||
* 业务编码
|
* 业务编码
|
||||||
*/
|
*/
|
||||||
private final String bizCode;
|
private final String bizCode;
|
||||||
/**
|
/**
|
||||||
* redis中的key
|
* redis中的key
|
||||||
*/
|
*/
|
||||||
private final String redisKey;
|
private final String redisKey;
|
||||||
/**
|
/**
|
||||||
* resources文件夹,template中的模版名称
|
* resources文件夹,template中的模版名称
|
||||||
*/
|
*/
|
||||||
private final String templateName;
|
private final String templateName;
|
||||||
/**
|
/**
|
||||||
* 业务描述
|
* 业务描述
|
||||||
*/
|
*/
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|
||||||
public static SystemCaptchaBizEnum getByBizCode(String bizCode) {
|
public static SystemCaptchaBizEnum getByBizCode(String bizCode) {
|
||||||
for (SystemCaptchaBizEnum value : SystemCaptchaBizEnum.values()) {
|
for (SystemCaptchaBizEnum value : SystemCaptchaBizEnum.values()) {
|
||||||
if (value.getBizCode().equals(bizCode)) {
|
if (value.getBizCode().equals(bizCode)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -24,19 +24,19 @@ import lombok.Getter;
|
|||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum SystemDataScopeEnum {
|
public enum SystemDataScopeEnum {
|
||||||
/* 全部的数据权限 */
|
/* 全部的数据权限 */
|
||||||
ALL("全部", "全部的数据权限"), /* 自己部门的数据权限 */
|
ALL("全部", "全部的数据权限"), /* 自己部门的数据权限 */
|
||||||
THIS_LEVEL("本级", "自己部门的数据权限"), /* 自定义的数据权限 */
|
THIS_LEVEL("本级", "自己部门的数据权限"), /* 自定义的数据权限 */
|
||||||
CUSTOMIZE("自定义", "自定义的数据权限");
|
CUSTOMIZE("自定义", "自定义的数据权限");
|
||||||
private final String value;
|
private final String value;
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|
||||||
public static SystemDataScopeEnum find(String val) {
|
public static SystemDataScopeEnum find(String val) {
|
||||||
for (SystemDataScopeEnum dataScopeEnum : SystemDataScopeEnum.values()) {
|
for (SystemDataScopeEnum dataScopeEnum : SystemDataScopeEnum.values()) {
|
||||||
if (dataScopeEnum.getValue().equals(val)) {
|
if (dataScopeEnum.getValue().equals(val)) {
|
||||||
return dataScopeEnum;
|
return dataScopeEnum;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -17,7 +17,7 @@ package cn.odboy.system.constant;
|
|||||||
|
|
||||||
public interface TransferProtocolConst {
|
public interface TransferProtocolConst {
|
||||||
|
|
||||||
String PREFIX_HTTP = "http://";
|
String PREFIX_HTTP = "http://";
|
||||||
String PREFIX_HTTPS = "https://";
|
String PREFIX_HTTPS = "https://";
|
||||||
String PREFIX_HTTPS_BAD_REQUEST = "外链必须以http://或者https://开头";
|
String PREFIX_HTTPS_BAD_REQUEST = "外链必须以http://或者https://开头";
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -21,8 +21,8 @@ import cn.odboy.system.dal.model.SystemUserLoginArgs;
|
|||||||
import cn.odboy.system.service.SystemAuthService;
|
import cn.odboy.system.service.SystemAuthService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import java.util.Map;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|||||||
+1
-1
@@ -24,9 +24,9 @@ import cn.odboy.system.service.SystemDeptService;
|
|||||||
import cn.odboy.util.KitPageUtil;
|
import cn.odboy.util.KitPageUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user