为什么封装成VO和BO、mybatis的resultMap的代码书写

本文介绍了VO(View Object)和BO(Business Object)在前后端交互中的作用,并通过MyBatis的ResultMap配置展示了如何将数据库查询结果映射到自定义的对象。VO主要用于方便前端数据展示,BO则用于封装业务逻辑数据。示例中详细展示了CategoryVO与SubCategoryVO的ResultMap配置,以及查询子分类列表和搜索商品的SQL语句。
摘要由CSDN通过智能技术生成

VO层:V指的是View,这不是指的前端的页面意思,而是指的将后台的数据(可以简单理解成SQL语句查询的来的数据),直接查询出的数据直接传给前端,前端需要对数据进行筛选处理才会展示到页面,而VO对象,可以理解成后台重新将查询出的数据进行一个简单的封装,目的就是方便提供给前端调用,所以取名叫做VO。

BO层是business Object 业务对象

我自己简单的理解就是前台传递给后台的数据,数据在后台的数据库表中不是一个个字段相对应。所以需要重新封装一个对象接受前台传递过来的数据。
 

mybatis的resultMap的代码书写

categoryVO中包含了subCategoryList的列表
<resultMap id="categoryVO" type="com.imooc.pojo.vo.CategoryVO">
    <id column="id" property="id"  />
    <result column="name" property="name"  />
    <result column="type" property="type"  />
    <result column="fatherId" property="fatherId" />

    <collection property="subCategoryList" ofType="com.imooc.pojo.vo.SubCategoryVO">
      <id column="subId" property="subId"  />
      <result column="subName" property="subName"  />
      <result column="subType" property="subType"  />
      <result column="subFatherId" property="subFatherId" />
    </collection>
  </resultMap>
  
    <select id="getSubCatList" resultMap="categoryVO" parameterType="integer">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.imooc.mapper.ItemsCustomMapper" >
  <select id="queryItemComments" parameterType="Map" resultType="com.imooc.pojo.vo.ItemCommentVO">

    select
        ic.comment_level commentLevel,
        ic.content content,
        ic.sepc_name sepcName,
        ic.created_time createdTime,
        u.face userFace,
        u.nickname nickname
        from items_comments ic
    LEFT JOIN users u
          ON ic.user_id = u.id
    where ic.item_id = #{paramsMap.itemId}
        <if test="paramsMap.level!=null and paramsMap.level!=''">
          AND ic.comment_level = #{paramsMap.level}
        </if>
  </select>
    
    <select id="searchItems" parameterType="map" resultType="com.imooc.pojo.vo.SearchItemsVO">

        SELECT
            i.id AS itemId,
            i.item_name AS itemName,
            i.sell_counts AS sellCounts,
            ii.url AS imgUrl,
            tempSpec.price_discount as price
        FROM
            items i
                LEFT JOIN items_img ii ON i.id = ii.item_id
                LEFT JOIN ( SELECT item_id, MIN( price_discount ) price_discount FROM items_spec GROUP BY item_id ) AS tempSpec ON i.id = tempSpec.item_id
        WHERE
            ii.is_main = 1
            <if test="paramsMap.keywords!=null and paramsMap.keywords!=''">
                AND i.item_name like '%${paramsMap.keywords}%'
            </if>
        order by
        <choose>
            <when test="paramsMap.sort == &quot;c&quot;">
                sellCounts desc
            </when>

            <when test="paramsMap.sort == &quot;p&quot;">
                price asc
            </when>
            <otherwise>
                itemName asc
            </otherwise>
        </choose>
    </select>
    

</mapper>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值