Skip to content

Instantly share code, notes, and snippets.

@benelog
Created June 4, 2012 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benelog/2866872 to your computer and use it in GitHub Desktop.
Save benelog/2866872 to your computer and use it in GitHub Desktop.
Spring jdbc
@Test
public void batchUpdateTest(){
SqlParameterSource[] source = new SqlParameterSource[]{new MapSqlParameterSource("id", 1), new MapSqlParameterSource("id", 2),new MapSqlParameterSource("id", 3)};
int[] updated = simpleJdbcTemplate.batchUpdate("update perform set id=:id where id = :id", source);
System.out.println(Arrays.toString(updated));
}
public Integer insertAndGetSeq(Book book) {
SqlParameterSource params = new BeanPropertySqlParameterSource(book);
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbc.update(WidgetSqls.insert, params, keyHolder);
return keyHolder.getKey().intValue();
}
public Integer insertAndGetSeq(Book input) {
SqlParameterSource params = new BeanPropertySqlParameterSource(input);
SimpleJdbcInsert insertAction = new SimpleJdbcInsert(dataSource).withTableName("book");
insertAction.setGeneratedKeyName("seq");
KeyHolder keyHolder = insertAction.executeAndReturnKeyHolder(params);
return keyHolder.getKey().intValue();
}
private void insert(Book input) {
SqlParameterSource params = new BeanPropertySqlParameterSource(input);
SimpleJdbcInsert insertAction = new SimpleJdbcInsert(ds).withTableName("book");
insertAction.execute(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment