Spring JdbcTemplate - 插入 blob 并返回生成的密钥
从 Spring JDBC 文档中,我知道如何 插入使用 JdbcTemplate 的 blob
final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_blob) VALUES (?, ?)",
new AbstractLobCreatingPreparedStatementCallback(lobhandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException {
ps.setLong(1, 1L);
lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length());
}
}
);
blobIs.close();
以及如何 检索新插入行的生成键:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"id"});
ps.setString(1, name);
return ps;
}
},
keyHolder);
// keyHolder.getKey() now contains the generated key
有没有办法将两者结合起来?
From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate
final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_blob) VALUES (?, ?)",
new AbstractLobCreatingPreparedStatementCallback(lobhandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException {
ps.setLong(1, 1L);
lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length());
}
}
);
blobIs.close();
And also how to retrieve the generated key of a newly inserted row:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"id"});
ps.setString(1, name);
return ps;
}
},
keyHolder);
// keyHolder.getKey() now contains the generated key
Is there a way I could combine the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我来这里寻找相同的答案,但对所接受的答案不满意。所以我做了一些挖掘,想出了这个解决方案,我在 Oracle 10g 和 Spring 3.0 中测试过,
这还需要以下抽象类,部分基于 Spring 的 AbstractLobCreatingPreparedStatementCallback
另外,您在 Oracle 中创建的表应该有一个 auto - 使用序列和触发器增加 id 的列。触发器是必要的,因为否则你必须使用Spring的NamedParameterJdbcOperations(在SQL中执行sequence.nextval),它似乎不支持KeyHolder(我用它来检索自动生成id)。有关详细信息,请参阅此博客文章(不是我的博客):
I came here looking for the same answer, but wasn't satisfied with what was accepted. So I did a little digging around and came up with this solution that I've tested in Oracle 10g and Spring 3.0
this also requires the following abstract class, based in part on Spring's AbstractLobCreatingPreparedStatementCallback
Also, the table you create in Oracle should have an auto-incremented column for the id using a sequence and trigger. The trigger is necessary because otherwise you'd have to use Spring's NamedParameterJdbcOperations (to do the sequence.nextval in your SQL) which doesn't seem to have support for KeyHolder (which I use to retrieve the auto-gen id). See this blog post (not my blog) for more info: http://www.lifeaftercoffee.com/2006/02/17/how-to-create-auto-increment-columns-in-oracle/
所有这一切对我来说似乎太复杂了。这很有效而且很简单。它使用org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
All of this seemed way too complicated to me. This works and is simple. It uses
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
我最终只执行了两个查询,一个用于创建行,一个用于更新 blob。
查看 Spring 源代码并提取所需的部分,我得出这样的结论:
我不能说我完全理解这里发生的事情。我不确定在这个简单的情况下是否需要使用复杂的方法来提取生成的密钥,而且我也不完全清楚当代码变得如此复杂时使用 JdbcTemplate 的好处。
无论如何,我测试了上面的代码并且它有效。就我而言,我认为这会使我的代码过于复杂。
I ended up just performing two queries, one to create the row and one to update the blob.
Looking at the Spring source code and extracting the needed parts, I came up with this:
I can't say I fully understand what is going on here. I'm not sure if the complicated method to extract the generated key is necessary in this simple case, and I'm not entirely clear about the benefit of even using JdbcTemplate when the code gets this hairy.
Anyway, I tested the above code and it works. For my case, I decided it would complicate my code too much.
2012 年,
SimpleJdbcTemplate
已弃用。这就是我所做的:SQL 如下所示:
In 2012,
SimpleJdbcTemplate
is deprecated. This is what I did:The SQL looks like this:
这仅在MySql上进行了测试,我只粘贴了相关部分。
运行我的测试类后,结果如下所示:
“通过 template.update(psc,kh) 添加记录:添加 1 条记录并获取密钥 36”
This is tested on MySql only and I only pasted the relevant part.
After Running my test class, the result is shown below:
"record added via template.update(psc,kh): 1 added and got key 36"
如果你的底层数据库是mysql,你可以自动生成你的主键。然后要将记录插入到数据库中,可以使用以下语法进行插入:
In case your underlying database is mysql, you can autogenerate your primary key. Then to insert a record into your db, you can use the following syntax for insertion:
另一种使用 lambda 的解决方案(这不是必需的):
注意。抱歉,这不包括密钥生成器。
Another solution with lambda (which is not required):
NB. Sorry this does not include KeyGenerator.
也许有些是这样的:
Maybe some like this:
请使用:
Please use: