Mybatis-plus 中的CRUD 的接口中 getById() 在for循环中使用的时候,如果查询的是同一条记录,一定要注意获取值的内存地址,如果查询的是同一条记录是情况下,第二次查询会直接返回上一次的 对象地址,如果第一次修改了对象中的非数据库内容,该内容会留存到第二次查询中。
例如:
List<Product> prds =productInoutRecords.stream()
.map(productInoutRecord -> {
Product product=new Product();
int productId=Integer.valueOf(productInoutRecord.getPrdId()).intValue();
product.setId(productId);
product=productService.getById(product);
//userNumber 是非数据库字段
product.setUseNumber(productInoutRecord.getCount());
product.setRemark(productInoutRecord.getRemarkB());
product.setMethodType(productInoutRecord.getMethodType());
farmingReqDto.setWarehouseId(productInoutRecord.getWarehouseId());
return product;
}).collect(Collectors.toList());
product 虽然是每次new出来的,但是在后面的getById中,product的引用地址就变成的查询的结果,第一次向List中add的就是查询出来的对象地址,第二次对UserNumber 进行赋值的时候 改变了第一个存入地址中的内容,导致第一次add跟第二次add 的内容一样!
版权声明:本文为qq_38272530原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。