2012-07-10 46 views
2

我需要大量的XML數據存儲到CLOB和CLOB轉換回字符串使用的Clob與Spring NamedParameterJdbc模板

但問題是,我們正在使用(春季NamedParameterJdbc模板)

SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
      positionResponsesDO); 
this.jdbcTemplate.update(sql, paramSource); 

凡PositionResponsesDO有獲取和設置屬性

private Clob xmlData; 

現在我需要字符串數據(大)轉換爲CLOB和Clob爲String。 請建議我最好的方法。

我不能用文件操作,因爲它是Web應用程序

回答

3

您可以使用LobHandlerLobCreator採取的CLOB和斑點,把它們變成別的東西。

Spring documentation在這裏討論它們。

爲CLOB或BLOB列轉換的東西,你可以使用一個NamedParameterJdbcTemplate方法query()採用一個RowMapper

LobHandler lobHandler = new DefaultLobHandler(); 
jdbcTemplate.query(sql, paramSource, 
    new RowMapper<Void>() { 
     public Void mapRow(ResultSet rs, int i) throws SQLException { 
      String clobText = lobHandler.getClobAsString(rs, "clobColumnName");             
      byte[] blobBytes = lobHandler.getBlobAsBytes(rs, "blobColumnName");             
      return null; 
     } 
    }); 
+0

如果我們正在使用RowMapper檢索結果,它將起作用。 但是如何將它與BeanPropertySqlParameterSource一起使用。因爲這個Spring實用程序將bean屬性映射到db列。所以,如果我用字符串標記bean屬性(db中的相應列是Clob),那會起作用嗎? – Reddy 2012-07-11 05:08:32

+0

也許你應該先嚐試使用它... – 2012-07-11 12:30:56