给大家转发一篇大厂的编码规范的(java版的)
虽然没在大厂任职过,不过有值得学习的内容流出来,仔细看看这都是先人门多年经验的总结,就是值得我们学习的。
声明转载的,原文https://blog.csdn.net/Daybreak1209/article/details/82378249
感觉具体是不是大厂的(错别字不少)流出来的,还是某培训机构自己编写的不重要,具体看下面吧
CodeStyle
DTO、BO、DO、VO、AO、PO
除外get,list,count,save/insert,remove/delete,update
String key="create"+tradeId
equals
方法;Integer -128~127
中可以使用 ==
,jdk5+自动拆装箱。超过此范围一律使用equals
判断static
类型,如果非要这么做,那么请加锁使用,或者用DateUtils工具类Integer.MAX_VALUE
,可能会堆积大量的请求,从而导致OOM。Integer.MAX_VALUE
,可能会创建大量的线程,从而导致OOM。Exception
MySQL
id、create_time,update_time
3字段,且更新时必须更新update字段Unit Test
贴出来是给大家参考的,有可鉴之处,一种约束,没要求必须去这样做,是希望您能去遵循规范。
否则您要写出像下面这种代码,
@RequestMapping(value = "/Getlist")
@ResponseBody
public ModelMap Getlist(HttpServletRequest request,
@RequestParam(value = Constants.DEFAULT_CURRPAGE_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_PAGE_START) Integer start,
@RequestParam(value = Constants.DEFAULT_PAGE_SIZE_MODEL_KEY, required = false, defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer limit) {
ModelMap mm = new ModelMap();
int subWayCount = 0;
List<SubWayDrivingStatus> statusList = new ArrayList<>();
String status = request.getParameter("subwayStatus");
String cityId = request.getParameter("citys");
String routeId = request.getParameter("routeId");
List<SubWayDrivingStatus> cacheList = new ArrayList<>();
List<SubWayDrivingStatus> resultList = new ArrayList<>();//???
Jedis jedis = redisManager.getJedis();
if (jedis.isConnected() && jedis.exists(DRIVING_STATE)) {
Map<String, String[]> userCityRoutes = getUserCityRoute();
String json = jedis.get(DRIVING_STATE);
cacheList = JsonUtil.toList(json, SubWayDrivingStatus.class);
// 要筛选用户可以访问的数据
String[] routes = userCityRoutes.get(ROUTES);
String[] citys = userCityRoutes.get(CITYS);
if (routes != null && routes.length > 0) {
for(SubWayDrivingStatus sds : cacheList){
for(String route : routes){
if(route.equals(sds.getRouteId())){
resultList.add(sds);
// continue;
}
}
}
} else if (citys != null && citys.length > 0) {
for (SubWayDrivingStatus sds : cacheList) {
for (String city : citys) {
if (city.equals(sds.getCitys())) {
resultList.add(sds);
// continue;
}
}
}
} else {
resultList = cacheList;//TODO ????什么逻辑???
}
// 要筛选用户下拉框
if (isNotEmpty(routeId)) {
Iterator<SubWayDrivingStatus> it = resultList.iterator();
while (it.hasNext()) {
SubWayDrivingStatus sds = it.next();
if (!sds.getRouteId().equals(routeId)) {
it.remove();
}
}
} else if (isNotEmpty(cityId)) {
Iterator<SubWayDrivingStatus> it = resultList.iterator();
while (it.hasNext()) {
SubWayDrivingStatus sds = it.next();
if (!sds.getCitys().equals(cityId)) {
it.remove();
}
}
}
subWayCount = resultList.size();
logger.debug("user check the status: "+status);
statusList = getShowList(status, resultList);
sort(statusList);
int showSubWayCount = Math.min(statusList.size(), subWayCount);
mm.addAttribute(Constants.DEFAULT_RECORD_MODEL_KEY,
statusList.subList((start - 1) * limit, Math.min(start * limit, showSubWayCount)));
mm.addAttribute(Constants.DEFAULT_COUNT_MODEL_KEY, showSubWayCount);
mm.addAttribute("subWayCount", Math.min(statusList.size(), subWayCount));
mm.addAttribute("subWayOnline", jedis.get(DRIVING_STATE_ONLINE));
mm.addAttribute("subWayOffline", jedis.get(DRIVING_STATE_OFFLINE));
mm.addAttribute("subWayOnFault", jedis.get(DRIVING_STATE_FAULT));
mm.addAttribute(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);
} else {
mm.addAttribute(Constants.DEFAULT_SUCCESS_KEY, Boolean.FALSE);
}
if (jedis != null) {
jedis.close();
}
return mm;
}
还有下面这样的
public void getlist() {
Integer subWayCount = 0;// 列车总量
SubWayStatusCount ssc = new SubWayStatusCount();
try {
Set<String> faults = getAllFaultFromCache();
List<SubWayInfo> subwayListAll = getSubwayListAll();//取所有列车数据
subWayCount = subwayListAll.size();
if(subWayCount <= 0) {
return ;
}
// 取方法名集合
List<String> fieldSets = getFieldNamesFromSubWayDrivingStatus();
List<SubWayDrivingStatus> statusList = new ArrayList<>();
SubWayDrivingStatus sds = null;
BigDataParam bigParam = new BigDataParam();
SpecialFieldCache spc = SpecialFieldCache.getInstance();
for (SubWayInfo subway : subwayListAll) {
sds = initSubWayDrivingStatus(subway);
if(sds == null) {
continue;
}
Map<String, Map<String, String>> specialFieldCache = spc.findValue(subway.getCitys(), subway.getRouteId());
Map<String, String> specialfieldMap = spc.findField(subway.getCitys(), subway.getRouteId());
JSONObject json2 = getRealTimeDatas(bigParam, subway, specialFieldCache);//取实时数据
if (json2 == null || json2.isEmpty() || json2.isNullObject()) {
setSubWayStatus(sds, subway.getId(), ssc);// 无实时数据,表明离线
} else {// 有数据
if (specialFieldCache.values() != null && !specialFieldCache.isEmpty()) {
Iterator<String> it = specialFieldCache.keySet().iterator();
String itemValue = "";
String dataCoding = "";
Field field = null;
while (it.hasNext()) {
dataCoding = it.next();// 指标代码
itemValue = specialfieldMap.get(dataCoding);
if (!fieldSets.contains(itemValue)) {
continue;
}
try {// 过滤出所需要的目标数据
field = sds.getClass().getDeclaredField(itemValue);
field.setAccessible(true);
String value = "";
for (Iterator<?> iter = json2.keys(); iter.hasNext();) {
String key = iter.next().toString();// 时间序列
JSONObject json = JSONObject.fromObject(json2.getString(key));
if (!json.isNullObject() && !json.isEmpty()) {
sds.setUpdateTime(key);
if (json.containsKey(dataCoding)) {
value = json.get(dataCoding).toString();
try {
field.set(sds, specialCodeShowValue(
specialFieldCache.get(dataCoding), value));
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
setSubWayStatus(sds, subway.getId(), ssc);
} else {
setSubWayStatus(sds, subway.getId(), ssc);
}
}//处理故障数据
if (faults.contains(keyOfFaultTrain(sds))) {
sds.setFaults("有故障");
ssc.setSubWayOnFault(ssc.getSubWayOnFault() + 1);
} else {
sds.setFaults("无故障");
}
statusList.add(sds);
}
Jedis jedis = redisManager.getJedis();
jedis.set(DRIVING_STATE, JsonUtil.toJson(statusList));
jedis.set(DRIVING_STATE_COUNT, subWayCount.toString());
jedis.set(DRIVING_STATE_ONLINE, String.valueOf(ssc.getSubWayOnline()));
jedis.set(DRIVING_STATE_OFFLINE, String.valueOf(ssc.getSubWayOffline()));
jedis.set(DRIVING_STATE_FAULT, String.valueOf(ssc.getSubWayOnFault()));
if (jedis != null) {
jedis.close();
}
fieldSets.clear();
faults.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
你会忍不住的说一声,WCNMLGB