日期:2014-05-16  浏览次数:20483 次

jdbc 批量添加 batch

/*

*应用在需要一次插入多行,表示两个对象的关联

*/

//修改或删除

public void saveRoleConfig(final Role role , final List<Integer> fdIds){
??if(fdIds==null){
???String sql_;
???if(role.getOid()>0){
????sql_="update C_SYS_ROLE set ROLE_NAME='"+role.getName()+
?????"',ROLE_DESC='"+role.getDesc()+"',ROLE_TYPE="+role.getType()+",STATUS="+role.getStatus()+" where Oid="+role.getOid();
???}
???else {
????sql_="insert into C_SYS_ROLE r (r.Oid,r.ROLE_NAME,r.ROLE_DESC,r.ROLE_TYPE,r.STATUS)" +
????" values (func_sequence.nextval,'"+role.getName()+"','"+role.getDesc()+"',"+role.getType()+","+role.getStatus()+")";
???}
???this.execute(sql_);
???return;
??}
??StringBuffer sb = new StringBuffer();
??if(role.getOid()>0){
???sb.append("update C_SYS_ROLE r");
???sb.append(" set r.ROLE_NAME=?,r.ROLE_DESC=?,");
???sb.append(" r.ROLE_TYPE=?,");
???sb.append(" r.STATUS=?");
???sb.append(" where r.OID="+role.getOid());
??}
??else{
???sb.append("insert into C_SYS_ROLE r");
???sb.append("(r.Oid,");
???sb.append(" r.ROLE_NAME,r.ROLE_DESC,");
???sb.append(" r.ROLE_TYPE,");
???sb.append(" r.STATUS)");
???sb.append(" values (");
???sb.append("func_sequence.nextval,");
???sb.append("?,?,?,?)");
??}
??final String sqll = sb.toString();
??//this.update(sqll, new Object[]{role.getName(),role.getDesc(),role.getType(),role.getStatus()});
??
??if(role.getOid()>0){
???getJdbcTemplate().update(new PreparedStatementCreator() {
????public PreparedStatement createPreparedStatement(Connection conn)
??????throws SQLException {
?????int i = 0;
?
?????java.sql.PreparedStatement ps = conn.prepareStatement(sqll,new String[] {"oid"});
?????ps.setString(++i, role.getName());
?????ps.setString(++i, role.getDesc());
?????ps.setInt(++i, role.getType());
?????ps.setInt(++i, role.getStatus());?
?????return ps;
????}
???});
???String sql = "insert into C_SYS_ROLE_FUNC_MAP(ROLE_OID,FUNC_OID) values(?,?)";
???this.batchUpdate(sql, new FuncBatchPreparedStatementSetter(role.getOid(), fdIds));?
??}
??else{
???KeyHolder keyHolder = new GeneratedKeyHolder();
???getJdbcTemplate().update(new PreparedStatementCreator() {
????public PreparedStatement createPreparedStatement(Connection conn)
??????throws SQLException {
?????int i = 0;
?
?????java.sql.PreparedStatement ps = conn.prepareStatement(sqll,new String[] {"oid"});
?????ps.setString(++i, role.getName());
?????ps.setString(++i, role.getDesc());
?????ps.setInt(++i, role.getType());
?????ps.setInt(++i, role.getStatus());
?????return ps;
????}
???}, keyHolder);
?
???String sql = "insert into C_SYS_ROLE_FUNC_MAP(ROLE_OID,FUNC_OID) values(?,?)";
???this.batchUpdate(sql, new FuncBatchPreparedStatementSetter(keyHolder.getKey().longValue(), fdIds));?
??}
??
?}

?

//批量添加

private class FuncBatchPreparedStatementSetter implements
???BatchPreparedStatementSetter {
??final List<Integer> temList;
??final long wSetId;

??
??public FuncBatchPreparedStatementSetter(long roleId,
????List<Integer> list) {
???temList = list;
???wSetId = roleId;
??}

??public int getBatchSize() {
???return temList.size();
??}

??public void setValues(PreparedStatement ps, int i) throws SQLException {
???ps.setLong(1, wSetId);
???ps.setLong(2, temList.get(i).longValue());
??}
?}