ry-with-resources自JDK7引入,在JDK9中进行了改进,使得用户可以更加方便、简洁的使用try-with-resources。
java
InputStream is = null;
OutputStream os = null;
try {
//...
} catch (IOException e) {
//...
}finally{
try {
if(os!=null){
os.close();
}
if(is!=null){
is.close();
}
} catch (IOException e2) {
//...
}
}
使用try-with-reason 多个流则用分号隔开
使用poi实现,导入excel并且识别,返回查询结果
java
@PostMapping("/paperBorrowTempletImport")
public Result<IPage<RecPaperBorrowRegisterResponse>>paperBorrowTempletImport(@RequestPart("file") MultipartFile file) {
List<PaperBorrowTempletSearchRequest> domainList = new ArrayList<>();
try (InputStream inpStream = file.getInputStream()){
List<List<Object>> objlist = new ArrayList<>();
// 创建Excel工作薄
Workbook workbook = getWorkBook(inpStream);
if (null == workbook) {
throw new Exception("Excel工作薄为空!");
}
// 遍历Excel中的所有sheet
int rowNull = 0;
for (Sheet sheet : workbook) {
// 取sheet
if (sheet == null) {
continue;
}
// 遍历当前sheet中的所有行
for (Row row : sheet) {
if (row == null) {
continue;
}
// 验证是否本业务数据模板
if (row.getRowNum() == 0) {
if (!"姓名".equals(getValue(row.getCell(0))) || !"工号".equals(getValue(row.getCell(1)))) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),"非本业务数据!");
} else {
continue;
}
}
// 连续两行为空行,则跳出
if ("".equals(getValue(row.getCell(0)).toString())) {
rowNull++;
if (rowNull == 2) {
break;
} else {
continue;
}
}
rowNull = 0;
// 遍历所有列
List<Object> li = new ArrayList<>();
for (Cell cell : row) {
li.add(getValue(cell));
}
objlist.add(li);
}
}
// 将字符数组转为对象
for (List<Object> obj : objlist) {
if (ObjectUtils.isEmpty(obj)) {
break;
}
PaperBorrowTempletSearchRequest domain = new PaperBorrowTempletSearchRequest();
domain.setInpNo(obj.get(0).toString());
domain.setVisitId(obj.get(1).toString());
domainList.add(domain);
}
// 根据导入的住院号获取标注患者信息
if (CollectionUtil.isNotEmpty(domainList)) {
// 去除重复数据
domainList.stream().distinct();
}
} catch (Exception e) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),e.getMessage());
}
RecPaperBorrowRegisterRequest request=new RecPaperBorrowRegisterRequest();
request.setRows(-1);
request.setPage(-1);
request.setPaperBorrowTempletSearchRequestList(domainList);
IPage<RecPaperBorrowRegisterResponse> iPage = iRecPaperBorrowService.getRecPaperBorrowRegisterList(request);
return Result.success(iPage);
}
同一个接口有时候想要分页,有时候又想要查全部数据,用的分页方式为mybatisPlus的方式
就是把page和row,同时设置为-1,因为小于零的话,就可以查全部,这是mybatisPlus的代码设置,不能设置为空,不然会报错
在项目中发现了一个bug,两个相等的Long类型使用了==来判断是否相等,但是每次都直接跳过了if,也就是说他们是不相等的。
1 .直接声明的 不管是Long 还是long -128 -127 之间使用 == 和 equlas 都是true 因为 Long包对 常用的做了缓存。如果在这个区间直接从cache 中取。所以 == 地址值也是一样的 为true
2 new 出来的 必须使用equals .虽然做了-128 -127 做了缓存,但是new出来的是新分配的内存,地址值不一样 使用== 数值即使一样也为false.
3 使用 == 不管是直接声明的,还是new出来的 ,只要一边有基本数据类型 都可以正常对比。会自动拆箱。例如:
因为下面 int和long 属于基本类型。所以 == 会将Long 拆箱成long 直接比较数值
Long e = 300L; int f = 300; Long g = 300L; long h = 300L; System.out.println("Long与int 300 equals 结果" + (e == f));//true System.out.println("Long与long 300L equals 结果" + (g == h));//true
测试代码
java
public static void main(String[] args) {
DemoLong demoLong=new DemoLong();
demoLong.setA(100L);
demoLong.setB(11237L);
DemoLongTwo demoLongTwo=new DemoLongTwo();
demoLongTwo.setAa(100L);
demoLongTwo.setBb(11237L);
System.out.println(demoLong.getA()== demoLongTwo.getAa());
System.out.println(demoLong.getB()== demoLongTwo.getBb());
System.out.println(demoLong.getA().equals( demoLongTwo.getAa()));
System.out.println(demoLong.getB().equals(demoLongTwo.getBb()) );
}
结果