Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created May 23, 2018 11:33
Show Gist options
  • Save bdarfler/708fd8584f92c6c25cdd64e3c017cb03 to your computer and use it in GitHub Desktop.
Save bdarfler/708fd8584f92c6c25cdd64e3c017cb03 to your computer and use it in GitHub Desktop.
final Collection<String> parts = buildUpdateParts( dbObj );
if ( parts.isEmpty() ) { return; }
final String query = "UPDATE " + dbObj.getTableName() + " SET " + join( parts, ", " ) + " WHERE id = :id";
simpleJdbcTemplate.update( query, new BeanPropertySqlParameterSource( dbObj ) );
private static final Collection<String> buildUpdateParts( final LocaDbObj dbObj ) {
 return transform( filter( dbObj.getFieldNames(), not( IS_ID ) ), new BuildPart( dbObj ) );
}
private static final class BuildPart implements Function<String, String> {
 private final LocaDbObj dbObj;
 public BuildPart( final LocaDbObj dbObj ) {
 this.dbObj = dbObj;
 }
 public String apply( final String propName ) {
 return dbObj.getColumnName( propName ) + " = :" + propName;
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment