开发小点
1.Req注解
@EqualsAndHashCode(callSuper = true)
@Data
public class BillSituationReq extends BillQueryReq {/*** Whether to display the ring ratio, default is not displayed*/@ApiModelProperty("Whether to Display YoY Comparison")private Boolean showDiffRate;
}
@EqualsAndHashCode(callSuper = true)
用于需要依赖父类字段生成hashcode
2.本地缓存
本地的缓存定义,初始化及其调用
@Component
public class TestCache{private static final int DEFAULT_EXPIRE_MINUTE = 1;private static LoadingCache<Long/*id*/, Optional<Long/*info*/>>infoIdMapCache;@PostConstructpublic void init() {infoIdMapCache = buildLoadingCache(this::getInfoById);}//构建缓存private <K, V> LoadingCache<K, Optional<V>> buildLoadingCache(Function<K, V> get) {return buildLoadingCache(get, DEFAULT_EXPIRE_MINUTE, null);}private <K, V> LoadingCache<K, Optional<V>> buildLoadingCache(Function<K, V> get, Integer expireMinute, Consumer<V> check) {return CacheBuilder.newBuilder().maximumSize(Integer.MAX_VALUE).expireAfterWrite(expireMinute, TimeUnit.MINUTES).build(new CacheLoader<K, Optional<V>>() {@Override@ParametersAreNonnullByDefaultpublic Optional<V> load(K key) {Optional<V> cacheValue = Optional.ofNullable(get.apply(key));if (check != null) {cacheValue.ifPresent(check);}return cacheValue;}});}//查缓存public Long getDomainIdByProcessId(Long processId) {return infoIdMapCache.getUnchecked(processId).orElse(null);}//业务查询private Long getInfoById(Long id) {return null;}
}
3.安全遍历处理集合元素
Iterator<MultiBizNode> it = nodes.iterator();
while (it.hasNext()) {MultiBizNode node = it.next();// 处理当前节点...
}
- 工具选择:使用
Iterator
而非for-each
,因为需要在遍历时安全删除元素。
4.Map工具类s
//取出类型为map的value
Map detail = MapUtils.getMap(result, "audit_record_detail");
//判断map不为null且非空
boolean validDetail = MapUtils.isNotEmpty(detail);
5.sql in查询
public List<ResponseVO> queryInfoPartition(Request request) {//依据配置好的分页查limit分组,进行分批查List<Long> idList = request.getIdList();List<List<Long>> partitonList = Lists.partition(idList, 50);List<Info> infoList = new ArrayList<>();for (List<Long> partition : partitonList) {List<Info> infos = mapper.queryInfos(parition);infoList.addAll(infos);}Map<Long, Info> collect = infoList.stream().collect(Collectors.toMap(Info::getId, Function.identity()));return request.getIdList().stream().map(id -> {//按照原始顺序组装数据返回Info info = collect.get(id);ResponseVO vo = new ResponseVO();ResponseVO.setId(id);if (Objects.nonNull(info)) {vo.setInfo(info);}return vo;}).collect(Collectors.toList());}