private void insertIntoMyTable (Myclass m) {
String query = \"INSERT INTO MYTABLE (NAME) VALUES (?)\";
jdbcTemplate.update(query, m.getName());
}
JdbcTemplate is at the core of Spring. Another option is to use SimpleJdbcInsert.
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
simpleJdbcInsert
.withTableName("TABLENAME")
.usingGeneratedKeyColumns("ID");
SqlParameterSource params = new MapSqlParameterSource()
.addValue("COL1", model.getCol1())
.addValue("COL2", model.getCol2());
Number number = simpleJdbcInsert.executeAndReturnKey(params);
You can still @Autowire jdbcTemplate. To me, this is more convenient than working with jdbcTemplate.update() method and KeyHolder to get the actual id.
Example code snippet is tested with Apache Derby and should work with the usual databases.
Use of Spring JPA is another option - if ORM is for you.