Spring3.0.5 获取表中自增的主键(mysql)
public int addWsstxCotent(final WsstxContent wsstxContent) { final String sql = "insert into wsstx_content(sstx_type,sstx_content,sstx_title,opr_time,ts_tag) values(?,?,?,now(),?)"; // this.jdbcTemplate.update(sql, // new Object[]{wsstxContent.getSstxType(), wsstxContent.getSstxContent(),wsstxContent.getSstxTitle(), wsstxContent.getTsTag()}); KeyHolder keyHolder = new GeneratedKeyHolder(); this.jdbcTemplate.update( new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, wsstxContent.getSstxType()); ps.setString(2, wsstxContent.getSstxContent()); ps.setString(3, wsstxContent.getSstxTitle()); ps.setString(4, wsstxContent.getTsTag()); return ps; } } , keyHolder); int generatedId = keyHolder.getKey().intValue(); return generatedId; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。