MyBatis中标签的多种应用场景与用法解析

在MyBatis中,<collection>标签用于处理一对多或多对多的关系映射,即将数据库中的多条记录映射为Java对象中的一个集合属性。<collection>标签可以用于映射集合类型包括ListSetArray等。以下是<collection>标签的几种常见用法:

图片[1]_MyBatis中标签的多种应用场景与用法解析_知途无界

基本用法

  • 通过<collection>标签,指定Java对象的集合属性,以及对应的数据库表和字段。
  • 常用于映射一个对象中包含的多个子对象。

    <resultMap id="personResultMap" type="Person">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <collection property="orders" ofType="Order">
    <id property="id" column="order_id" />
    <result property="amount" column="amount" />
    </collection>
    </resultMap>
    <resultMap id="personResultMap" type="Person">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <collection property="orders" ofType="Order">
            <id property="id" column="order_id" />
            <result property="amount" column="amount" />
        </collection>
    </resultMap>
    <resultMap id="personResultMap" type="Person"> <id property="id" column="id" /> <result property="name" column="name" /> <collection property="orders" ofType="Order"> <id property="id" column="order_id" /> <result property="amount" column="amount" /> </collection> </resultMap>

    上述例子中,一个Person对象包含多个Order对象。

    嵌套查询

    • 使用select属性指定一个子查询来获取集合中的元素。
    • 适用于需要通过其他查询获取关联数据的场景。

      <resultMap id="personResultMap" type="Person">
      <id property="id" column="id" />
      <result property="name" column="name" />
      <collection property="orders" ofType="Order" select="selectOrdersByPersonId" column="id" />
      </resultMap>
      <select id="selectOrdersByPersonId" resultType="Order">
      SELECT id, amount FROM orders WHERE person_id = #{personId}
      </select>
      <resultMap id="personResultMap" type="Person">
          <id property="id" column="id" />
          <result property="name" column="name" />
          <collection property="orders" ofType="Order" select="selectOrdersByPersonId" column="id" />
      </resultMap>
      
      <select id="selectOrdersByPersonId" resultType="Order">
          SELECT id, amount FROM orders WHERE person_id = #{personId}
      </select>
      <resultMap id="personResultMap" type="Person"> <id property="id" column="id" /> <result property="name" column="name" /> <collection property="orders" ofType="Order" select="selectOrdersByPersonId" column="id" /> </resultMap> <select id="selectOrdersByPersonId" resultType="Order"> SELECT id, amount FROM orders WHERE person_id = #{personId} </select>

      在这个例子中,selectOrdersByPersonId是一个子查询,用于根据person_id检索订单。

      嵌套结果

      • 当关联数据可以在同一个查询中获取时,使用嵌套结果来避免额外的查询。
      • 通过<resultMap>中的<collection>和子对象的<resultMap>配置。

        <select id="selectPersonWithOrders" resultMap="personWithOrdersResultMap">
        SELECT p.id AS person_id, p.name AS person_name, o.id AS order_id, o.amount AS order_amount
        FROM persons p
        LEFT JOIN orders o ON p.id = o.person_id
        </select>
        <resultMap id="personWithOrdersResultMap" type="Person">
        <id property="id" column="person_id" />
        <result property="name" column="person_name" />
        <collection property="orders" ofType="Order">
        <id property="id" column="order_id" />
        <result property="amount" column="order_amount" />
        </collection>
        </resultMap>
        <select id="selectPersonWithOrders" resultMap="personWithOrdersResultMap">
            SELECT p.id AS person_id, p.name AS person_name, o.id AS order_id, o.amount AS order_amount
            FROM persons p
            LEFT JOIN orders o ON p.id = o.person_id
        </select>
        
        <resultMap id="personWithOrdersResultMap" type="Person">
            <id property="id" column="person_id" />
            <result property="name" column="person_name" />
            <collection property="orders" ofType="Order">
                <id property="id" column="order_id" />
                <result property="amount" column="order_amount" />
            </collection>
        </resultMap>
        <select id="selectPersonWithOrders" resultMap="personWithOrdersResultMap"> SELECT p.id AS person_id, p.name AS person_name, o.id AS order_id, o.amount AS order_amount FROM persons p LEFT JOIN orders o ON p.id = o.person_id </select> <resultMap id="personWithOrdersResultMap" type="Person"> <id property="id" column="person_id" /> <result property="name" column="person_name" /> <collection property="orders" ofType="Order"> <id property="id" column="order_id" /> <result property="amount" column="order_amount" /> </collection> </resultMap>

        这个例子使用左连接在同一个查询中获取PersonOrder的数据。

        使用鉴别器

        • 可以结合<discriminator>来使用,用于根据某一列的值动态选择子映射。
        • 适用于需要根据特定字段值映射不同类型对象的场景。

          <resultMap id="vehicleResultMap" type="Vehicle">
          <id property="id" column="id" />
          <result property="type" column="type" />
          <discriminator column="type" javaType="String">
          <case value="CAR" resultType="Car" />
          <case value="TRUCK" resultType="Truck" />
          </discriminator>
          <collection property="features" ofType="Feature">
          <id property="id" column="feature_id" />
          <result property="description" column="description" />
          </collection>
          </resultMap>
          <resultMap id="vehicleResultMap" type="Vehicle">
              <id property="id" column="id" />
              <result property="type" column="type" />
              <discriminator column="type" javaType="String">
                  <case value="CAR" resultType="Car" />
                  <case value="TRUCK" resultType="Truck" />
              </discriminator>
              <collection property="features" ofType="Feature">
                  <id property="id" column="feature_id" />
                  <result property="description" column="description" />
              </collection>
          </resultMap>
          <resultMap id="vehicleResultMap" type="Vehicle"> <id property="id" column="id" /> <result property="type" column="type" /> <discriminator column="type" javaType="String"> <case value="CAR" resultType="Car" /> <case value="TRUCK" resultType="Truck" /> </discriminator> <collection property="features" ofType="Feature"> <id property="id" column="feature_id" /> <result property="description" column="description" /> </collection> </resultMap>

          这些是<collection>标签在MyBatis中的一些典型用法,具体使用哪种方式取决于数据表的结构和业务逻辑的需求。通过合理配置<collection>标签,可以高效地完成复杂对象关系的映射。

          © 版权声明
          THE END
          喜欢就点个赞,支持一下吧!
          点赞47 分享
          Every day has not danced, all are life's disappointment.
          每一个不曾起舞的日子,都是对生命的辜负
          评论 抢沙发
          头像
          欢迎您留下评论!
          提交
          头像

          昵称

          取消
          昵称表情代码图片

            暂无评论内容