Spring JdbcTemplate - Insert blob and return generated key

后端 未结 11 1792
猫巷女王i
猫巷女王i 2020-12-15 06:06

From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate

final File blobIn = new File(\"spring2004.jpg\");
final InputStream blobIs         


        
11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 06:16

    Another solution with lambda (which is not required):

    jdbcTemplate.update(dbcon -> {
        PreparedStatement ps = dbcon.prepareStatement("INSERT INTO ...");
        ps.setString(1, yourfieldValue);
        ps.setBinaryStream(2, yourInputStream, yourInputStreamSizeAsInt));
        return ps;
    });
    

    NB. Sorry this does not include KeyGenerator.

提交回复
热议问题