MyBatis对一(collection)和对多(association)

trim

trim标记是一个格式化的标记,可以完成set或者是where标记的功能

代码名称作用
prefix=”“代替功能
prefixOverrides=”“去除前方或后方多出
prefix=”“末尾添加
@使用范例
<trim prefix="where" prefixOverrides="and | or">
<if test="userName != null and userName != ''">
and userName like CONCAT ('%',#{userName},'%') 
</if>
<if test="userRole != null">
and userRole = #{userRole}
</if>

<!-- 填充 set  末尾拼接where id = #{id} 末尾省略逗号 -->
<trim prefix="set" suffix="where id = #{id}" suffixOverrides=",">
<if test="userName != null">userName=#{userName},</if>
</trim>

一对多关联

<resultMap type="本类别名" id="父类方法名">

中间是本类基本属性

<id property="id" column="r.id"></id>
<result property="userCode" column="userCode"/>
<result property="userNmae" column="userNmae"/>

</resultMap>
<resultMap type="本类别名" id="子类方法名" extends="父类resultMap的id名">

本类里的声明的属性名books本类里声明的属性类型BOOK对方命名空间.resultMap的id名

<collection property="addresses" ofType="Address" resultMap="com.bdqn.dao.getAddress"></collection>
</resultMap>

查的时候就用子类id名

一对一关联

<resultMap type="本类别名" id="方法名">

中间是本类属性

<id property="id" column="r.id"></id>
<result property="userCode" column="userCode"/>
<result property="userNmae" column="userNmae"/>
<result property="userRole" column="userRole"/>
<!-- 一对一 关联 User要多一个属性-->

本类里声明的属性名books 本类里声明的属性类型BOOK

<association property="role" javaType="role">

对方属性名

<id property="id" column="rl_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association>
</resultMap>

多对多

多对多的使用与一对多的区别在于数据库中,存在着关联两表的第三表,在sql语句中表现,其余地方与上文无异

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值