插入数据时获取自增长主键
KeyHolder keyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = con -> {
PreparedStatement ps = con.prepareStatement("insert into enzoCaseTask (caseId,createTime,taskNum,caseName,caseUrl,parameter,env) values " + "(" + "\'" + caseId + "\'," + "\'" + createTime + "\'," + "\'" + taskNum + "\'," + "\'" + caseName + "\'," + "\'" + caseUrl + "\'," + "\'" + parameter + "\'," + "\'" + env + "\'" + ")", Statement.RETURN_GENERATED_KEYS);
return ps;
};
jdbcTemplate.update(preparedStatementCreator, keyHolder);
//getKey()获取
return keyHolder.getKey().intValue();
网友评论