MyBatis 中可以通過以下方式操作集合:
<resultMap id="userMap" type="User">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<select id="selectUsers" resultMap="userMap">
select id, name from user
</select>
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="orders" ofType="Order">
<id property="orderId" column="order_id"/>
<result property="orderName" column="order_name"/>
</collection>
</resultMap>
<select id="selectUsers" resultMap="userMap">
select u.id, u.name, o.order_id, o.order_name
from user u
left join order o on u.id = o.user_id
</select>
<update id="insertUsers" parameterType="java.util.List">
insert into user (id, name) values
<foreach collection="list" item="user" separator=",">
(#{user.id}, #{user.name})
</foreach>
</update>
通過以上方式,可以方便地操作集合對象,并將查詢結果映射到集合對象中,或者將集合對象傳遞到 SQL 語句中進行操作。