可以使用Spring提供的接口KeyHolder
使用方法
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update( new PreparedStatementCreator(){
@Override
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException{
PreparedStatement ps = conn.prepareStatement(SQL_ADD, new String[] {});
ps = conn.prepareStatement(YOUR_SQL_SCRIPT, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "TEST");
ps.setInt(2, 1);
//...
return ps;
}
},
keyHolder);
return keyHolder.getKey().intValue();