国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

mybatis3 autoMappingBehavior

2019-11-15 00:14:28
字體:
來源:轉載
供稿:網友
mybatis3 autoMappingBehavior

轉載請注明:TheViperhttp://m.survivalescaperooms.com/TheViper

autoMappingBehavior是一個容易被忽略的屬性

可以看到,默認是PARTIAL,只會自動映射沒有定義嵌套結果集映射的結果集。這句話有點拗口,意思就是映射文件中,對于resultMap標簽,如果沒有顯式定義result標簽,mybatis不會幫你把結果映射到model(pojo)上.

具體的,比如feeling(心情)和feeling_comment(評論)是一對多關系。

    <resultMap id="FeelingCommentResult" type="Feeling">        <id 

<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>仍然取得到具體值。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪泽县| 惠州市| 西平县| 奇台县| 西青区| 大石桥市| 南宫市| 大方县| 赤城县| 会同县| 麻阳| 修武县| 名山县| 龙口市| 阳城县| 城口县| 龙口市| 奎屯市| 温州市| 垣曲县| 陆良县| 文水县| 舞钢市| 富锦市| 商丘市| 沙田区| 扎赉特旗| 定安县| 普格县| 青河县| 乌鲁木齐县| 丰台区| 昌都县| 基隆市| 榆中县| 团风县| 托里县| 永靖县| 观塘区| 云阳县| 缙云县|