5.Spring Cloud (Hoxton.SR8) 实战笔记—项目中细节实现 约束 注意事项、模块难点总结
本文目录如下:
二、项目中细节实现 & 约束 & 注意事项
判断字符串是否为空?
判断是否为
空
:StringUtils.isEmpty(str)
判断是否非空
:StringUtils.isNotEmpty(str)
// StringUtils.isEmpty(str) 内部实现
public static boolean isEmpty(String str) {return str == null || str.length() == 0;
}
入参 ‘Num’ 字段转换为 ‘num’ 的问题?
使用
@JsonProperty
对 字段 进行标注即可。
通过 Java 获取时间 (Date类型) 并插入数据库?
- java 获取当前时间的三种方法
- Java时间格式字符串与Date的相互转化
// 创建时间
Calendar calendar= Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// calendar.add(Calendar.HOUR_OF_DAY, 8); // 在原时间上加 8 小时
try {Date date = dateFormat.parse(dateFormat.format(calendar.getTime()));// ...
} catch (ParseException e) {e.printStackTrace();
}
神坑: baseMapper.selectById(String str)之坑?
selectById()
只会根据表的 主键 查询数据, 传入的 参数 只是 字符串 (用于匹配值) 而已。- 如果需要根据 指定字段 进行查询, 需要使用
wrapper
- 使用
selectOne() + wrapper
和selectList() + wrapper
代替selectById()
。
AES 实现加密函数, 供模块调用?
@Component
public class AesUtil {private final static String keyStr = "xqzhao";public static String AesEncrypt(String data) throws Exception {SecretKeySpec secretKey = new SecretKeySpec(keyStr.getBytes(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedData = cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encryptedData);}public static String AesDecrypt(String data) throws Exception {SecretKeySpec secretKey = new SecretKeySpec(keyStr.getBytes(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));return new String(decryptedData);}
}
Java实体类 与 Map 相互转换的方法?
点击查看: Java对象与Map转换,你了解几种?
public static void main(String[] args){Map<String, Object> map = new HashMap();map.put("name", "谢清照");map.put("age", 18);//map转java对象Student stu = JSONObject.parseObject(JSONObject.toJSONString(map), Student .class);// Map<String, Object> s = JSON.parseObject(JSON.toJSONString(user), new TypeReference<Map<String, Object>>() {});Student xqz= new Student ();user.setName("谢清照");user.setAge(18);//java对象转mapMap<String, Object> xqzhao = JSONObject.parseObject(JSONObject.toJSONString(xqz));
}
三、模块难点总结
1、模块一: DeptUser (简单)
2、模块二: WebHome (中等)
存储过程
3、模块三: WebFlow (困难)
XML文档对象处理
通过
SAXReader
把字符串解析为 XML文档对象:Document
private Document GetXPDLXml(String instanceid) {CommonR r = GetInstanceXPDL(instanceid);if (StringUtils.isEmpty(r.getData())) {return null;}String gbEncode = "GB2312";byte[] byteXpdl = r.getData().replace(gbEncode, "UTF-8").getBytes(Charset.forName(gbEncode));ByteArrayInputStream bis = new ByteArrayInputStream(byteXpdl);// SAXReader: dom4j 库中的一个 XML 解析器,用于解析 XML 文档。SAXReader reader = new SAXReader();// Document: 代表一个 XML 文档对象,是 dom4j 库中的一个核心类。它提供了对 XML 文档的访问和操作方法,负责管理文档的结构和内容。Document doc_XPDL = null;try {doc_XPDL = reader.read(bis);} catch (DocumentException e) {e.printStackTrace();}return doc_XPDL;
}
获取XML文档对象中存储的信息
import javax.xml.soap.Node;public CAppInfo(Node node) {// < AppInfo TableStyleName = "默认样式" TableStyleID = "0418cf8d-af21-4477-8706-487c53a5d678" TableName = "T_P_bsbzdj" />AppName = node.getAttributes().getNamedItem("TableName").getNodeValue();TableStyleID = node.getAttributes().getNamedItem("TableStyleID").getNodeValue();TableStyleName = node.getAttributes().getNamedItem("TableStyleName").getNodeValue();
}
应用Enum
public enum ControlType {CanSeeAttachment,CanSeeDealInfo,// ......
}public boolean GetBtnControlValue(ControlType controlType) {for (CAttribute attr : listAttribute) {if (attr.Name.toLowerCase().equals("btncontrol")) {String tmp = attr.Value;String[] tmpArray = tmp.split("\\\\|");// 重点在: controlType.ordinal()return Boolean.parseBoolean(tmpArray[controlType.ordinal() - 1]);}}return false;
}