zoukankan      html  css  js  c++  java
  • JdbcTemplate使用

    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;
        }
    
    }
  • 相关阅读:
    Gym 100989E
    解决Pycharm无法导入包问题 Unresolved reference
    模块介绍/time/os...
    列表/元组/字符串/字典/集合/文件/字符编码
    迭代器/生成器/装饰器 /Json & pickle 数据序列化
    network / shuangwangka / inner + outer
    performance python httprunner
    performance python locust
    web sec / fastjson safemode
    python 向下取整,向上取整,四舍五入
  • 原文地址:https://www.cnblogs.com/unique1319/p/7691557.html
Copyright © 2011-2022 走看看