KeyHolder holder = new GeneratedKeyHolder();getJdbcTemplate().update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS); ps.setString(1, person.getUsername()); ps.setString(2, person.getPassword()); ps.setString(3, person.getEmail()); ps.setLong(4, person.getRole().getId()); return ps; } }, holder);Long newPersonId = holder.getKey().longValue();
请注意,在较新版本的Postgres中,你需要使用
connection.prepareStatement(sql.toString(), new String[] { "idcompte" })
代替
connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)