首页 > 代码库 > SpringJDBC jdbcTemplate获取自增主键

SpringJDBC jdbcTemplate获取自增主键

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()); // 获取自增主键


SpringJDBC jdbcTemplate获取自增主键