用jdbcTempate調用存儲過程,處理BLOBCLOB小記

 用jdbcTempate調用存儲過程,處理BLOB/CLOB小記

 
 
1、利用spring的jdbcTemplate調用存儲過程 
假如我們有P_GET_TOPIC_NUM這一個存儲過程,有兩個參數,第一個參數userId是傳進去的,第二個參數是傳出來的,舉例如下: 
 
Java代碼  
public int getUserTopicNum(final int userId) {  
    String sql = "{call P_GET_TOPIC_NUM(?,?)}";  
    //使用?   Object execute(String callString, CallableStatementCallback action)接口  
    Object obj = getJdbcTemplate().execute(sql,new CallableStatementCallback(){  
        public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {  
            cs.setInt(1,userId);  
            cs.registerOutParameter(2, Types.INTEGER);  
            cs.execute();  
            return new Integer(cs.getInt(2));  
        }     
    });  
    return ((Integer)obj).intValue();  
}  
 
2、spring定義了一個以統一的方式操作各種數據庫的Lob類型數據的LobCreator(保存的時候用),同時提供了一個LobHandler爲操作二進制字段和大文本字段提供統一接口訪問。 
舉例,例子裏面的t_post表中post_text字段是CLOB類型,而post_attach是BLOG類型: 
 
Java代碼  
public class PostJdbcDao extends JdbcDaoSupport implements PostDao {  
    private LobHandler lobHandler;  
    private DataFieldMaxValueIncrementer incre;  
    public LobHandler getLobHandler() {  
        return lobHandler;  
    }  
    public void setLobHandler(LobHandler lobHandler) {  
        this.lobHandler = lobHandler;  
    }  
    public void addPost(final Post post) {        
        String sql = " INSERT INTO t_post(post_id,user_id,post_text,post_attach)"  
                + " VALUES(?,?,?,?)";  
        getJdbcTemplate().execute(  
                sql,  
                new AbstractLobCreatingPreparedStatementCallback(  
                        this.lobHandler) {  
                    protected void setValues(PreparedStatement ps,  
                            LobCreator lobCreator) throws SQLException {  
                        ps.setInt(1, incre.nextIntValue());   
                        ps.setInt(2, post.getUserId());   
                        lobCreator.setClobAsString(ps, 3, post.getPostText());  
                        lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());  
                    }  
                });  
    }  
}  
設置相對應的配置文件(Oracle 9i版本),Oracle的數據庫最喜歡搞搞特別的東西啦: 
Java代碼  
<bean id="nativeJdbcExtractor"  
    class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"  
    lazy-init="true" />  
<bean id="oracleLobHandler"  
    class="org.springframework.jdbc.support.lob.OracleLobHandler"  
    lazy-init="true">  
    <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />  
</bean>  
<bean id="dao" abstract="true">  
    <property name="jdbcTemplate" ref="jdbcTemplate" />  
</bean>  
<bean id="postDao" parent="dao"  
    class="com.baobaotao.dao.jdbc.PostJdbcDao">  
    <property name="lobHandler" ref="oracleLobHandler" />  
</bean>  
 
Oracle 10g或其他數據庫如下設置: 
Java代碼  
<bean id="defaultLobHandler"  
    class="org.springframework.jdbc.support.lob.DefaultLobHandler"  
    lazy-init="true" />  
<bean id="dao" abstract="true">  
    <property name="jdbcTemplate" ref="jdbcTemplate" />  
</bean>  
<bean id="postDao" parent="dao"  
    class="com.baobaotao.dao.jdbc.PostJdbcDao">  
    <property name="lobHandler" ref="defaultLobHandler" />  
</bean>  
 
 
讀取BLOB/CLOB塊,舉例: 
Java代碼  
public List getAttachs(final int userId){  
    String sql = "SELECT post_id,post_attach FROM t_post where user_id =? and post_attach is not null";  
    return getJdbcTemplate().query(  
            sql,new Object[] {userId},  
            new RowMapper() {  
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {  
                    Post post = new Post();  
                    int postId = rs.getInt(1);  
                    byte[] attach = lobHandler.getBlobAsBytes(rs, 2);  
                    post.setPostId(postId);  
                    post.setPostAttach(attach);  
                    return post;  
                }  
            });  
}  
 
 
 
注:代碼均來自<<精通spring2.x企業應用開發詳解>>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章