JDBC之——JDBCTemplate添加记录获取自增ID

1
2
3
4
5
6
7
8
String insertSql = "insert into user(name,age) values (:name,:age)";
User user = new User();
user.setName("test");
user.setAget(18);

KeyHolder keyHolder = new GeneratedKeyHolder();
int insertResult = namedParameterJdbcTemplate.update(insertSql, new BeanPropertySqlParameterSource(user), keyHolder);
int userId = keyHolder.getKey().longValue();