提高java編碼效率的常用但容易忘記的方法
1.將集合分割成逗號分割的字串
List<String> zoneIds= Lists.newArrayList(); for(PropertyOrderDeptConfResp propertyOrderDeptConfResp :propertyOrderDeptConfRespList2){ zoneIds.add(propertyOrderDeptConfResp.getZoneId()); } zoneIdsString = String.join(",",zoneIds);
2.集合裡是否包含某個字串
list.contains(),返回值為布林值
3.將逗號分割的字串變成集合,並去除重複的字串
List<String> zoneIds =Lists.newArrayList();
if(StringUtils.isNotEmpty(zoneIdsString)){
zoneIds =Arrays.asList((zoneIdsString.split(",")));
zoneIds = zoneIds.stream().distinct().collect(toList());
}
4.判斷一個字串是否為null並且不為空
StringUtil.isNotEmpty(字串)
5.將一個物件轉換成另一個物件,物件裡面的引數要差不多相同,三種方法
如:
第一種:
public SettlementRecordResp findBySettlementRecordId(String id) { SettlementRecordQueryModel settlementRecordQueryModel = new SettlementRecordQueryModel(); settlementRecordQueryModel.setId(id); SettlementRecordDo settlementRecordDo = settlementRecordBusiness.getDetails(settlementRecordQueryModel); return BaseConverter.doToResp(settlementRecordDo,SettlementRecordResp.class); }
settlementRecordDo和SettlementRecordResp引數差不多
BaseConverter.doToResp()實現方法為:
public static <T> T doToResp(Object obj, Class<T> c) { if (null == obj) { return null; } String json = JsonUtils.toJson(obj); T t = JsonUtils.toObject(json, c); return t; }
第二種:
我們也可以使用BeanUtils.copyProperties方法進行轉換,如:
BeanUtils.copyProperties(meterFeeApportionReq, meterFeeApportionEntity);
將meterFeeApportionReq物件裡面的引數值拷貝到meterFeeApportionEntity中
第三種:
A和B做轉換,A必須是C的子類,這樣轉換:
QueryRequestConverter.converQueryReqToQueryModel(serviceComplaintReportQueryReq,new CommunityServiceComplaintReportQueryModel())
其中
CommunityServiceComplaintReportQueryModel要是
BaseQueryModel的子類,converQueryReqToQueryModel實現方法如下:
private static Class queryReqClass; static { try { queryReqClass = Class.forName("com.mobcb.platform.service.common.dto.BaseQueryReq"); } catch(ClassNotFoundException e) { //log.error("find baseQueryReq class failed.", e); } }
public static <T extends BaseQueryModel> T converQueryReqToQueryModel(Object queryReq, T queryModel) { if (queryReq.getClass().getSuperclass().equals(queryReqClass)) { BeanUtils.copyProperties(queryReq, queryModel); return queryModel; } else { throw new IllegalArgumentException( "queryReq not baseQueryReq type, it is " + queryReq.getClass().getCanonicalName()); } }
6.在linux環境中,有時候和第三方系統對接,伺服器在內網當中,看介面通不通
curl http://173.16.23.37:11111/api/v3/manageme/property/handleStatusUp ------------------看內網環境通不通
檢視linux連結的方式有如下幾種,具體啥區別大家可以去百度下哈:
命令檢視連結:curl,wget ,telnet
在linux中可以用get和post直接除錯,具體方法如下,checksession:false是有些介面需要額外帶的請求頭
用linux進行get請求:curl -H "checksession:false" http://192.168.0.31:11112/api/v3/repair/management/property/AjHXhpBOCgEOJj7M?rnd=xsxsxaswwww
post請求:
curl -d "param1=value1¶m2=value2" "http://www.baidu.com"
在linux中通過程序號檢視埠:
netstat -tunlp | grep “程序號/埠號”7.查詢sql,引數在集合裡和不包含
SELECT COUNT(id) FROM mob_community.community_repair_management WHERE ( check_status = 1 and zone_id in ( "AhLJXuN8HPeHE88R","AhNY6P9saegguju8" ) and accept_department_id <> "AjB247pY51dwkP3G" or accept_department_id is null )
不包含AjB247pY51dwkP3G