SpringJDBC jdbcTemplate获取自增主键

时间:2017-06-18 10:23:29   收藏:0   阅读:1337

String sql = "insert into notice_type(typeName,configType,state,creatorID,corpID,createTime) values (?,?,?,?,?,?)";

Object[] objs = new Object[]{

noticeType.getTypename(),

noticeType.getConfigtype(),

noticeType.getState(),

noticeType.getCreatorid(),

noticeType.getCorpid(),

noticeType.getCreatetime()

};

// 参数对应类型

int[] types = new int[]{

Types.VARCHAR,

Types.VARCHAR,

Types.INTEGER,

Types.VARCHAR,

Types.VARCHAR,

Types.TIMESTAMP

};

KeyHolder keyHolder = new GeneratedKeyHolder(); // 自增主键Holder

PreparedStatementCreatorFactory pscFactory = new PreparedStatementCreatorFactory(sql, types);

pscFactory.setReturnGeneratedKeys(true); // 返回自增主键

PreparedStatementCreator psc = pscFactory.newPreparedStatementCreator(objs);

int i = getJdbcTemplate().update(psc, keyHolder);

noticeType.setId(keyHolder.getKey().longValue()); // 获取自增主键


原文:http://881206524.blog.51cto.com/10315134/1939394

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!