0

在一个带有spring的项目中,我想使用以下选择进行更新查询:

select col1, col2 from tab1
where col3 = :param1
for update of col4, col5

随着以下更新

update tab1
set col4 = :col4, col5 = :col5
where current of curscom

curscom光标的名称在哪里。

可以curscom是常量还是每次运行查询时都必须更改名称?一个人将如何实现这一点?

我可以访问命名参数 jdbc 模板,但由于我无法控制的架构原因,我不能“超出”它(我不能只打开文档中定义的原始连接)。

一种可能性似乎是在相关的执行方法中使用准备好的语句回调并在那里设置游标名称,例如

模型

public class SelectRowModel {
    // attributes, getters, setters, etc
}

服务

private final NamedParameterJdbcTemplate template;

...

public List<SelectRowModel> selectForUpdate(
        String select,
        Map<String, Object> selectParams,
        String update,
        Map<String, Object> updateParams,
        String cursorName) {
    return template.execute(select, selectParams, preparedStatement -> {
        preparedStatement.setCursorName(cursorName);
        preparedStatement.execute();
        try (ResultSet rs = preparedStatement.getResultSet()) {
            List<SelectRowModel> models = new ArrayList<>();
            while (rs.next()) {
                SelectRowModel m = new SelectRowModel();
                // populate model
                models.add(m);
            }
            // add the cursor as a param in the updateParams map? can it be done?
            template.update(update, updateParams);
            return models;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
4

0 回答 0