JAVA基础知识总结

Posted alicelin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础知识总结相关的知识,希望对你有一定的参考价值。

一、引言

      从测试转JAVA开发已经将近快两年了,正好做项目的时候,有个功能,几乎囊括了所有基础知识点,所以把这段自己写的代码记录下来,算是反馈学习成果。

 

二、代码

     

技术分享图片
  1 public class UdnsAclCommitServiceImpl implements IUdnsAclCommitService
  2 {
  3     //创建LOGGER日志以供查询
  4     private static final Logger LOG = Logger.getLogger(UdnsAclCommitServiceImpl.class);
  5 
  6     @Autowired
  7     private UdnsAclCommitDao dao;
  8 
  9     //db数据库
 10     @Autowired
 11     private UdnsAclGroupDao aclGroupDao;
 12 
 13     //db数据库配置表
 14     @Autowired
 15     private UdnsAclDbDDao dbDDao;
 16 
 17     //固定列和动态列
 18     @Autowired
 19     private UdnsAclColDao aclColDao;
 20 
 21     //固定列和动态列配置表
 22     @Autowired
 23     private UdnsAclColDDao aclColDDao;
 24 
 25     //本省内容ACL
 26     @Autowired
 27     private UdnsAclCtDao aclCtDao;
 28 
 29     @Autowired
 30     private UdnsAclGroupDDao aclCtDDao;
 31 
 32     //获取menu.txt的路径
 33     @Value("${menu.filepath}")
 34     private String menupath;
 35 
 36     //获取submenu.txt的路径
 37     @Value("${submenu.filepath}")
 38     private String submenupath;
 39 
 40     /**
 41      * 变量
 42      */
 43     //1.定义储存消息的变量
 44     private String resultMessage;
 45 
 46     /**
 47      * 常量:
 48      */
 49     //设置UDNS_ACL_COMMIT表中status的值
 50     private static final String ACLINVAILDSTATUS = "未生效";
 51 
 52     private static final String ACLSTATUS = "生效";
 53 
 54     //增加静态常量空格
 55     private static final String NAMESPACE = " ";
 56 
 57     //增加静态常量0和1
 58     private static final String ZERO = "0";
 59 
 60     private static final String ONE = "1";
 61 
 62     //对固定列进行判断
 63     String[] yracl_yracl = { "DIRECTIN", "CTTIN", "CMCCIN" };
 64 
 65     String[] yracl_gsacl = { "IDC", "CACHE", "CDN", "SELFCMCC" };
 66 
 67     List<String> yracl_yraclList = Arrays.asList(yracl_yracl);
 68 
 69     List<String> yracl_gsaclList = Arrays.asList(yracl_gsacl);
 70 
 71     /**
 72      * 写方法
 73      */
 74 
 75     //数据库ACL生效,要同步修改menu.txt文件的内容
 76     @Override
 77     public boolean updateMenuTxt()
 78     {
 79         //1.检查menu.txt是否创建
 80         createConf(menupath);
 81 
 82         //2.获取表UDNS_ACL_GROUP中的全部的数据
 83         Map<String, Object> params = new HashMap<String, Object>();
 84         List<UdnsAclGroup> UdnsAclGroupList = aclGroupDao.query4List(params);
 85 
 86         //3.写入配置文件中;
 87         BufferedWriter bw = null;
 88         try
 89         {
 90             bw = new BufferedWriter(new FileWriter(menupath));
 91             for (UdnsAclGroup udnsAclGroup : UdnsAclGroupList)
 92             {
 93                 bw.write(udnsAclGroup.getGname() + "=" + udnsAclGroup.getGcname());
 94                 bw.newLine();
 95                 bw.flush();
 96             }
 97 
 98         }
 99         catch (IOException e)
100         {
101             LOG.error(e);
102         }
103         finally
104         {
105             if (bw != null)
106             {
107                 try
108                 {
109                     bw.close();
110                 }
111                 catch (IOException e)
112                 {
113                     LOG.error(e);
114                 }
115             }
116         }
117 
118         return true;
119     }
120 
121     //本省内容ACL配置生效,要同步修改submenu.txt文件的内容。
122     @Override
123     public boolean updateSubMenuTxt()
124     {
125         //1.检查submenu.txt是否创建
126         createConf(submenupath);
127 
128         //2.获取表UDNS_ACL_CT中的全部数据
129         Map<String, Object> params = new HashMap<String, Object>();
130         List<UdnsAclCt> udnsAclCtList = aclCtDao.query4List(params);
131 
132         //3.写入配置文件中;
133         BufferedWriter bw = null;
134         try
135         {
136             bw = new BufferedWriter(new FileWriter(submenupath));
137 
138             for (UdnsAclCt udnsAclCt : udnsAclCtList)
139             {
140                 bw.write(udnsAclCt.getCid() + "=" + udnsAclCt.getCtcname());
141                 bw.newLine();
142                 bw.flush();
143             }
144 
145         }
146         catch (IOException e)
147         {
148             LOG.error(e);
149         }
150         finally
151         {
152             if (bw != null)
153             {
154                 try
155                 {
156                     bw.close();
157                 }
158                 catch (IOException e)
159                 {
160                     LOG.error(e);
161                 }
162             }
163         }
164 
165         return true;
166     }
167 
168     @Override
169     public Map<String, Object> checkAndCreateConfs()
170     {
171         Map<String, Object> result = new HashMap<String, Object>();
172 
173         List<String> checkIpsegExist = checkIpsegExist();
174         if (!checkIpsegExist.isEmpty())
175         {
176             result.put("message", checkIpsegExist);
177         }
178         else
179         {
180             boolean createConfs = createConfs();
181             result.put("status", createConfs);
182         }
183 
184         return result;
185     }
186 
187     /**
188      * 检查四种类型配置的IPSEG是否存在
189      *
190      * @return
191      */
192 
193     public List<String> checkIpsegExist()
194     {
195         List<String> list = new ArrayList<String>();
196         //1.获取UDNS_ACL_COMMIT表中状态为“未生效”的记录
197         Map<String, Object> params = new HashMap<String, Object>();
198         params.put("status", ACLINVAILDSTATUS);
199         List<UdnsAclCommit> invaildAclList = dao.query4List(params);
200 
201         //2.遍历invaildAclList,逐个生成文件
202         for (UdnsAclCommit acl : invaildAclList)
203         {
204             switch (acl.getAcltype())
205             {
206             case "数据库ACL":
207                 list.addAll(checkDbIpseg());
208                 break;
209             case "固定列ACL":
210                 list.addAll(checkFixedIpseg());
211                 break;
212             case "动态列ACL":
213                 list.addAll(checkNonFixedIpseg());
214                 break;
215             case "本省内容ACL":
216                 list.addAll(checkAclCtIpseg());
217                 break;
218             default:
219                 break;
220             }
221         }
222 
223         return list;
224     }
225 
226     private List<String> checkAclCtIpseg()
227     {
228         List<String> list = new ArrayList<String>();
229 
230         //1.1.获取表UDNS_ACL_CT中的全部的数据
231         Map<String, Object> params = new HashMap<String, Object>();
232         List<UdnsAclCt> udnsAclCt4List = aclCtDao.query4List(params);
233 
234         for (UdnsAclCt udnsAclCt : udnsAclCt4List)
235         {
236             if (checkNraclIpsegExist(udnsAclCt.getCtid()) == 0)
237             {
238                 list.add("本省内容ACL对应的  " + udnsAclCt.getCtname() + " 没有配置相应的IP段,无法进行配置生效!");
239             }
240         }
241 
242         return list;
243     }
244 
245     private List<String> checkNonFixedIpseg()
246     {
247         List<String> list = new ArrayList<String>();
248 
249         //1.1.获取表UDNS_ACL_COL中的全部的数据
250         Map<String, Object> params = new HashMap<String, Object>();
251         params.put("coltype", ONE);
252         List<UdnsAclCol> udnsAclColList = aclColDao.query4List(params);
253 
254         for (UdnsAclCol udnsAclCol : udnsAclColList)
255         {
256             if (CheckYraclIpsegExist(udnsAclCol.getCid()) == 0)
257             {
258                 list.add("动态列ACL对应的  " + udnsAclCol.getColname() + " 没有配置相应的IP段,无法进行配置生效!");
259             }
260         }
261 
262         return list;
263     }
264 
265     private List<String> checkFixedIpseg()
266     {
267         List<String> list = new ArrayList<String>();
268 
269         //1.1.获取表UDNS_ACL_COL中的全部的数据
270         Map<String, Object> params = new HashMap<String, Object>();
271         params.put("coltype", ZERO);
272         List<UdnsAclCol> udnsAclColList = aclColDao.query4List(params);
273 
274         for (UdnsAclCol udnsAclCol : udnsAclColList)
275         {
276             if (CheckYraclIpsegExist(udnsAclCol.getCid()) == 0)
277             {
278                 list.add("固定列ACL对应的  " + udnsAclCol.getColname() + " 没有配置相应的IP段,无法进行配置生效!");
279             }
280         }
281         return list;
282     }
283 
284     private List<String> checkDbIpseg()
285     {
286         List<String> list = new ArrayList<String>();
287         //1.1.获取表UDNS_ACL_GROUP中的全部的数据
288         Map<String, Object> params = new HashMap<String, Object>();
289         List<UdnsAclGroup> UdnsAclGroupList = aclGroupDao.query4List(params);
290 
291         for (UdnsAclGroup udnsAclGroup : UdnsAclGroupList)
292         {
293             if (checkDbIpsegExist(udnsAclGroup.getGid()) == 0)
294             {
295                 list.add("db库ACL对应的  " + udnsAclGroup.getGname() + " 没有配置相应的IP段,无法进行配置生效!");
296             }
297         }
298 
299         return list;
300     }
301 
302     /**
303      * 创建配置文件方法
304      *
305      * @return
306      */
307 
308     public boolean createConfs()
309     {
310         boolean createSuccess = false;
311 
312         //1.获取UDNS_ACL_COMMIT表中状态为“未生效”的记录
313         Map<String, Object> params = new HashMap<String, Object>();
314         params.put("status", ACLINVAILDSTATUS);
315         List<UdnsAclCommit> invaildAclList = dao.query4List(params);
316 
317         //2.遍历invaildAclList,逐个生成文件
318         for (UdnsAclCommit acl : invaildAclList)
319         {
320             switch (acl.getAcltype())
321             {
322 
323             case "数据库ACL":
324                 createSuccess = createDbConf(acl.getFilepath());
325                 updateAclCommit(acl.getAcltype());
326                 break;
327             case "固定列ACL":
328                 createSuccess = createYraclConf(acl.getFilepath());
329                 updateAclCommit(acl.getAcltype());
330                 break;
331             case "动态列ACL":
332                 createSuccess = createGsaclConf(acl.getFilepath());
333                 updateAclCommit(acl.getAcltype());
334                 break;
335             case "本省内容ACL":
336                 createSuccess = createNraclConf(acl.getFilepath());
337                 updateAclCommit(acl.getAcltype());
338                 break;
339             default:
340                 break;
341             }
342             updateMenuTxt();
343             updateSubMenuTxt();
344         }
345         return createSuccess;
346     }
347 
348     public void updateAclCommit(final String acltype)
349     {
350         Map<String, Object> params = new HashMap<String, Object>();
351         Date date = new Date();
352         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
353         String lastcommittime = sdf.format(date);
354         params.put("lastcommit", lastcommittime);
355         params.put("acltype", acltype);
356         params.put("status", ACLSTATUS);
357         dao.update(params);
358     }
359 
360     private boolean createNraclConf(final String filepath)
361     {
362         //1.判断文件是否存在,存在直接覆盖,不存在则创建文件;
363         createConf(filepath);
364         LOG.debug("=====NRACLCONF文件创建成功且已存在=====");
365 
366         //2.写入配置文件中;
367         BufferedWriter bw = null;
368 
369         //3.1.获取表UDNS_ACL_CT中的全部的数据
370         Map<String, Object> params = new HashMap<String, Object>();
371         List<UdnsAclCt> udnsAclCt4List = aclCtDao.query4List(params);
372 
373         try
374         {
375             bw = new BufferedWriter(new FileWriter(filepath));
376             LOG.debug("=====将数据写入NRACLCONF文件=====");
377 
378             //3.2 遍历ctid,联合查询表,获取相应的数据
379             for (UdnsAclCt udnsAclCt : udnsAclCt4List)
380             {
381 
382                 bw.write("nracl" + NAMESPACE + "{");
383                 bw.newLine();
384                 bw.write("	name" + NAMESPACE + udnsAclCt.getCtname() + ";");
385                 bw.newLine();
386                 bw.write("	cname" + NAMESPACE + udnsAclCt.getCtcname() + ";");
387                 bw.newLine();
388                 bw.write("	cid" + NAMESPACE + udnsAclCt.getCid() + ";");
389                 bw.newLine();
390 
391                 //3.3 根据ctid查询相应的IPSEG;
392                 Map<String, Object> par = new HashMap<String, Object>();
393                 par.put("ctid", udnsAclCt.getCtid());
394                 List<UdnsAclCt> unionList = aclCtDao.selectByUnion(par);
395 
396                 for (UdnsAclCt udnsAclCt2 : unionList)
397                 {
398                     bw.write("	" + udnsAclCt2.getIpseg() + checkSemicolon(udnsAclCt2.getIpseg()));
399                     bw.newLine();
400                 }
401                 bw.write("}");
402                 bw.newLine();
403                 bw.flush();
404 
405             }
406             LOG.debug("=====写入NRACLCONF成功=====");
407         }
408         catch (IOException e)
409         {
410             LOG.error(e);
411         }
412         finally
413         {
414             if (bw != null)
415             {
416                 try
417                 {
418                     bw.close();
419                 }
420                 catch (IOException e)
421                 {
422                     LOG.error(e);
423                 }
424             }
425         }
426 
427         return true;
428     }
429 
430     private boolean createGsaclConf(final String filepath)
431     {
432         //1.判断文件是否存在,存在直接覆盖,不存在则创建文件;
433         createConf(filepath);
434         LOG.debug("=====动态COLCONF文件创建成功且已存在=====");
435 
436         //2.写入配置文件中;
437         BufferedWriter bw = null;
438 
439         //3.1.获取表UDNS_ACL_COL中的全部的数据
440         Map<String, Object> params = new HashMap<String, Object>();
441         params.put("coltype", ONE);
442         List<UdnsAclCol> udnsAclColList = aclColDao.query4List(params);
443 
444         try
445         {
446             bw = new BufferedWriter(new FileWriter(filepath));
447             LOG.debug("=====将数据写入动态COLCONF文件=====");
448 
449             //3.2 遍历CID,联合查询表,获取相应的数据
450             for (UdnsAclCol udnsAclCol : udnsAclColList)
451             {
452 
453                 bw.write("gsacl" + NAMESPACE + "{");
454                 bw.newLine();
455 
456                 bw.write("	name" + NAMESPACE + udnsAclCol.getColname() + ";");
457                 bw.newLine();
458                 bw.write("	cname" + NAMESPACE + udnsAclCol.getColcname() + ";");
459                 bw.newLine();
460 
461                 //3.3 根据gid查询相应的IPSEG;
462                 Map<String, Object> par = new HashMap<String, Object>();
463                 par.put("cid", udnsAclCol.getCid());
464                 par.put("coltype", ONE);
465                 List<UdnsAclCol> unionList = aclColDao.selectByUnion(par);
466                 for (UdnsAclCol udnsAclCol2 : unionList)
467                 {
468                     bw.write("	" + udnsAclCol2.getIpseg() + checkSemicolon(udnsAclCol2.getIpseg()));
469                     bw.newLine();
470                 }
471                 bw.write("}");
472                 bw.newLine();
473                 bw.flush();
474 
475             }
476             LOG.debug("=====写入动态COLCONF成功=====");
477         }
478         catch (IOException e)
479         {
480             LOG.error(e);
481         }
482         finally
483         {
484             if (bw != null)
485             {
486                 try
487                 {
488                     bw.close();
489                 }
490                 catch (IOException e)
491                 {
492                     LOG.error(e);
493                 }
494             }
495         }
496 
497         return true;
498     }
499 
500     public boolean createYraclConf(final String filepath)
501     {
502         //1.判断文件是否存在,存在直接覆盖,不存在则创建文件;
503         createConf(filepath);
504         LOG.debug("=====固定COLCONF文件创建成功且已存在=====");
505 
506         //2.写入配置文件中;
507         BufferedWriter bw = null;
508 
509         //3.1.获取表UDNS_ACL_COL中的全部的数据
510         Map<String, Object> params = new HashMap<String, Object>();
511         params.put("coltype", ZERO);
512         List<UdnsAclCol> udnsAclColList = aclColDao.query4List(params);
513 
514         try
515         {
516             bw = new BufferedWriter(new FileWriter(filepath));
517 
518             LOG.debug("=====将数据写入固定COLCONF文件=====");
519 
520             //3.2 遍历CID,联合查询表,获取相应的数据
521             for (UdnsAclCol udnsAclCol : udnsAclColList)
522             {
523 
524                 //3.3. 对固定列的内容进行判断
525 
526                 if (yracl_yraclList.contains(udnsAclCol.getColname()))
527                 {
528                     bw.write("yracl" + NAMESPACE + "{");
529                 }
530                 else if (yracl_gsaclList.contains(udnsAclCol.getColname()))
531                 {
532                     bw.write("gsacl" + NAMESPACE + "{");
533                 }
534                 else
535                 {
536                     //throw new Exception("固定列的名称:" + udnsAclCol.getColname() + "不属于固定列的范围!!");
537                     return false;
538                 }
539 
540                 bw.newLine();
541 
542                 bw.write("	name" + NAMESPACE + udnsAclCol.getColname() + ";");
543                 bw.newLine();
544                 bw.write("	cname" + NAMESPACE + udnsAclCol.getColcname() + ";");
545                 bw.newLine();
546 
547                 //3.4 根据gid查询相应的IPSEG;
548                 Map<String, Object> par = new HashMap<String, Object>();
549                 par.put("cid", udnsAclCol.getCid());
550                 par.put("coltype", ZERO);
551                 List<UdnsAclCol> unionList = aclColDao.selectByUnion(par);
552                 for (UdnsAclCol udnsAclCol2 : unionList)
553                 {
554                     bw.write("	" + udnsAclCol2.getIpseg() + checkSemicolon(udnsAclCol2.getIpseg()));
555                     bw.newLine();
556                 }
557                 bw.write("}");
558                 bw.newLine();
559                 bw.flush();
560 
561             }
562             LOG.debug("=====写入固定COLCONF成功=====");
563         }
564         catch (Exception e)
565         {
566             LOG.error(e);
567         }
568         finally
569         {
570             if (bw != null)
571             {
572                 try
573                 {
574                     bw.close();
575                 }
576                 catch (IOException e)
577                 {
578                     LOG.error(e);
579                 }
580             }
581         }
582 
583         return true;
584     }
585 
586     public boolean createDbConf(final String filepath)
587     {
588         //1.判断文件是否存在,存在直接覆盖,不存在则创建文件;
589         createConf(filepath);
590         LOG.debug("=====DBCONF文件创建成功且已存在=====");
591 
592         //2.写入配置文件中;
593         BufferedWriter bw = null;
594 
595         //3.1.获取表UDNS_ACL_GROUP中的全部的数据
596         Map<String, Object> params = new HashMap<String, Object>();
597         List<UdnsAclGroup> UdnsAclGroupList = aclGroupDao.query4List(params);
598 
599         try
600         {
601             bw = new BufferedWriter(new FileWriter(filepath));
602             //3.2 遍历gid,联合查询表,获取相应的数据
603             LOG.debug("=====将数据写入DBCONF文件=====");
604             for (UdnsAclGroup udnsAclGroup : UdnsAclGroupList)
605             {
606                 bw.write("db" + NAMESPACE + "{");
607                 bw.newLine();
608                 //判断是否包含“.db”该字符串
609                 String includedb = !udnsAclGroup.getGname().contains(".db") ? ".db" : "";
610                 bw.write("	name" + NAMESPACE + udnsAclGroup.getGname() + includedb + ";");
611                 bw.newLine();
612 
613                 //3.3根据gid查询对应的记录
614                 Map<String, Object> par = new HashMap<String, Object>();
615                 par.put("gid", udnsAclGroup.getGid());
616                 List<UdnsAclGroup> unionList = aclGroupDao.selectByUnion(par);
617                 for (UdnsAclGroup udnsAclGroup2 : unionList)
618                 {
619                     bw.write("	" + udnsAclGroup2.getIpseg() + checkSemicolon(udnsAclGroup2.getIpseg()));
620                     bw.newLine();
621                 }
622                 bw.write("}");
623                 bw.newLine();
624                 bw.flush();
625 
626             }
627 
628             LOG.debug("=====写入DBCONF成功=====");
629         }
630         catch (IOException e)
631         {
632             LOG.error(e);
633         }
634         finally
635         {
636             if (bw != null)
637             {
638                 try
639                 {
640                     bw.close();
641                 }
642                 catch (IOException e)
643                 {
644                     LOG.error(e);
645                 }
646             }
647         }
648 
649         return true;
650     }
651 
652     private int checkNraclIpsegExist(final String ctid)
653     {
654         Map<String, Object> params = new HashMap<String, Object>();
655         params.put("ctid", ctid);
656         int count = aclCtDDao.selectByIpsegCount(params);
657         return count;
658     }
659 
660     private int CheckYraclIpsegExist(final String cid)
661     {
662         Map<String, Object> params = new HashMap<String, Object>();
663         params.put("cid", cid);
664         int count = aclColDDao.selectByIpsegCount(params);
665         return count;
666     }
667 
668     private int checkDbIpsegExist(final String gid)
669     {
670         Map<String, Object> params = new HashMap<String, Object>();
671         params.put("gid", gid);
672         int count = dbDDao.selectByIpsegCount(params);
673         return count;
674     }
675 
676     //检查IP段是否包含分号判断
677     private static String checkSemicolon(final String semicolon)
678     {
679         return semicolon.contains(";") ? "" : ";";
680     }
681 
682     private static void createConf(final String filepath)
683     {
684         File file = new File(filepath);
685         if (!file.exists())
686         {
687             try
688             {
689                 file.createNewFile();
690             }
691             catch (IOException e)
692             {
693                 LOG.error(e);
694             }
695         }
696 
697     }
698 
699     @Override
700     public boolean update_all(final String acltype)
701     {
702 
703         Map<String, Object> params = new HashMap<String, Object>();
704         Date date = new Date();
705         SimpleDateFormat sdf = new SimpleDateFormat(Constant.FULL_DATE_FORMAT);
706         String lastcommittime = sdf.format(date);
707         params.put("acltype", acltype);
708         params.put("status", "未生效");
709         //params.put("modname", "yd_super");
710         //获取用户名
711         User loginUser = QueryUtil.getLoginUser();
712         params.put("modname", loginUser.getLoginName());
713         params.put("lastmodify", lastcommittime);
714         return dao.update(params);
715     }
716 
717     @Override
718     public Page<UdnsAclCommit> query4Page(final Map<String, Object> params, final int PageNo, final int PageSize)
719     {
720         return dao.query4Page(params, PageNo, PageSize);
721     }
722 
723     @Override
724     public List<UdnsAclCommit> query4List(final Map<String, Object> params)
725     {
726         return dao.query4List(params);
727     }
View Code

 

以上是关于JAVA基础知识总结的主要内容,如果未能解决你的问题,请参考以下文章

Java Web学习总结JSP

线程学习知识点总结

Java基础总结之Thymeleaf详解

201621123062《java程序设计》第九周作业总结

Java进阶之光!2021必看-Java高级面试题总结

VsCode 代码片段-提升研发效率