import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @Repository public class WaterDataDao { @Resource private JdbcTemplate jdbcTemplate; public List<WaterDataEntity> getAll(){ StringBuffer sql = new StringBuffer(); sql.append(" SELECT TT1.STCD,TT1.STNM,TT1.LGTD,TT1.LTTD,TT2.Z,TT2.TM FROM") .append("( SELECT t.STCD,t.STNM,t.LGTD,t.LTTD FROM ST_STBPRP_B t WHERE t.ADMAUTH!='气象' and t.RVNM='长江') TT1,") .append("( SELECT XX.STCD,XX.tm,XX.z FROM ST_RIVER_R XX,") .append("( SELECT t.STCD, "MAX"(t.TM) maxTM FROM ST_RIVER_R t") .append(" WHERE t.TM < SYSDATE AND t.TM > (SYSDATE - 3/24)") .append(" GROUP BY t.STCD) YY") .append(" WHERE XX.STCD=YY.STCD AND XX.TM = YY.MAXTM) TT2 ") .append(" WHERE TT1.STCD = TT2.STCD"); // RowMapper<WaterDataEntity> rowMapper = new BeanPropertyRowMapper<>(WaterDataEntity.class); // return jdbcTemplate.queryForObject(sql.toString(),rowMapper); final List<WaterDataEntity> waterDataList = new ArrayList<WaterDataEntity>();//在内部匿名类中使用 jdbcTemplate.query(sql.toString(), new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { // TODO Auto-generated method stub WaterDataEntity waterDataEntity = new WaterDataEntity(); waterDataEntity.setStcd(rs.getString("STCD")); waterDataEntity.setStnm(rs.getString("STNM")); waterDataEntity.setLgtd(rs.getBigDecimal("LGTD")); waterDataEntity.setLttd(rs.getBigDecimal("LTTD")); waterDataEntity.setZ(rs.getBigDecimal("Z")); waterDataEntity.setTm(rs.getString("TM")); waterDataList.add(waterDataEntity); } }); return waterDataList; } }