MyBatis通過注解實現映射中的嵌套語句和嵌套結果


案例描述:查看訂單或購物車訂單信息的同時查詢出該訂單中所有書籍的信息。

一、嵌套語句

@Select("select* from shopcart where shopcartid = #{id}")
@Results({ @Result(id = true, column = "shopcartid", property = "shopCartId"),
        @Result(column = "bookid", property = "bookId"), @Result(column = "amount", property = "amount"),
        @Result(column = "price", property = "price"), @Result(column = "status", property = "status"),
        @Result(column = "bookid", property = "book", one = @One(select = "com.yh.mybatis.mapper.BookMapper.findBookInfoById")) })
ShopCart findShopCartInfoById(int id);

 

 

二、嵌套結果

// 查找剛剛添加的訂單信息
@Select("select orderinfo.*,book.bookname,book.imgurl,book.introduction,book.packstyle,book.sellerid,book.price bookprice from orderinfo,book where orderinfo.bookid = book.bookid and orderid like '${orderId}%'")
@Results({ @Result(id = true, column = "identityid", property = "identityId"),
        @Result(column = "orderid", property = "orderId"), @Result(column = "buyerid", property = "buyerId"),
        @Result(column = "bookid", property = "bookId"), @Result(column = "amount", property = "amount"),
        @Result(column = "orderdate", property = "orderDate"), @Result(column = "express", property = "express"),
        @Result(column = "payment", property = "payment"), @Result(column = "addressid", property = "addressId"),
        @Result(column = "price", property = "price"), @Result(column = "message", property = "message"),
        @Result(column = "status", property = "status"),
        // 自定義類成員變量的屬性
        @Result(column = "bookname", property = "book.bookName"),
        @Result(column = "sellerid", property = "book.sellerId"),
        @Result(column = "bookprice", property = "book.price"),
        @Result(column = "imgurl", property = "book.imgUrl"),
        @Result(column = "introduction", property = "book.introduction"),
        @Result(column = "packstyle", property = "book.packStyle") })
List<Order> findLastOrder(@Param("orderId") String orderId);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM