springJDBC的几种查询数据库方法

1.RowCallbackHandler

@Override

public List<FilingFCSegmentCorrection> retrieveFCSegmentCorrections(Integer year,String ruleTariffCode,

String carrierCode) {

final Map<String,Object> sqlParamaterMap= new HashMap<String,Object>();

sqlParamaterMap.putAll(setTCToParaMap(ruleTariffCode,carrierCode));

sqlParamaterMap.put("year",year);

final List<FilingFCSegmentCorrection> fcList=new ArrayList<FilingFCSegmentCorrection>();

nameJdbcTemplate.query(sqlGetFCs,sqlParamaterMap,new RowCallbackHandler() {

 

@Override

public void processRow(ResultSet rs) throws SQLException {

final FilingFCSegmentCorrection fc=new FilingFCSegmentCorrection();

fc.setCarrierCode(rs.getString("CXR_CD"));

fc.setRuleTariffCode(rs.getString("RULE_TAR_CD"));

fc.setInEffYr(rs.getInt("IN_EFF_YR"));

fc.setRuleCode(rs.getString("RULE_CD"));

fc.setSeqNo((Integer) rs.getObject("SEQ_NO"));

fc.setZoneNO(rs.getInt("ZONE_NO"));

fcList.add(fc);

}

});

return fcList;

}

 

2.RowMapper

@Override

public List<FilingFCSegmentCorrection> retrieveFCSegmentCorrections(Integer year,String ruleTariffCode,

String carrierCode) {

final Map<String ,Object> sqlParamaterMap=setTCToParaMap(ruleTariffCode,carrierCode);

sqlParamaterMap.put("year",year);

final RowMapper<FilingFCSegmentCorrection> rowMapper=new RowMapper<FilingFCSegmentCorrection>() {

 

@Override

public FilingFCSegmentCorrection mapRow(ResultSet result, int arg1) throws SQLException {

final FilingFCSegmentCorrection fc=new FilingFCSegmentCorrection();

setCommonFieldsForFc(result,fc);

fc.setSeqNo((Integer) result.getObject("SEQ_NO"));

fc.setZoneNO(result.getInt("ZONE_NO"));

return fc;

}

};

return nameJdbcTemplate.query(sqlGetFCs,sqlParamaterMap,rowMapper);

}

sqlGetFCs

SELECT IN_EFF_YR,CXR_CD,RULE_TAR_CD,RULE_CD,SEQ_NO ,ZONE_NO FROM FC_SEG_CRRN

WHERE IN_EFF_YR=:year and RULE_TAR_CD= :tariff and CXR_CD= :carry

 

3.ResultSetExtractor

@Override

 

public List<String> getRuleCodes(final List<Integer> batchSgList) {

final MapSqlParameterSource msps = new MapSqlParameterSource();

msps.addValue(FilingConstants.BATCHSGS, batchSgList);

final List<String> ruleCodeSet = new ArrayList<String>();

return namedJdbcTemplate.query(sqlGetRuleCodes, msps,

new ResultSetExtractor<List<String>>() {

@Override

public List<String> extractData(final ResultSet arg0)

throws SQLException, DataAccessException {

while (arg0.next()) {

ruleCodeSet.add(arg0.getString("RULE_CD"));

}

return ruleCodeSet;

}

});

 

}

 

附dao类:

public

abstract class DDSDAO {

@Autowired

protected transient NamedParameterJdbcTemplate nameJdbcTemplate;

 

protected JdbcTemplate getJdbcTemplate() {

return (JdbcTemplate) this.nameJdbcTemplate.getJdbcOperations();

}

 

}

4.getJdbcOperations   queryForList

 selectPrimaryKey:
             SELECT distinct
    A.WORK_UNIT_OWN_ORG_CD wuOwnOrgCode
    ,A.WORK_UNIT_CRTE_DT wuCreatDateTime
    ,A.WORK_UNIT_ID_NO wuIdNumber
    ,A.ERR_WARN_SCOP_TYPE_CD errWarnScopeTypeCode
    ,A.REL_NO releaseNumber
    ,A.ERR_TYPE_CD errTypeCode
    FROM ${ddsSchema}WORK_UNIT_ERR_WARN A INNER
    JOIN ${ddsSchema}WU_VLDTN_PRCS_TYPE B
    ON A.WU_VLDTN_PRCS_TYPE_CD = B.WU_VLDTN_PRCS_TYPE_CD
    WHERE A.WU_VLDTN_PRCS_TYPE_CD = ?
    AND A.FILG_GRP_NO = ?
    AND A.WORK_UNIT_OWN_ORG_CD = ?
    AND A.WORK_UNIT_CRTE_DT = ?
    AND A.WORK_UNIT_ID_NO = ?
final Object[] parameters = new Object[] {processCode, filingGroupNum, wuKeyInfo.getWuOwnOrgCode(), wuKeyInfo.getWuCreateDt(), wuKeyInfo.getWuIdNo()};
final List<Map<String, Object>> priKeyMapList = namedJdbcTemplate.getJdbcOperations().queryForList(selectPrimaryKey, parameters);
 

 

 =========================================

 

 

附另外查询方法:queryForList,queryForObject

 

final String createId = namedJdbcTemplate.query(sqlSelectCategoryLock,

lockRequest.getSqlParamaterMap(),

new ResultSetExtractor<String>() {

@Override

public String extractData(final ResultSet rs)

throws SQLException, DataAccessException {

if (rs.next()) {

return rs.getString(FilingConstants.CREATE_ID_COLUMN);

} else {

return null;

}

}

});

也可以写一个类继承ResultSetExtractor,覆写extractData()方法,然后把这个类代替new ResultSetExtractor()放进query()方法里。

 

@Override

 

public List<String> retrieveBtchTypeList(final WorkUnitKeyInfo wuKeyInfo) {

final Map<String, Object> sqlParamaterMap = new HashMap<String, Object>();

sqlParamaterMap.put(DDSConstants.WORK_UNIT_OWN_ORG_CD,wuKeyInfo.getWuOwnOrgCode());

sqlParamaterMap.put(DDSConstants.WORK_UNIT_CRTE_DT, wuKeyInfo.getWuCreateDt());

sqlParamaterMap.put(DDSConstants.WORK_UNIT_ID_NO, wuKeyInfo.getWuIdNo());

return namedJdbcTemplate.queryForList(sqlBtchTypeList, sqlParamaterMap,String.class);

}

====(sqlBtchTypeList:SELECT DISTINCT WORK_UNIT_BTCH_TYPE_CD    ${ddsSchema}WORK_UNIT_SUMRY  WHERE  WORK_UNIT_OWN_ORG_CD = :wuOwnOrgCode    AND WORK_UNIT_CRTE_DT = :wuCreatDateTime )

 

public Integer comparesubsRec3TS(final Integer wuSg, final String appTypeCode){

final Map<String, Object> updateWuStatusMap = new HashMap<String, Object>();

updateWuStatusMap.put(DDSConstants.WUSG, wuSg);

updateWuStatusMap.put(DDSConstants.APPTYPECODE, appTypeCode);

return namedJdbcTemplate.queryForObject(sqlSubsRec3TSCompare,

updateWuStatusMap,Integer.class);

}

 

@Override  

totFareBatchCntSql:SELECT BATCH_SG, SUM(CNT) AS B_CNT FROM CTR  GROUP BY BATCH_SG)

public Map<Integer,Integer> getCountByBatch(final FilingBatchInfo fbInfo) {

        final Map<Integer,Integer> resMap = new HashMap<Integer, Integer>();

        final MapSqlParameterSource msps = new MapSqlParameterSource();

        if(fbInfo.isFareBatchesExists()) {

        msps.addValue(FilingConstants.BATCHSGS, fbInfo.getFareBatchSgList());

        namedJdbcTemplate.query(totFareBatchCntSql, msps,

        new ResultSetExtractor<Map<Integer,Integer>>() {

        @Override

        public Map<Integer,Integer> extractData(final ResultSet arg0)

        throws SQLException, DataAccessException {

        while (arg0.next()) {

        resMap.put((Integer)arg0.getObject(BATCH_SG), (Integer)arg0.getObject(B_CNT));

        }

        return resMap;

        }

        });

}

 

queryForMap

final String sqlLock = (String)applicationContext.getBean("sqlLock");

final Map<String,Object> actResult = namedJdbcTemplate.getJdbcOperations().queryForMap(sqlLock);

Assert.assertTrue(BOOLEAN_ASSERTION, FilingJUnitUtil.isObjectValueEqual(userId,actResult.get(FilingConstants.CREATE_ID_COLUMN).toString().trim()));

附:sqlLock

select create_id from ${parSchema}ftnt_appl_lock_cat

where cxr_cd = 'ATC' and

ftnt_tar_cd = 'ATPPCOL' and

ftnt_cd = 'AB' and

cat_no = 1

 

queryForList

final List<?> actResult = namedJdbcTemplate.getJdbcOperations().queryForList(sqlUnlock);

Assert.assertTrue(BOOLEAN_ASSERTION, FilingJUnitUtil.isObjectValueEqual(0,actResult.size()));

附:sqlUnlock

select create_id from ${parSchema}ftnt_appl_lock_cat

where cxr_cd = 'ATP' and

ftnt_tar_cd = 'ATPPCOL' and

ftnt_cd = 'AB' and

cat_no = 1

====

List<Map<String,Object>> lockList

= namedJdbcTemplate.getJdbcOperations()

.queryForList(sqlTCRCatLock, carrierCode, tariffCode);

附:sqlTCRCatLock

SELECT cxr_cd, rule_tar_cd, rule_cd

FROM

PARGRP.rule_appl_lock_cat

WHERE cxr_cd = ? AND rule_tar_cd = ?  (在有很多条数据的时候用到)

如果只有一条数据则用queryForMap

 Map<String, Object> actlResult = namedJdbcTemplate.getJdbcOperations().queryForMap(selectFCGovtInsert); 
selectFCGovtInsert:
SELECT * FROM ${gfsSchema}FC_GOVT_FILG_CRRN WHERE IN_EFF_YR=2014 AND CXR_CD='AA' AND RULE_TAR_CD ='AATCP' AND RULE_CD='A' AND FC_CD ='B'  AND SEQ_NO =2 AND SEG_ORD_NO=-1 

==================================================================

final List<String> actualStr = namedJdbcTemplate.getJdbcOperations().queryForList(sqlForJust, String.class);
      
sqlForJust= SELECT ERR_MSG_DESC_TEXT FROM ${ddsSchema}WORK_UNIT_ERR_WARN WHERE WORK_UNIT_OWN_ORG_CD = 'ATP' AND WORK_UNIT_ID_NO=9 AND REL_NO =1

 

你可能感兴趣的:(spring,jdbc,查询数据库)