SpringJDBC更新数据库的代码

1.批量更新

@Override

@Transactional(propagation=Propagation.REQUIRED)

public int[] insertFc(FilingFCSegmentCorrection fc,List<String> ruleCds) {

final List<MapSqlParameterSource> list=new ArrayList<MapSqlParameterSource>();

for(final String ruleCd:ruleCds){

final MapSqlParameterSource mapSql=new MapSqlParameterSource();

mapSql.addValue("inYr",fc.getInEffYr());

mapSql.addValue("carryCd",fc.getCarrierCode());

mapSql.addValue("tariffCd",fc.getRuleTariffCode());

mapSql.addValue("seqCd",fc.getSeqNo());

mapSql.addValue("zoneCd",fc.getZoneNO());

mapSql.addValue("ruleCd",ruleCd);

list.add(mapSql);

}

MapSqlParameterSource[] params=list.toArray(new MapSqlParameterSource[list.size()] );

 

return nameJdbcTemplate.batchUpdate(insertFCs,params);

}

 

2.删除

@Override

@Transactional(propagation = Propagation.REQUIRED)

public boolean deleteDDCData(final WorkUnitKeyInfo wuKeyInfo,

final FilingGroup flgGrp, final List<String> ruleCodeList) {

final MapSqlParameterSource msps = new MapSqlParameterSource();

msps.addValue(DDSConstants.CARRIER_CODE, flgGrp.getCarrier());

msps.addValue(DDSConstants.RULE_TARIFF_CODE, flgGrp.getRulesTariff());

msps.addValue(FilingConstants.RULECDLIST, ruleCodeList);

msps.addValue(DDSConstants.WORK_UNIT_OWN_ORG_CD, wuKeyInfo.getWuOwnOrgCode());

msps.addValue(DDSConstants.WORK_UNIT_CRTE_DT, wuKeyInfo.getWuCreateDt());

msps.addValue(DDSConstants.WORK_UNIT_ID_NO, wuKeyInfo.getWuIdNo());

 

final boolean ddcTextStatus = namedJdbcTemplate.update(

sqlDelDDCText, msps)>0;

 

final boolean ddcStatus = namedJdbcTemplate.update(

sqlDelDDC, msps) > 0;

 

return (ddcTextStatus && ddcStatus);

}

其中MapSqlParameterSource可换成HashMap

 

 3.单个参数

final String UPDATE_SQL = "UPDATE DDSGRP.WORK_UNIT set REL_NO = REL_NO + 1, "

+ " LAST_MNT_ACT = 'U'"

+ ",LAST_MNT_ID = ? "

+ ",LAST_MNT_TS = CURRENT TIMESTAMP "

+ "WHERE WORK_UNIT_SG = ?";

namedJdbcTemplate.getJdbcOperations().update(UPDATE_SQL,ThreadLocalContext.getUserId().toUpperCase(),wuSg);

 

4.

namedJdbcTemplate

.getJdbcOperations().execute( insertFtnt);

你可能感兴趣的:(批量操作,SpringJDBC,更新数据库)