您的位置:首页 > 其它

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

2018-01-16 13:18 302 查看
trim

一对多关联

一对一关联

多对多

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语句中表现,其余地方与上文无异
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis