@TableId private int id;
问题不是是BeanUtils拷贝为null的问题。
@TableId当时没加,然后名字不是叫id,是xxxxid。
这样会导致mybatis不能识别 id 在哪,即使数据库有,但是代码里他不知道的。
所以
1,要不,写id,可以不写主键
2,要不。取其他名字,但是要写@TableId注解。
=========================下方说明部分攻略无效,特此指下,以及后续也可参考。
网上很多都是,说null,就用下面的方式,其实没用。。。当然具体时候可能会有用吧,目前没遇到过。
BeanUtils.copyProperties(req,updatePr,getNullPropertyNames(req));private String[] getNullPropertyNames(Object source){ // org.springframework.beans.BeanWrapper final BeanWrapper src = new BeanWrapperImpl(source); // java.beans.PropertyDescriptor PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set<String> emptyNames = new HashSet<>(); for(PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) { emptyNames.add(pd.getName()); } } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }