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 语句中进行操作。