在MyBatis中,可以使用嵌套查询(nested queries)或者联合查询(join queries)来返回多张表的字段。
例如:
<select id="getUserWithOrders" resultMap="UserWithOrdersResultMap">
select * from users
where user_id = #{userId}
</select>
<resultMap id="UserWithOrdersResultMap" type="User">
<result property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
<collection property="orders" ofType="Order" resultMap="OrderResultMap"/>
</resultMap>
<resultMap id="OrderResultMap" type="Order">
<result property="orderId" column="order_id"/>
<result property="orderDate" column="order_date"/>
<result property="totalAmount" column="total_amount"/>
</resultMap>
例如:
<select id="getUserWithOrders" resultMap="UserWithOrdersResultMap">
select u.*, o.*
from users u
join orders o on u.user_id = o.user_id
where u.user_id = #{userId}
</select>
<resultMap id="UserWithOrdersResultMap" type="User">
<result property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
<result property="orderId" column="order_id"/>
<result property="orderDate" column="order_date"/>
<result property="totalAmount" column="total_amount"/>
</resultMap>
通过以上方式,可以实现在MyBatis中返回多张表的字段。需要根据具体业务需求选择合适的方法来查询关联的表数据。