Substring()函数:截取字符串中的一段
substring(int strat,int end)中第一个参数是开始位置,第二个参数是结束位置,
lastIndexOf() 方法:
public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
@RequestBody注解:
主要用来接收前端传递给后端的json字符串中的数据的即@RequestBody注解一般是与json配合使用的。前端将参数汇成json传输,后端使用一个@RequestBody注解的参数接收。
在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
@ResponseBody注解:
的作用是将controller的方法返回的对象 通过适当的转换器 转换为指定的格式之后,写入到response对象的body区(响应体中),通常用来返回JSON数据或者是XML。
FilenameUtils.getExtension(): 获取文件名后缀
String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
字符串相等:
.equals
selectTreeDto.getParentId().equals("1")
if(Objects.equals(sysOffice.getType(), "unit")){
sysOffice.setSjParentId(sysOffice.getId());
}
通常比较字符串会使用
a.equals(b)
此方法前提是a需要非null,否则会NullPointException异常,而b则不会触发NullPointException异常
如果判断的双方都有可能是null则推荐使用Objects.equals(a,b)
Objects.equals("abc", "abc") 返回 true
Objects.equals(null, null) 返回 true
Objects.equals(null, "abc") 返回 false
Objects.equals("abc",null) 返回 false
判断字段是否为空:
if(StringUtils.isNotEmpty(sysOffice1.getId())){
sysOffice.setSjParentId(sysOffice1.getId());
}
往字段里添加:
if(Objects.equals(sysOffice.getType(), "unit")){
sysOffice.setSjParentId(sysOffice.getId());
}
泛型集合添加:
List<SysOffice> NewList = new ArrayList<>();
NewList.add(selectTreeDto);
for循环判断:
List<SysOffice> list = sysOfficeDAO.getSecondaryList(sysOffice);
for (SysOffice obj : list) {
SysOffice selectTreeDto = obj;
if (selectTreeDto.getParentId().equals("1807")){
NewList.add(selectTreeDto);
}
}
抛出异常的方法:
String result = "";
try {
sysOfficeDAO.updateById(sysOffice);
}catch (Exception e){
result = e.getMessage();
}
return result;
转化为树形方法:
private List<SysOffice> listToTree(List<SysOffice> dataList) {
List<SysOffice> allMenuList = new ArrayList<>();
for (SysOffice obj : dataList) {
SysOffice selectTreeDto = obj;
allMenuList.add(selectTreeDto);
}
Map<String, SysOffice> newMap = new HashMap<>();
List<SysOffice> result = new ArrayList<>();
for (SysOffice treeDto : allMenuList) {
newMap.put(treeDto.getId(), treeDto);
}
for (SysOffice treeDto : allMenuList) {
SysOffice parent = newMap.get(treeDto.getParentId());
if (parent != null) {
if (parent.getChildren() == null) {
List<SysOffice> ch = new ArrayList<>();
ch.add(treeDto);
parent.setChildren(ch);
} else {
List<SysOffice> ch = parent.getChildren();
ch.add(treeDto);
parent.setChildren(ch);
}
} else {
result.add(treeDto);
}
}
return result;
}
双列集合的添加:
Map<String, SysOffice> newMap = new HashMap<>();
newMap.put(treeDto.getId(), treeDto);
//双列集合取值,根据key取,取得结果为value,不包含key
SysOffice parent = newMap.get(treeDto.getParentId());