import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; public class jdbcTest{ //setter and getter //..... //function final String sql="..."; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException{ PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERAT ED_KEYS); ps.setString(1, "areaName"); ps.setInt(2, 98); //.... return ps; } }, keyHolder); return keyHolder.getKey().intValue(); }
时间: 2024-11-29 07:33:27