MyBatis中的一对多关系可以使用<collection>标签来配置。
以下是一对多关系配置的基本步骤:
<select>标签来完成。例如:<select id="getParentObject" parameterType="int" resultMap="parentObjectResult"> SELECT * FROM parent_table WHERE id = #{id} </select> <select>标签来完成。例如:<select id="getChildObjects" parameterType="int" resultMap="childObjectResult"> SELECT * FROM child_table WHERE parent_id = #{parentId} </select> <resultMap>标签来完成。在<resultMap>标签中,使用<collection>标签来映射一对多关系。例如:<resultMap id="parentObjectResult" type="ParentObject"> <id column="id" property="id"/> <result column="name" property="name"/> <!-- 定义子对象的映射 --> <collection property="childObjects" ofType="ChildObject" resultMap="childObjectResult"/> </resultMap> <resultMap>标签来完成。例如:<resultMap id="childObjectResult" type="ChildObject"> <id column="id" property="id"/> <result column="name" property="name"/> </resultMap> <collection>标签来关联子对象的查询语句。例如:<select id="getParentObject" parameterType="int" resultMap="parentObjectResult"> SELECT * FROM parent_table WHERE id = #{id} <!-- 关联子对象的查询语句 --> <collection property="childObjects" select="getChildObjects" column="id" javaType="java.lang.Integer" ofType="java.lang.Integer"/> </select> 以上就是使用MyBatis配置一对多关系的基本步骤。注意,这里的示例中使用了两个表parent_table和child_table,你需要根据自己的实际情况进行调整和修改。