spring jdbcTemplet相关记录

2017-03-16 10:49:00
1147533288
原创
955
摘要:jdbcTemplet相关坑~

1.获取oracle记录对应rowid,以便实现根据rowid批量更新(场景举例:10000数据,每200条拼一次xml调用对方接口,调用完成后批量更新该200条记录对应状态标志为已完成)

//注意使用ROWIDTOCHAR进行转换文本,否则解析出错。

StringBuffer sql = new StringBuffer();
sql.append("select a.*,ROWIDTOCHAR(a.ROWID) as pk_rowid from ").append(tablename);


2.批量提交举例:

<!--

private void uptRecords(List<String> rowids,String tablename) {
        String sql = "update "+tablename+" a set scbz='1' where a.rowid=?";
        final List<String> rowids1 = rowids;
        jdbc.batchUpdate(sql,new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1,rowids1.get(i));
            }

            @Override
            public int getBatchSize() {
                return rowids1.size();
            }
        });
        log.info("更新记录完毕");
    }

-->

3.批量写入数据库时,报文存放为blob

<!--

LobHandler lobHandler = new DefaultLobHandler();
        jdbc.execute("insert into log_call(id,insertdate,v_in) values(?,?,?)",
                new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                    protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                        ps.setString(1, uuid1);
                        try {
                            lobCreator.setBlobAsBinaryStream(ps, 3, new ByteArrayInputStream(xml1.getBytes("GBK")), xml1.length());
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        ps.setString(2, sdf.format(new Date()));
                    }
                });

-->

4.批量更新时写入blob

<!--

jdbc.execute("update log_call set v_out=?,updatedate=?,result=? where id=?",
                new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                    protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                        ps.setString(3, result1);
                        try {
                            lobCreator.setBlobAsBinaryStream(ps, 1, new ByteArrayInputStream(v_out.getBytes("GBK")), v_out.length());
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        ps.setString(2, sdf.format(new Date()));
                        ps.setString(4, uuid1);
                    }
                });
-->

文章分类
联系我
联系人: meepo
电话: *****
Email: 1147533288@qq.com
QQ: 1147533288