JdbcTemplate中的封装了很多实用的方法。
首先来介绍一下用 JdbcTemplate来查询数据的方法。
1.返回指定对象类型的List (query方法)
看代码:
- public List getProductModle(String value) throws DataAccessException {
- List resultList = new ArrayList();
- List args = new ArrayList();
- String sql = "select F_CPUT_MOD_ID as id ,F_CPUT_MOD_NAME as name from CPUT_MOD_T" +
- " WHERE F_MARKET_LOCA_ID = ? order by F_CPUT_MOD_ID" ;
- if (StringUtils.isNotEmpty(value)){
- args.add(value);
- }
- resultList = this .getJdbcTemplate().query(sql,args.toArray(),
- new ProductModleRowMapper());
- return resultList;
- }
- /**
- * @author rockjava
- * @description 封装产品型号结果集
- */
- private class ProductModleRowMapper implements RowMapper{
- public Object mapRow( final ResultSet rs, final int rowNum) throws SQLException {
- ProductModleVO vo = new ProductModleVO();
- vo.setProModleId((String)rs.getString( "id" ));
- vo.setProModleName((String)rs.getString( "name" ));
- return vo;
- }
- }
这里实现了RowMapper类的mapRow方法,用来把查询到的结果集用指定的类来封装,最后返回一个List,List中装着
我们自定的值对象。
2.返回指定类型的结果
(queryForObject方法)
- public String getCurrentPhase(String arg) throws DataAccessException {
- String currentPhase = "" ;
- String sql= "select F_CURR_YEAR from COMPETITION_BASE_T where F_COMPETITION_ID=?" ;
- Object[] o ={arg};
- try {
- currentPhase = (String) this .getJdbcTemplate().queryForObject(sql,o, String. class );
- } catch (Exception e) {
- currentPhase = "" ;
- e.printStackTrace();
- }
- return currentPhase;
- }
在这个例子中,用到了queryForObject这个方法,其中第一个参数是要执行的sql,第二个参数是Object数组类型(其中装的是sql脚本用到
的参数),第三个参数就是要制定返回结果的类型(这里我定义的是String类型)
3.查询结果返回Int类型
(queryForInt方法)
- public boolean doCheckCompete(Map args) throws DataAccessException {
- ....省略代码
- /**sql脚本用到的参数集合*/
- Object[] args = new Object[]{
- companyId,
- competitionId,
- marketType,
- martID,
- currentPhase
- };
- StringBuffer sql = new StringBuffer();
- sql.append( "SELECT COUNT ( * ) " );
- sql.append( "FROM BEFOR_ORDER_T t " );
- sql.append( "WHERE T.F_COMPANY_ID = ? " );
- sql.append( " and T.F_COMPETITION_ID=? " );
- sql.append( " and T.F_MARKET_LOCA_ID=? " );
- sql.append( " and T.F_MARKET_SCOPE_ID=? " );
- sql.append( " and T.F_CURRENT_PHASE = ? " );
- int i= 0 ;
- i = this .getJdbcTemplate().queryForInt(sql.toString(), args);
- if (i> 0 ){
- return true ;
- }
- return false ;
- }
这里用的是queryForInt方法返回int类型。
4.查询结果返回Map类型 (queryForMap方法)
- String sql = "select T.F_COMPETE_ORDER_STATE ,T.F_GENERATED_ORDERS " +
- "from COMPETITION_BASE_T t " +
- "where T.F_COMPETITION_ID = ?" ;
- Map state = (Map) this .getJdbcTemplate().queryForMap(sql, new Object[]{args});
- String compete_order_state = state.get( "F_COMPETE_ORDER_STATE" ).toString();
- String generated_orders = state.get( "F_GENERATED_ORDERS" ).toString();
返回后的map中的键值对应的是select语句中的字段名字。
5.查询结果集直接返回list
(queryForList方法)
- List list = new ArrayList();
- String sql= "select T.F_OFF_PERIOD,T.F_PROBABILITY from ORDER_OFF_PERIOD_T t " +
- "where T.F_COMPETITION_ID=?" ;
- Object[] args ={arg};
- try {
- list = this .getJdbcTemplate().queryForList(sql, args);
- } catch (Exception e) {
- e.printStackTrace();
- }
- ...代码省略
- String a = (String)(((Map) list.get( 0 )).get( "F_PROBABILITY" ));
- String b = (String)(((Map) list.get( 1 )).get( "F_PROBABILITY" ));
- String c = (String)(((Map) list.get( 2 )).get( "F_PROBABILITY" ));
返回的List中装载的是Map对象,没个map对象中的键值对应sql脚本的字段名字。