美文网首页
插入数据时获取自增长主键

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

作者: 耗纸锅 | 来源:发表于2018-03-29 21:58 被阅读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();

    相关文章

      网友评论

          本文标题:插入数据时获取自增长主键

          本文链接:https://www.haomeiwen.com/subject/hjivcftx.html