如何将ResultSetExtractor用于JdbcTemplate

时间:2018-04-17 22:56:12

标签: java sql

我有像这样的SQL

truck.selectFullTruckDetail = select * from truck t 
left join orderz o 
on t.truckId = o.truckId  
order by t.truckId

返回卡车列表和与下列卡车相匹配的订单

TRUCKID     TRUCKCODE   PURCHASEDDATE   DESCRIPTION     ORDERID     PETROLQTY   ORDERDATE   TRUCKID

我正在尝试实现一对多的关系,它应该返回表和所有信息,所以这样做并且有这样的类 我得 fullTruckDetailsql 总是为null,它说我的sql语句总是为null。我的方法有什么问题。

List<TruckDetail> getTruckDetails  = namedParameterJdbcTemplate.query(fullTruckDetailsql,
        new ResultSetExtractor<List<TruckDetail>>() {

    @Override
    public List<TruckDetail> extractData(ResultSet rs) throws SQLException, DataAccessException {
        Map<Integer, TruckDetail> map = new HashMap<Integer, TruckDetail>();
        TruckDetail truckDetail;
        while (rs.next()) {
            Integer id = rs.getInt("truckId");
            truckDetail = map.get(id);

            if(truckDetail == null){

                String description = rs.getString("descriptions");
                String truckCode =  rs.getString("truckCode");
                Date purchasedDate = rs.getDate("purchasedDate");
                Double avgPerMonth = rs.getDouble("avgPerMonth");

                truckDetail = new TruckDetail(id,truckCode, purchasedDate, description,avgPerMonth);

                map.put(id, truckDetail);
            }

            Order order = new Order(
                    rs.getInt("orderId"),
                    rs.getDouble("qty"),
                    rs.getDate("date"),
                    rs.getInt("id"));

            truckDetail.getOrderList().add(order);

        }
        return new ArrayList<TruckDetail>(map.values());
    }
});

这是我试图映射到的

的dto类
public class TruckDetail {

    private Integer truckId;

    private String truckCode;
    private Date purchasedDate;
    private String descriptions;
    private List<Order> orderList;
    private Double avgPerMonth;
    }

我不确定我的resultsetExtractor是否准确,但我也不确定如何继续使用jdbcTemplate或nameParams执行sql命令..这是我所做的但不起作用 我不确定如何继续任何人的帮助,请指出我的错误

public final TruckDetail getFullTruckDetail(){

    MyObjectExtractor myObjectExtractor = new MyObjectExtractor();
   TruckDetail truckDetails =
            namedParameterJdbcTemplate.query(fullTruckDetail,myObjectExtractor.extractData( )) {
            });

   return truckDetails;
}

0 个答案:

没有答案