插入数据时获取自增长主键

2018-03-29  本文已影响0人  耗纸锅

插入数据时获取自增长主键

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();

上一篇下一篇

猜你喜欢

热点阅读