dbUtil 級聯屬性結果集封裝問題
阿新 • • 發佈:2018-12-23
現有兩個表Book (圖書),Category(分類)
public Book findByBid(String bid) { try { String sql="select * from book where bid=?"; //return qr.query(sql, new BeanHandler<Book>(Book.class),bid); Map<String,Object> map=qr.query(sql, new MapHandler(),bid); Category category=CommonUtils.toBean(map, Category.class); Book book=CommonUtils.toBean(map, Book.class); book.setCategory(category); return book; } catch (SQLException e) { throw new RuntimeException(e); } }
普通查詢封裝的結果集用 BeanHandler,但是book 裡面並沒有category的資訊,所以用MapHandler(),把Category儲存到Book物件中
問題在於Book裡面含有Category所有沒有的屬性值(比如bookName,price等),同時,Book的cid,和Category 的cid存在關係
Category category=CommonUtils.toBean(map, Category.class);
這是把查詢出來的Book,封裝到Category中;
為什麼Book裡面含有Category所沒有的屬性值,但是依然可以封裝到Category裡?
我認為是封裝的時候,只封裝主鍵外來鍵存在關聯的屬性,比如Book裡的cid–>Category裡的cid
但是其他屬性在原始碼裡是怎麼"跳過"封裝的?我也不知道,沒看到實際有用的原始碼