cuterabbitbaby 2020-01-07
1、自动填充的字段:
createdBy,updatedBy,createdDate,updatedDate
/*** 插入使用*/ @Override public void insertFill(MetaObject metaObject) { String operator = "bokai"; fillValue(metaObject, CREATED_BY, () -> operator); fillValue(metaObject, UPDATED_BY, () -> operator); fillValue(metaObject, CREATED_DATE, () -> getDateValue(metaObject.getSetterType(CREATED_DATE))); fillValue(metaObject, UPDATED_DATE, () -> getDateValue(metaObject.getSetterType(UPDATED_DATE))); } @Override public void updateFill(MetaObject metaObject) { String operator = "bokai"; fillValue(metaObject, "et."+UPDATED_BY, () -> operator); fillValue(metaObject, "et."+UPDATED_DATE, () -> getDateValue(metaObject.getSetterType("et."+UPDATED_DATE))); } private void fillValue(MetaObject metaObject, String fieldName, Supplier<Object> valueSupplier) { if (!metaObject.hasGetter(fieldName)) { return; } Object sidObj = metaObject.getValue(fieldName); if (sidObj == null && metaObject.hasSetter(fieldName) && valueSupplier != null) { setFieldValByName(fieldName, valueSupplier.get(), metaObject); } } private Object getDateValue(Class<?> setterType) { if (Date.class.equals(setterType)) { return new Date(); } else if (LocalDateTime.class.equals(setterType)) { return LocalDateTime.now(); } return null; }
涉及获取需要更新的createdDate,updatedDate 字段属性的类型
2、insertFill方法和updateFill方法的入参 MetaObject metaObject 中的属性不同
metaObject.getSettreType()获取字段属性类型的Class在 insertFill 和 updateFill 中就不能同样使用了
insertFill中metaObject如下:
updateFill中metaObject如下:
我们去看BaseMapper源码,如下:
可以看出update的语句都有 et
更新时,我们需要使用et.字段名才会生效