利用GeneratedKeyHolder获得新建数据主键值

Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录对应主键值的方法: 

int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)

 

Spring为KeyHolder接口指代了一个通用的实现类GeneratedKeyHolder,该类返回新增记录时的自增长主键值。假设我们希望在新增论坛板块对象后,希望将主键值加载到对象中,则可以按以下代码进行调整:

final String INSERT_SQL = "insert into gtable(username,callback) values(?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(
					Connection connection) throws SQLException {
				PreparedStatement ps = connection.prepareStatement(INSERT_SQL,
						new String[] { "id" });
				ps.setString(1, request.getUsername());
				ps.setString(2, request.getCallback());
				return ps;
			}
		}, keyHolder);
logger.debug("id:" + keyHolder.getKey()+"long:"+keyHolder.getKey().longValue());
 

 

你可能感兴趣的:(Spring)