码迷,mamicode.com
首页 > 移动开发 > 详细

mybatis3 autoMappingBehavior

时间:2015-05-20 20:08:33      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

转载请注明: 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>仍然取得到具体值。

如果您觉得本文的内容对您有所帮助,您可以打赏我:

技术分享

mybatis3 autoMappingBehavior

标签:

原文地址:http://www.cnblogs.com/TheViper/p/4480765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!