private JdbcTemplate jdbcTemplate;
public Resource createResource(final Resource resource) {
final String sql = "insert into sys_resource(name, type, url, permission, parent_id, parent_ids, available) values(?,?,?,?,?,?,?)";
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
int count = 1;
psst.setString(count++, resource.getName());
psst.setString(count++, resource.getType().name());
psst.setString(count++, resource.getUrl());
psst.setString(count++, resource.getPermission());
psst.setLong(count++, resource.getParentId());
psst.setString(count++, resource.getParentIds());
psst.setBoolean(count++, resource.getAvailable());
return psst;
}
}, keyHolder);
resource.setId(keyHolder.getKey().longValue());
return resource;
}