〇×〇站

个人博客,佛系更新
UpdateUtil
  • 代码
  • 2026-03-03

    UpdateEntityDao.class: import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.UpdateProvider; @Mapper public interface UpdateEntityDao { @UpdateProvider(type = UpdateEntityProvider.class, method = "updateEntity") int updateEntity(UpdateEntityReq<? extends IEntity> updateEntityReq); } UpdateEntityProvider.class: import cn.hutool.core.util.ReflectUtil; import org.apache.ibatis.jdbc.SQL; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class UpdateEntityProvider { public <T extends IEntity> String updateEntity(UpdateEntityReq<T> updateEntityReq) { if (ObjectUtils.isEmpty(updateEntityReq.getEntity())) { throw new IllegalArgumentException("entity不能为空"); } if(ObjectUtils.isEmpty(updateEntityReq.getUpdateFields())) { throw new IllegalArgumentException("updateFields不能为空"); } if(StringUtils.isEmpty(updateEntityReq.getIdName())) { throw new IllegalArgumentException("idName不能为空"); } IEntity entity = updateEntityReq.getEntity(); Class<?> clazz = updateEntityReq.getEntity().getClass(); if(clazz.isAnonymousClass() || clazz.isSynthetic()) { clazz = clazz.getSuperclass(); } String tableName = clazz.getSimpleName(); Field[] fields = ReflectUtil.getFields(clazz); Set<String> fieldNames = Arrays.stream(fields).map(Field::getName).collect(Collectors.toSet()); for (String field : updateEntityReq.getUpdateFields()) { if (!fieldNames.contains(field)) { throw new IllegalArgumentException(String.format("updateFields中的字段无效:%s", field)); } } if (!fieldNames.contains(updateEntityReq.getIdName())) { throw new IllegalArgumentException("idName字段无效"); } return new SQL() {{ String where = null; List<String> setList = new ArrayList<>(); for (Field field : fields) { String fieldName = field.getName(); Object value = ReflectUtil.getFieldValue(entity, field); // idName默认为id if(fieldName.equals(updateEntityReq.getIdName())) { where = String.format("%s = #{entity.%s}", fieldName, fieldName); } if (!updateEntityReq.getUpdateFields().contains(fieldName)) { continue; } // isFilterNull默认为ture if (updateEntityReq.isFilterNull() && value == null) { continue; } // isFilterEmptyStrings默认为false if (updateEntityReq.isFilterEmptyStrings() && value instanceof String && StringUtils.isEmpty(value)) { continue; } setList.add(String.format("%s = #{entity.%s}", fieldName, fieldName)); } if(StringUtils.isEmpty(tableName)) { throw new IllegalArgumentException("表名不能为空"); } if(ObjectUtils.isEmpty(setList)) { throw new IllegalArgumentException("未生成更新字段"); } if(StringUtils.isEmpty(where)) { throw new IllegalArgumentException("未生成查询条件"); } UPDATE(tableName); setList.forEach(this::SET); WHERE(where); }}.toString(); } } UpdateEntityReq.class: import lombok.Builder; import lombok.Data; import lombok.NonNull; import java.util.List; @Data @Builder public class UpdateEntityReq<T extends IEntity> { @NonNull private T entity; @NonNull private List<String> updateFields; @Builder.Default private String idName = "id"; @Builder.Default private boolean filterNull = true; @Builder.Default private boolean filterEmptyStrings = false; } UpdateUtil.class: import cn.hutool.core.lang.func.Func1; import cn.hutool.core.lang.func.LambdaUtil; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public final class UpdateUtil { private UpdateUtil() {} public static <T extends IEntity> UpdateStep<T> update(T entity) { return new UpdateStep<>(entity); } public static class UpdateStep<T extends IEntity> { private final T entity; public UpdateStep(T entity) { this.entity = entity; } @SafeVarargs public final SetStep<T> set(Func1<T, ?>...updateFields) { return new SetStep<>(entity, toList(updateFields), false, false); } @SafeVarargs public final SetStep<T> setNotNull(Func1<T, ?>...updateFields) { return new SetStep<>(entity, toList(updateFields), true, false); } @SafeVarargs public final SetStep<T> setNotBlank(Func1<T, ?>...updateFields) { return new SetStep<>(entity, toList(updateFields), true, true); } @SafeVarargs private static <T extends IEntity> List<String> toList(Func1<T, ?>...updateFields) { return Arrays.stream(updateFields).map(LambdaUtil::getFieldName).collect(Collectors.toList()); } } public static class SetStep<T extends IEntity> { private final T entity; private final List<String> updateFields; private final boolean filterNull; private final boolean filterEmptyStrings; public SetStep(T entity, List<String> updateFields, boolean filterNull, boolean filterEmptyStrings) { this.entity = entity; this.updateFields = updateFields; this.filterNull = filterNull; this.filterEmptyStrings = filterEmptyStrings; } public ExecuteStep<T> whereBy(Func1<T, ?> idName) { UpdateEntityReq<T> updateEntityReq = UpdateEntityReq.<T>builder() .entity(entity) .updateFields(updateFields) .idName(LambdaUtil.getFieldName(idName)) .filterNull(filterNull) .filterEmptyStrings(filterEmptyStrings) .build(); return new ExecuteStep<>(updateEntityReq); } public ExecuteStep<T> whereById() { UpdateEntityReq<T> updateEntityReq = UpdateEntityReq.<T>builder() .entity(entity) .updateFields(updateFields) .idName("id") .filterNull(filterNull) .filterEmptyStrings(filterEmptyStrings) .build(); return new ExecuteStep<>(updateEntityReq); } } public static class ExecuteStep<T extends IEntity> { private final UpdateEntityReq<T> updateEntityReq; public ExecuteStep(UpdateEntityReq<T> updateEntityReq) { this.updateEntityReq = updateEntityReq; } public int execute(UpdateEntityDao updateEntityDao) { if(updateEntityDao == null) { throw new IllegalArgumentException("updateEntityDao不能为空"); } return updateEntityDao.updateEntity(updateEntityReq); } } }
  • 0
  • 0
  • 0
  • Custom validation starter for Spring Boot
  • 代码
  • 2026-03-03

    Custom validation starter for Spring Boot
  • 7
  • 0
  • 0
  • mysql字符集排序规则
  • 代码
  • 2024-07-19

    mysql字符集排序规则
  • 28
  • 0
  • 0
  • Mysql中使用LIKE语句查询%_
  • 代码
  • 2024-04-16

    使用 "ESCAPE" 关键字定义转义符号如 "\\"
  • 43
  • 0
  • 0
  • Java以字素单位获取字符串长度
  • 代码
  • 2024-04-10

    Java以字素单位获取字符串长度
  • 27
  • 0
  • 0
  • 分享10个高级sql写法
  • 代码
  • 2023-08-09

    本文主要介绍博主在以往开发过程中,对于不同业务所对应的 sql 写法进行归纳总结而来。进而分享给大家。
  • 98
  • 0
  • 5
  • Mysql中符号@的作用
  • 代码
  • 2023-08-01

    Mysql中符号@的作用
  • 45
  • 0
  • 0
  • MYSQL中的FIND_IN_SET()函数的使用
  • 代码
  • 2023-08-01

    MYSQL中的FIND_IN_SET()函数的使用
  • 17
  • 0
  • 0
  • 如何制作srt字幕文件
  • test
  • 2023-07-06

    如何制作srt字幕文件
  • 13
  • 0
  • 0
  • MySQL多列IN写法
  • 代码
  • 2023-07-04

    MySQL多列IN写法
  • 18
  • 0
  • 0
  • 双拼学习
  • 闲扯
  • 2023-06-17

    双拼学习
  • 45
  • 0
  • 3
  • sourceTree闪退的解决方法
  • 代码
  • 2023-06-16

    sourceTree闪退的解决方法
  • 63
  • 0
  • 0
  • encodeURI编码和encodeURIComponent编码
  • 闲扯
  • 2023-05-12

    encodeURI编码和encodeURIComponent编码
  • 55
  • 0
  • 0
  • 关于我删掉了七牛云
  • 闲扯
  • 2023-05-01

    关于我删掉了七牛云
  • 116
  • 1
  • 6
  • Vue解析markdown,并将其展示到页面(代码高亮 + 显示代码行号)
  • 代码
  • 2023-04-27

    Vue解析markdown,并将其展示到页面(代码高亮 + 显示代码行号),具体步骤如下:
  • 444
  • 0
  • 0
  • Vue配置marked链接添加target="_blank"
  • 代码
  • 2023-04-27

    Vue配置marked链接添加target="_blank"
  • 37
  • 0
  • 0
  • VUE 实现复制内容到剪贴板功能
  • 代码
  • 2023-04-27

    注: 依赖第三方插件 clipboard
  • 309
  • 0
  • 0
  • vue中 满屏雪花降落效果
  • 代码
  • 2023-04-27

    vue中 满屏雪花降落效果
  • 108
  • 0
  • 0
  • vue中实现回到顶部功能
  • 代码
  • 2023-04-27

    下面直接看代码:(不清楚的看对应注释)
  • 29
  • 0
  • 0
  • 网易云音乐直链提取
  • 闲扯
  • 2023-04-27

    网易云音乐歌曲的id有两种方式可以获得:
  • 2518
  • 0
  • 0
  • AUDIO标签详细解读
  • 代码
  • 2023-04-27

    AUDIO标签详细解读
  • 102
  • 0
  • 0
  • 通过ip地址获取省份城市
  • 代码
  • 2023-04-07

    ip2region 优点:免费,可离线运行,准确查询全球城市信息,支持多种客户端,包括Java
  • 1155
  • 2
  • 1
  • 前后端分离获取IP地址
  • 代码
  • 2023-04-06

    前后端分离获取IP地址
  • 378
  • 2
  • 1
  • mysql分组排序并取各分组前几个数据
  • 代码
  • 2023-03-24

    在开发过程中,会遇到分组后取每一组排名前几的信息,具体实现的sql如下:
  • 976
  • 2
  • 12
  • vue使用axios调用接口
  • 代码
  • 2023-03-23

    vue本身不支持ajax接口的请求,所以在vue中经常使用axios这个接口请求工具,这个axios也是vue官方推荐的库。
  • 316
  • 2
  • 1