mybatis一对多关系映射,一对一映射

第一种```

<resultMap type="UserInfo" id="userRM">
	<!-- column:表里的字段  property:实体的属性名 -->
	<id column="id" property="id"/>
	<result column="user_name" property="userName"/>
	<result column="user_addr" property="userAddr"/>
	<result column="user_age" property="userAge"/>
</resultMap>
<!-- 绑定一对一的userextra的关系 -->
<resultMap type="UserInfo" id="userExtraRM" extends="userRM">
	<!--association:一对一关系描述   property:关联关系中对象的属性名 -->
	<association property="userExtra" javaType="cn.tedu.pojo.UserExtra">
		<id column="Id" property="id"/>
		<result column="work" property="work"/>
		<result column="Salary" property="salary"/>
		<result column="user_id" property="userId"/>
	</association>
</resultMap>

<select id="findExtraByUser" resultMap="userExtraRM">
	select * from user_info t1,user_extra t2 where t1.id=t2.user_id and t1.id=#{id}
</select>

<!-- 绑定一对多 -->
<resultMap type="UserInfo" id="orderRM" extends="userRM">
	<collection property="orders" ofType="cn.tedu.pojo.Orders">
	<id column="oId" property="id"/>
	<result column="User_id" property="userId"/>
	<result column="Order_no" property="orderNo"/>
	<result column="Order_desc" property="orderDesc"/>
	<result column="Price" property="price"/>
	</collection>
</resultMap>
<select id="findOrdersByUser" resultMap="orderRM">
	select t1.id,t1.user_name,t1.user_addr,t1.user_age,
	t2.Id oId,t2.User_id,t2.Order_no,t2.Order_desc,t2.price from user_info t1,orders t2 where t1.id=t2.User_id and t1.id=#{id}
</select>
第二种

```


select
label_record.name,label_record.id
from novel_record left join novel_label_record ON novel_record.id = novel_label_record.novel_id
LEFT JOIN label_record ON novel_label_record.label_id = label_record.id
where novel_record.id = #{id,jdbcType=BIGINT} and label_record.status=0 and label_record.deleted=0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章