当前位置:   article > 正文

Java 代码笔记

代码笔记

 平时写接口总是忘记,一些事情,每次都要花时间做相同的事,所以在此记一份笔记,以后直接拷贝,相当于代码生成器,更新了模板,参数类检查的模板

标准接口代码模板 

  1. // 接口代码模板
  2. /**
  3. * @param request
  4. * @param response
  5. * @return
  6. */
  7. @RequestMapping(params = "testControllerMethod", method = {RequestMethod.GET, RequestMethod.POST})
  8. @ResponseBody
  9. public String testControllerMethod(HttpServletRequest request, HttpServletResponse response) {
  10. AjaxJson returnJson = new AjaxJson();
  11. TSUser user = ResourceUtil.getSessionUser();
  12. String ipAddress = IpUtil.getIpAddr(request);
  13. String interfileid = request.getParameter("interfileid");
  14. // 参数多的话,建议使用参数类,参数类的必填项检查,请参考类:IInterfileEntity.class
  15. if(CheckUtils.checkIsNull(interfileid)){
  16. returnJson.setSuccess(false);
  17. returnJson.setMsg("interfileid is empty");
  18. return returnJson.getJsonStr();
  19. }
  20. // 系统日志
  21. TSSyslogEntity syslogEntity = new TSSyslogEntity();
  22. syslogEntity.setObjectId("");
  23. syslogEntity.setModuleId(GlobalData.MODULE_INTERNAL);
  24. syslogEntity.setLogType(Globals.LOG_TYPE_SEARCH);
  25. syslogEntity.setIpAddress(ipAddress);
  26. syslogEntity.setBrowser(BrowserUtils.checkBrowse(request));
  27. syslogEntity.setLogLevel(Globals.LOG_LEAVEL_INFO);
  28. // 文件日志
  29. TSDoclogEntity docLogEntity = new TSDoclogEntity();
  30. docLogEntity.setSpecNo("F333333");
  31. docLogEntity.setDocId("");
  32. docLogEntity.setVersion("A");
  33. docLogEntity.setOperation(GlobalData.OPT_VIEW);
  34. StringBuilder logBuilder = StringUtility.createBuilder("dmsCommonFuncController testControllerMethod Start");
  35. logBuilder = StringUtility.builderAppend(logBuilder,"\r\n",JSONObject.toJSONString(request.getParameterMap()),"\r\n",request.getQueryString());
  36. recordExceptionService.recordPrintErrMsg(logBuilder.toString());
  37. logBuilder.delete(0,logBuilder.length());
  38. logBuilder=null;
  39. StringBuilder docLogBuilder = StringUtility.createBuilder("内部文件PDF预览");
  40. Map<String,Object> sysLogMap = new HashMap<>();
  41. sysLogMap.put("方法名称","检查发行回收单");
  42. sysLogMap.put("Controller","dmsCommonFuncController");
  43. sysLogMap.put("Method","testControllerMethod");
  44. Map<String,Object> sysParamMap = new HashMap<>();
  45. sysParamMap.put("POST",request.getParameterMap());
  46. sysParamMap.put("GET",request.getQueryString());
  47. sysLogMap.put("输入参数",sysParamMap);
  48. try {
  49. //业务逻辑
  50. sysLogMap.put("操作动作","下载表单");
  51. returnJson.setMsg(mutiLangService.getLang("tip.suc"));
  52. Map<String,Object> sysObjMap = new HashMap<>();
  53. sysObjMap.put("文件id", "");
  54. sysObjMap.put("文件名称", "");
  55. sysObjMap.put("文件编号", "");
  56. sysObjMap.put("文件版本", "");
  57. docLogBuilder = StringUtility.builderAppend(docLogBuilder, "\r\n章节号:", "", "\r\n章节名:", "");
  58. sysLogMap.put("日志详情",sysObjMap);//"Java对象"
  59. sysLogMap.put("操作结果","操作成功;");
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. sysLogMap.put("操作结果","操作失败;");
  63. sysLogMap.put("错误信息",e.toString());
  64. returnJson.setSuccess(false);
  65. String errMsg = mutiLangService.getLang("system.msg.error.001");
  66. String excepMsg = responseWriteService.getErrMsgTranslate(e);
  67. errMsg = StringUtility.replace(errMsg, "#{module}", "获取Token接口", "#{errMsg}", excepMsg);
  68. returnJson.setMsg(errMsg);
  69. returnJson.setRespCode("E999");
  70. syslogEntity.setLogLevel(Globals.LOG_LEAVEL_ERROR);
  71. recordExceptionService.recordPrintException(e, request);
  72. } finally {
  73. sysLogMap.put("返回信息",returnJson.getJSONObject());
  74. docLogEntity.setDetailsContent(docLogBuilder.toString());
  75. String logJsonContent = JSON.toJSONString(sysLogMap);
  76. syslogEntity.setRemark(logJsonContent);
  77. dmsLogService.writeSysLog(syslogEntity);
  78. dmsLogService.writeDocLog(docLogEntity);
  79. recordExceptionService.recordPrintErrMsg(logJsonContent);
  80. recordExceptionService.recordPrintErrMsg("dmsCommonFuncController testControllerMethod End");
  81. docLogBuilder.delete(0, docLogBuilder.length());
  82. sysLogMap.clear();
  83. sysLogMap = null;
  84. docLogBuilder = null;
  85. //syslogEntity = null;
  86. //docLogEntity = null;
  87. }
  88. return returnJson.getJsonStr();
  89. }

发送系统消息和邮件代码模板 

  1. private void sendSysNotice(String perUser, List<ReturnOutLink> linkList) {
  2. // 通知信息
  3. String title = "【系统消息】:文件分享链接通知";
  4. //String content = "这是您申请的文件打印的链接,内容如下:<br><style type=\"text/css\">table{border:1px solid#DCDCDC;border-collapse:collapse;width:60%}th,td{border:1px solid#DCDCDC;text-align:center}</style><table><tr><th>文件</th><th>超链接</th><th>提取码</th>#{extension}</tr></table>";
  5. // String tableString = "";
  6. //String trBaseString = "<tr><td>#{specNo}</td><td><a href=\"#{url}\"target=\"_blank\"style=\"text-decoration: none;\">链接</a></td><td><b>#{pwd}</b></td></tr>";
  7. StringBuilder contentBuilder = FileStream.getContentByClass("notice_temp", "publicdoc_share_email_content.mail");
  8. StringBuilder trBaseBuilder = FileStream.getContentByClass("notice_temp", "publicdoc_share_email_trlist.mail");
  9. TSUser user = ResourceUtil.getSessionUser();
  10. // 发送外链邮件,系统通知
  11. StringBuilder trBuilder = new StringBuilder();
  12. try {
  13. for (int i = 0; i < linkList.size(); i++) {
  14. trBuilder.append(trBaseBuilder);
  15. trBuilder = StringUtility.replace(trBuilder, "#{specNo}", linkList.get(i).getSpecNo(),"#{title}",linkList.get(i).getTitle(), "#{url}",
  16. linkList.get(i).getUrl());
  17. }
  18. String trString = trBuilder.toString();
  19. String content = StringUtility.replace(contentBuilder,"#{extension}", trString,"#{realName}",user.getRealName()).toString();
  20. dccCommonService.createNotice("系统", title, content, "1", "3", perUser, new Date(), "");
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. recordExceptionService.recordPrintException(e, null);
  24. }
  25. }

发送系统消息和邮件代码模板 - 2

  1. public AjaxJson doSyncKeyWord() throws Exception{
  2. AjaxJson returnJson = new AjaxJson();
  3. //1、获取circle_customer_keyword数据
  4. List<CircleCustomerKeywordEntity> list=findHql("from CircleCustomerKeywordEntity where nodeid='F'");//loadAll(CircleCustomerKeywordEntity.class);
  5. if (0==list.size()) {
  6. return returnJson;
  7. }
  8. //2、拼接语句
  9. HashMap<String, Object> data = new HashMap<String, Object>();
  10. StringBuilder sqlBuilder = FileStream.getContentByClass(
  11. StringUtility.createPathString("com", "circle", "customerkeyword", "dao"), "CircleCustomerKeywordDao_insertKeyword.sql");
  12. StringBuilder sqlValBuilder = FileStream.getContentByClass(
  13. StringUtility.createPathString("com", "circle", "customerkeyword", "dao"), "CircleCustomerKeywordDao_insertKeyword_2.sql");
  14. //3、获取数据源
  15. int size=list.size();
  16. for (int i = 0; i < size; i++) {
  17. CircleCustomerKeywordEntity entity = list.get(i);
  18. if (i>0) {
  19. sqlBuilder.append(sqlValBuilder);
  20. }
  21. StringUtility.replace(sqlBuilder, "#{id}",entity.getId());//UUIDGenerator.generateByUUID()
  22. StringUtility.replace(sqlBuilder, "#{KeyWord}",entity.getKeyWord());
  23. StringUtility.replace(sqlBuilder, "#{deleteflag}",entity.getDeleteFlag());
  24. StringUtility.replace(sqlBuilder, "#{Warehouse}",GlobalData.DOC_WARE_HOUSE);
  25. }
  26. //4、写入
  27. data.clear();
  28. // 设置外部服务器数据表,置为同步成功,下次同步不会再同步一次
  29. //executeSql(sqlBuilder.toString(), data);
  30. String dbKey = "Circle_FCS";
  31. DynamicDBUtil.updateByHash(dbKey, sqlBuilder.toString(), data);
  32. return returnJson;
  33. }

  1. // 内部文件新建接口
  2. /**
  3. * 根据请求带过来的流程表单的信息,创建内部文件
  4. *
  5. * 参数结构:&id=c1f26a9a19024976bfc328b0961aad08&title=撒扥撒扥森扥&docType=&userName=&docTypeEN=&docLevelCode=
  6. *
  7. * 返回值结构:"{\"respCode\":\"E002\",\"msg\":\"文件类型不能为空\",\"success\":false}"
  8. *
  9. * @param iInterfileEntity
  10. * @param request
  11. * @param response
  12. * @return
  13. */
  14. @RequestMapping(params = "IcreateInterfile", method = { RequestMethod.GET, RequestMethod.POST })
  15. @ResponseBody
  16. public String IcreateInterfile(IInterfileEntity iInterfileEntity, HttpServletRequest request,
  17. HttpServletResponse response) {
  18. AjaxJson returnJson = new AjaxJson();
  19. TSSyslogEntity syslogEntity = new TSSyslogEntity();
  20. syslogEntity.setObjectId(iInterfileEntity.getId());
  21. syslogEntity.setModuleId("dmsCommonFuncController");
  22. syslogEntity.setLogType(Globals.LOG_TYPE_INSERT);
  23. StringBuilder sysMsgBuilder = StringUtility.createBuilder("接口IcreateInterfile,参数:",iInterfileEntity.toString());
  24. try {
  25. syslogEntity.setLogLevel(Globals.LOG_LEAVEL_INFO);
  26. // -----------------参数接收类必填检查---------------------
  27. String resultMsg = BeanHelper.checkBeanAttr(iInterfileEntity);
  28. if (StringUtils.isNotBlank(resultMsg)) {
  29. returnJson.setSuccess(false);
  30. returnJson.setMsg(mutiLangService.getLang(resultMsg));
  31. returnJson.setRespCode("E002");
  32. return returnJson.getJsonStr();
  33. }
  34. // **************************************************
  35. TSUser user = ResourceUtil.getSessionUser();
  36. if (StringUtils.isNotBlank(iInterfileEntity.getUserName())) {// 如果传了账号名 ,就抓取账号名的user
  37. TSUser baseUser = new TSUser();
  38. baseUser.setUserName(iInterfileEntity.getUserName());
  39. user = userService.getUserInfoByUsername(baseUser);// 获取用户对象
  40. }
  41. DccInternalFileEntity internalFileEntity = new DccInternalFileEntity();
  42. internalFileEntity.setId(iInterfileEntity.getId());
  43. DccDeptcodeEntity deptEntity = OrgUtil.getHrDeptCode(user.getDepartid(), user.getPlant());
  44. List<CircleSystemSettingEntity> setList = systemService.loadAll(CircleSystemSettingEntity.class);
  45. CircleSystemSettingEntity systemSettingEntity = setList.get(0);
  46. // 人
  47. if ("0".equals(systemSettingEntity.getFileowner())) {
  48. internalFileEntity.setOwnerName(user.getRealName());
  49. internalFileEntity.setOwnerNo(user.getEmpno());
  50. }
  51. // 部门
  52. if ("1".equals(systemSettingEntity.getFileowner())) {
  53. internalFileEntity.setOwnerName(deptEntity.getDeptName());
  54. internalFileEntity.setOwnerNo(deptEntity.getHrDeptCode());
  55. }
  56. internalFileEntity.setDeptCode(deptEntity.getHrDeptCode());
  57. // 保存内部文件
  58. setInterFile(iInterfileEntity, user, internalFileEntity);// 给内部文件赋值
  59. // 设置返回值
  60. returnJson.setJsonData(JSONObject.toJSONString(internalFileEntity));
  61. // 创建新建记录
  62. createDCCFlowEntity(user, internalFileEntity);// 创建DCC 创建记录
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. StringUtility.builderAppend(sysMsgBuilder, "\r\nErrorMsg:",e.getMessage());
  66. returnJson.setSuccess(false);
  67. returnJson.setMsg("操作失败");
  68. returnJson.setRespCode("E999");
  69. recordExceptionService.recordPrintException(e, request);
  70. syslogEntity.setLogLevel(Globals.LOG_LEAVEL_ERROR);
  71. } finally {
  72. syslogEntity.setRemark(sysMsgBuilder.toString());
  73. dmsLogService.writeSysLog(syslogEntity);
  74. }
  75. return returnJson.getJsonStr();
  76. }
  1. package com.dcc.common.entity;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.validation.constraints.NotNull;
  5. import com.alibaba.fastjson.JSONObject;
  6. /**
  7. * 2022-06-15
  8. * @author tianming.fan
  9. *
  10. */
  11. public class IInterfileEntity {
  12. /*add by ftm
  13. * !!!数据接收类属性必须赋予默认值,否则,如果前端未给某属性传值,程序就不会检查到该属性*/
  14. private java.lang.String id="";//文件id
  15. private java.lang.String title="";//文件名称
  16. private java.lang.String docType="";//文件类型名
  17. private java.lang.String userName="";// 账号名
  18. private java.lang.String docTypeEN="";//暂时无值,可以不传
  19. private java.lang.String docTypeCode="";//文件类型码
  20. private java.lang.String docLevelCode="";//文件等级
  21. private java.lang.String confidentialLevel="";//机密等级
  22. private Map<String, String> tipMap = new HashMap<String, String>();//提示信息
  23. private Map<String, Boolean> requiredMap = new HashMap<String, Boolean>();//是否必填
  24. public Map<String, Boolean> getRequiredMap() {
  25. requiredMap.put("id", true);
  26. requiredMap.put("title", true);
  27. requiredMap.put("docType", true);
  28. requiredMap.put("userName", false);
  29. requiredMap.put("docTypeEN", false);
  30. requiredMap.put("docTypeCode", true);
  31. requiredMap.put("docLevelCode", true);
  32. requiredMap.put("confidentialLevel", true);
  33. return requiredMap;
  34. }
  35. public Map<String, String> getTipMap() {
  36. tipMap.put("id", "文件ID不能为空");
  37. tipMap.put("title", "文件名称不能为空");
  38. tipMap.put("docType", "文件类型不能为空");
  39. tipMap.put("userName", "账号名不能为空");
  40. tipMap.put("docTypeEN", "文件类型英文名称不为空");
  41. tipMap.put("docTypeCode", "文件类型码不能为空");
  42. tipMap.put("docLevelCode", "文件等级不能为空");
  43. tipMap.put("confidentialLevel", "机密等级不能为空");
  44. return tipMap;
  45. }
  46. public java.lang.String getUserName() {
  47. return userName;
  48. }
  49. public void setUserName(java.lang.String userName) {
  50. this.userName = userName;
  51. }
  52. public java.lang.String getTitle() {
  53. return title;
  54. }
  55. public void setTitle(java.lang.String title) {
  56. this.title = title;
  57. }
  58. public java.lang.String getDocType() {
  59. return docType;
  60. }
  61. public void setDocType(java.lang.String docType) {
  62. this.docType = docType;
  63. }
  64. public java.lang.String getDocTypeCode() {
  65. return docTypeCode;
  66. }
  67. public void setDocTypeCode(java.lang.String docTypeCode) {
  68. this.docTypeCode = docTypeCode;
  69. }
  70. public java.lang.String getDocLevelCode() {
  71. return docLevelCode;
  72. }
  73. public void setDocLevelCode(java.lang.String docLevelCode) {
  74. this.docLevelCode = docLevelCode;
  75. }
  76. public java.lang.String getConfidentialLevel() {
  77. return confidentialLevel;
  78. }
  79. public void setConfidentialLevel(java.lang.String confidentialLevel) {
  80. this.confidentialLevel = confidentialLevel;
  81. }
  82. public java.lang.String getId() {
  83. return id;
  84. }
  85. public void setId(java.lang.String id) {
  86. this.id = id;
  87. }
  88. public java.lang.String getDocTypeEN() {
  89. return docTypeEN;
  90. }
  91. public void setDocTypeEN(java.lang.String docTypeEN) {
  92. this.docTypeEN = docTypeEN;
  93. }
  94. @Override
  95. public String toString() {
  96. return "IInterfileEntity [id=" + id + ", title=" + title + ", docType=" + docType + ", userName=" + userName
  97. + ", docTypeEN=" + docTypeEN + ", docTypeCode=" + docTypeCode + ", docLevelCode=" + docLevelCode
  98. + ", confidentialLevel=" + confidentialLevel + "]";
  99. }
  100. public String getJsonStr(){
  101. JSONObject obj = new JSONObject();
  102. return obj.toJSONString();
  103. }
  104. }
  1. /**
  2. * 日志方法调用案例 add by ftm 2022-05-06
  3. *
  4. * @param request
  5. * @param response
  6. * @return
  7. */
  8. @RequestMapping(params = "testLog")
  9. @ResponseBody
  10. public AjaxJson testLog(HttpServletRequest request, HttpServletResponse response) {
  11. AjaxJson j = new AjaxJson();
  12. // 系统日志
  13. TSSyslogEntity syslogEntity = new TSSyslogEntity();
  14. syslogEntity.setRemark("测试日志");
  15. syslogEntity.setObjectId("");
  16. syslogEntity.setModuleId(String.valueOf(request.getSession().getAttribute("moduleId")));
  17. syslogEntity.setLogLevel(Globals.LOG_LEAVEL_INFO);
  18. syslogEntity.setLogType(Globals.LOG_TYPE_UPDATE);
  19. dmsLogService.writeSysLog(syslogEntity);
  20. // 文件日志
  21. TSDoclogEntity docLogEntity = new TSDoclogEntity();
  22. docLogEntity.setSpecNo("F333333");
  23. docLogEntity.setDocId("");
  24. docLogEntity.setVersion("A");
  25. docLogEntity.setOperation("查看");
  26. docLogEntity.setDetailsContent("内部文件PDF预览");
  27. dmsLogService.writeDocLog(docLogEntity);
  28. // 审批记录日志
  29. TSOperatelogEntity operationEntity = new TSOperatelogEntity();
  30. operationEntity.setDocId("");
  31. operationEntity.setOperateName("同意");
  32. operationEntity.setOperateMemo("信息未填写");
  33. operationEntity.setDocStatus("DCC签核");
  34. operationEntity.setDocType("");
  35. operationEntity.setSpecNo("F000000");
  36. dmsLogService.writeOperateLog(operationEntity);
  37. return j;
  38. }
  1. /**
  2. * 批量删除外发文件子表
  3. *
  4. * @return
  5. */
  6. @RequestMapping(params = "doBatchDel")
  7. @ResponseBody
  8. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  9. AjaxJson returnJson = new AjaxJson();
  10. //系统日志
  11. TSSyslogEntity tsLog = new TSSyslogEntity();
  12. String message = mutiLangService.getLang("common.delete.success");
  13. StringBuilder logBuilder = new StringBuilder();
  14. try{
  15. for(String id:ids.split(",")){
  16. DmsOutPubFileEntity dmsOutPubFile = systemService.getEntity(DmsOutPubFileEntity.class,
  17. id
  18. );
  19. dmsOutPubFileService.delete(dmsOutPubFile);
  20. logBuilder = StringUtility.builderAppend(logBuilder,dmsOutPubFile.getSpecNo(),"\r\n",dmsOutPubFile.getTitle(),",",message);
  21. tsLog.setLogLevel(Globals.Log_Leavel_INFO);
  22. tsLog.setLogType(Globals.Log_Type_DEL);
  23. }
  24. }catch(Exception e){
  25. e.printStackTrace();
  26. message = mutiLangService.getLang("system.msg.operation_failed");
  27. recordExceptionService.recordPrintException(e, request);
  28. logBuilder = StringUtility.builderAppend(logBuilder,message,"\r\n异常信息:",e.getMessage());
  29. returnJson.setRespCode("E005");//数据更新失败
  30. returnJson.setSuccess(false);
  31. tsLog.setLogLevel(Globals.Log_Leavel_ERROR);
  32. tsLog.setLogType(Globals.Log_Type_DEL);
  33. }finally {
  34. tsLog.setRemark(logBuilder.toString());
  35. dmsLogService.writeSysLog(tsLog);
  36. }
  37. returnJson.setMsg(message);
  38. return returnJson;
  39. }
  1. /**
  2. * 更新变更申请
  3. *
  4. * @param ids
  5. * @return
  6. */
  7. @RequestMapping(params = "doUpdate")
  8. @ResponseBody
  9. public AjaxJson doUpdate(DccChangeApplyEntity dccChangeApply, HttpServletRequest request) {
  10. //String message = "变更申请更新成功";
  11. AjaxJson returnJson = new AjaxJson();
  12. //系统日志
  13. TSSyslogEntity tsLog = new TSSyslogEntity();
  14. tsLog.setModuleId("dccChangeApplyController");
  15. String message = mutiLangService.getLang("common.edit.success");
  16. StringBuilder logBuilder = new StringBuilder();
  17. DccChangeApplyEntity t = dccChangeApplyService.get(DccChangeApplyEntity.class, dccChangeApply.getId());
  18. if (null == t) {
  19. return doAdd(dccChangeApply, request);
  20. }
  21. try {
  22. MyBeanUtils.copyBeanNotNull2Bean(dccChangeApply, t);
  23. dccChangeApplyService.updateEntitie(t);
  24. //systemService.addLog(message, Globals.LOG_TYPE_UPDATE, Globals.Log_Leavel_INFO);
  25. logBuilder = StringUtility.builderAppend(logBuilder,dccChangeApply.getSpecNo(),"\r\n",dccChangeApply.getTitle(),",",message);
  26. tsLog.setLogLevel(Globals.Log_Leavel_INFO);
  27. tsLog.setLogType(Globals.Log_Type_UPDATE);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. /*message = "变更申请更新失败";
  31. throw new BusinessException(e.getMessage());*/
  32. message = mutiLangService.getLang("system.msg.operation_failed");
  33. recordExceptionService.recordPrintException(e, request);
  34. logBuilder = StringUtility.builderAppend(logBuilder,message,"\r\n异常信息:",e.getMessage());
  35. returnJson.setRespCode("E005");//数据更新失败
  36. returnJson.setSuccess(false);
  37. tsLog.setLogLevel(Globals.Log_Leavel_ERROR);
  38. tsLog.setLogType(Globals.Log_Type_UPDATE);
  39. }finally {
  40. tsLog.setRemark(logBuilder.toString());
  41. dmsLogService.writeSysLog(tsLog);
  42. }
  43. returnJson.setMsg(message);
  44. return returnJson;
  45. }

String.endsWith的StringBuffer替代方法是什么?

  1. if(buf.charAt(buf.length()-1)==File.separatorChar){
  2. //
  3. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/246388
推荐阅读
相关标签
  

闽ICP备14008679号