@Test public void testUpdate() { UserDO updateUser = new UserDO(); updateUser.setId(1); updateUser.setUsername("nicai"); userDao.updateById(updateUser); }
public void updateById(UserDO entity) { final Update update = new Update(); ReflectionUtils.doWithFields(entity.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if ("id".equals(field.getName()) || field.getAnnotation(Transient.class) != null || Modifier.isStatic(field.getModifiers())) { return; } if (!field.isAccessible()) { field.setAccessible(true); } if (field.get(entity) == null) { return; } update.set(field.getName(), field.get(entity)); } }); if (update.getUpdateObject().isEmpty()) { return; } mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(entity.getId())), update, UserDO.class); }
UserDao { public void updateById(UserDO entity) { final Update update = new Update(); ReflectionUtils.doWithFields(entity.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if ("id".equals(field.getName()) || field.getAnnotation(Transient.class) != null || Modifier.isStatic(field.getModifiers())) { return; } if (!field.isAccessible()) { field.setAccessible(true); } if (field.get(entity) == null) { return; } update.set(field.getName(), field.get(entity)); } }); if (update.getUpdateObject().isEmpty()) { return; } mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(entity.getId())), update, UserDO.class); } }
UserDao { public void updateById(UserDO entity) { final Update update = new Update(); ReflectionUtils.doWithFields(entity.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if ("id".equals(field.getName()) || field.getAnnotation(Transient.class) != null || Modifier.isStatic(field.getModifiers())) { return; } if (!field.isAccessible()) { field.setAccessible(true); } if (field.get(entity) == null) { return; } update.set(field.getName(), field.get(entity)); } }); if (update.getUpdateObject().isEmpty()) { return; } mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(entity.getId())), update, UserDO.class); }  }
UserDao { public void updateById(UserDO entity) { final Update update = new Update(); ReflectionUtils.doWithFields(entity.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if ("id".equals(field.getName()) || field.getAnnotation(Transient.class) != null || Modifier.isStatic(field.getModifiers())) { return; } if (!field.isAccessible()) { field.setAccessible(true); } if (field.get(entity) == null) { return; } update.set(field.getName(), field.get(entity)); } }); if (update.getUpdateObject().isEmpty()) { return; } mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(entity.getId())), update, UserDO.class); }  void insert(UserDO entity); void updateById(UserDO entity); void deleteById(Integer id); UserDO findById(Integer id); UserDO findByUsername(String username); List<UserDO> findAllById(List<Integer> ids); }
UserDao { public void updateById(UserDO entity) { final Update update = new Update(); ReflectionUtils.doWithFields(entity.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if ("id".equals(field.getName()) || field.getAnnotation(Transient.class) != null || Modifier.isStatic(field.getModifiers())) { return; } if (!field.isAccessible()) { field.setAccessible(true); } if (field.get(entity) == null) { return; } update.set(field.getName(), field.get(entity)); } }); if (update.getUpdateObject().isEmpty()) { return; } mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(entity.getId())), update, UserDO.class); }  void insert(UserDO entity); void updateById(UserDO entity); void deleteById(Integer id); UserDO findById(Integer id); UserDO findByUsername(String username); List<UserDO> findAllById(List<Integer> ids);  }