開發小點
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());}