创建VO类来封装多表查询的结果

VO:Value Object,值对象。当进行SELECT查询时,查询的结果数据包含多张表的内容,此时查询的结果集不能使用现有的POJO实体类来接收。

解决方法:重新去构建一个新对象,这个对象用于存储所查询出来的结果集对应的映射,这一类的对象就称作值对象。

举个例子:以下代码查询的结果包含了两张表的字段

复制代码
 1 <select id="selectVOByUid" resultType="com.cy.store.vo.CartVO">
 2         SELECT
 3             cid,
 4             uid,
 5             pid,
 6             tc.price,
 7             tc.num,
 8             tp.title,
 9             tp.image,
10             tp.price AS realPrice
11         FROM
12             store.t_cart tc LEFT JOIN store.t_product tp ON tc.pid=tp.id
13         WHERE
14             uid=#{uid}
15         ORDER BY
16             tc.created_time DESC
17 </select>
复制代码

以下是两张表对应的POJO类,以及构建的用来接收查询结果的VO类

复制代码
public class Cart extends BaseEntity {
    Integer cid;
    Integer uid;
    Integer pid;
    Integer price;
    Integer num;
}

public class Product extends BaseEntity implements Serializable {
    Integer id;
    Integer categoryId;
    String itemType;
    String title;
    String sellPoint;
    Integer price;
    Integer num;
    String image;
    Integer status;
    Integer priority;
}

/** 表示购物车数据的VO类 */
public class CartVO implements Serializable {
    private Integer cid;
    private Integer uid;
    private Integer pid;
    private Integer price;
    private Integer num;
    private String title;
    private Integer realPrice;
    private String image;
}
复制代码

这样就能够使用所编写的VO类来接受查询的结果集

/**
* 通过用户id查询购物车信息*
* @param uid 用户id
* @return 购物车信息列表
*/
List<CartVO> selectVOByUid(Integer uid);     

 下面贴上信息获取的来源,一个很不错的spring boot实战教学视频:

https://www.bilibili.com/video/BV1bf4y1V7Bx/?p=33&spm_id_from=333.880.my_history.page.click

posted @   逆涯  阅读(539)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示