在MyBatis中,<collection>
标签用于处理一对多或多对多的关系映射,即将数据库中的多条记录映射为Java对象中的一个集合属性。<collection>
标签可以用于映射集合类型包括List
、Set
、Array
等。以下是<collection>
标签的几种常见用法:
![图片[1]_MyBatis中标签的多种应用场景与用法解析_知途无界](https://zhituwujie.com/wp-content/uploads/2025/04/d2b5ca33bd20250402100235.png)
基本用法:
- 通过
<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_amountFROM persons pLEFT 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>
这个例子使用左连接在同一个查询中获取Person
和Order
的数据。
使用鉴别器:
- 可以结合
<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
暂无评论内容