mybatis3 autoMappingBehavior

转载请注明:

TheViper http://www.cnblogs.com/TheViper

autoMappingBehavior是一个容易被忽略的属性

可以看到,默认是PARTIAL,只会自动映射没有定义嵌套结果集映射的结果集。这句话有点拗口,意思就是映射文件中,对于resultMap标签,如果没有显式定义result标签,mybatis不会帮你把结果映射到model(pojo)上.

具体的,比如feeling(心情)和feeling_comment(评论)是一对多关系。

<resultMap id="FeelingCommentResult" type="Feeling">
	<id property="feeling_id" column="feeling_id" />
	<!-- <result property="content" column="content" /> -->
	<collection property="feelingComments" ofType="FeelingComment">
		<id property="feeling_comment_id" column="feeling_comment_id" />
		<!-- <result property="commentContent" column="commentContent" /> -->
	</collection>
</resultMap>
<select id="selectFeelingComment" parameterType="map" resultMap="FeelingCommentResult">
	select * from feeling left outer join feeling_comment on feeling.feeling_id=feeling_comment.feeling_id where feeling.id =#{id}
</select>

<resultMap>和<collection>中都没有定义<result>,这时

List<Feeling> feeling = (List<Feeling>) sqlSessionTemplate.selectList(
                "user.selectFeelingComment", map);
        System.out.println(feeling.get(0).getContent());
        System.out.println(feeling.get(0).getFeelingComments().get(0).getCommentContent());

取出content和commentcontent都是null,当然,如果只是

System.out.println(feeling);
System.out.println(feeling.get(0).getFeelingComments());

mybatis会初始化model的,因此输出会有结果的,但里面除了id属性有具体值外,其余都没有,因为没有定义<result>。去掉上面的注释,再去取content和commentcontent就会有具体的值了。

如果select语句中有很多需要取的字段,autoMappingBehavior就派上用场了。只需要将其设置为FULL,就不用再写那一大堆<result>了。

<setting name="autoMappingBehavior" value="FULL" />

注意,mybatis的autoMappingBehavior确实很赞,使用很方便,但是前提是开放者需要确保model和数据库字段是一一对应的,还有相应表的字段名不要重复。

比如,feeling的内容是content,feeling_comment的内容是commentContent,这时数据库feeling表保存内容的字段名字也应该是content,feeling_comment同理。

另外这里对feeling_comment表保存内容的字段名就不能简单的设置为content.

至于model那边,可以有名字相同的属性,比如feeling类和feeling_comment类都可以有一个content属性,但必须要用<result>。

<resultMap id="FeelingCommentResult" type="Feeling">
        <id property="feeling_id" column="feeling_id" />
        <collection property="feelingComments" ofType="FeelingComment">
            <id property="feeling_comment_id" column="feeling_comment_id" />
            <result property="content" column="commentContent" />
        </collection>
    </resultMap>

可以看出<result>相当于sql里面的as.如果相应表间有重复的字段名,比如这里保存内容的字段名都是content,就算用<result>

<result property="commentContent" column="content" />

也是无能为力,这相当于select content as commentContent,content as content,....

mybatis不会知道是哪个表的content,美中不足!

当然,如果直接就在<select>里面把sql写好,如select feeling.content,feeling_comment.content as commentContent,。。。上面的内容都可以不看了,没有用到mybatis的autoMappingBehavior。

最后如果用的是懒加载,比如

<resultMap type="Feeling" id="FeelingResult">
	<collection property="feelingComments" select="selectComment"
		column="feeling_id" ofType="FeelingComment"></collection>
</resultMap>
<select id="selectComment" parameterType="int" resultType="FeelingComment">
	select * from feeling_comment where feeling_comment_id = #{id}
</select>
<select id="selectFeeling" parameterType="int" resultMap="FeelingResult">
	select * from feeling where id = #{id}
</select>

虽然有<resultMap>,但由于本质上是多条select语句,所以不用写<result>仍然取得到具体值。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值