mybatis最重要的mapper文件書寫


1.MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap。

也只有在mapper的select標簽中,才會指定resultMap屬性的值,其他insert/update/delete是不需要指定resultsMap屬性的。

mapper文件里還有一個重要屬性,parameterType。指定參數的類型

<select id="selectBlog" parameterType="int" resultType="Blog">
  		select * from t_blog where id = #{id}
</select><!--來自SQL映射文件BlogMapper.xml-->

resultMap就是進行object與table記錄的轉換的啊。

 

3. MyBatis是基於“數據庫結構不可控”的思想建立的,也就是我們希望數據庫遵循第三范式或BCNF,但實際事與願違,那么結果集映射就是MyBatis為我們提供這種理想與現實間轉換的手段了,而resultMap就是結果集映射的配置標簽了。

resultMap就是結果映射標簽

resultMap就是結果映射標簽!

resultMap就是結果映射標簽!!

所以resultMap要指定兩邊的具體值啊。即:指定

<select id="selectOne" resultMap="BaseResultMap">
        select * from tbl_user
        <where>
        <trim prefixOverrides="and">
            <if test="openid != null">
                and openid = #{openid}                
            </if>
            <if test="uid != null">
                and uid = #{uid}                
            </if>
            <if test="phone != null">
                and phone = #{phone}                
            </if>
        </trim>
        </where> 
        limit 1
    </select>

 

即只有查詢時候才會得到數據,才會用到類型匹配轉換,insert/update/delete等這種寫操作是不會用到resultmap/resulttype類型轉換的。

2.resultmap也可以進行繼承的,從其他resultmap里繼承過來匹配的關系

 

<resultMap id="BaseResultMap" type="User" extends="SimpleResultMap">
        <id property="uid" column="uid" />

        <result property="unionid" column="unionid"/>
        <result property="openid" column="openid"/>
        <result property="age" column="age"/>
        <result property="birthday" column="birthday"/>
        <result property="sex" column="sex"/>
        <result property="phone" column="phone"/>
        <result property="email" column="email"/>
        <result property="qq" column="qq"/>
        <result property="wechat" column="wechat"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="country" column="country"/>
        <result property="channel" column="channel"/>
        <result property="password" column="password"/>
        
        <!-- SimpleResultMap 中已經有
        <result property="nickname" column="nickname"/>
        <result property="headimgurl" column="headimgurl"/>
        <result property="appid" column="appid"/>
        <result property="password" column="password"/>
         -->
        <result property="backgroundimg" column="backgroundimg"/>
        <result property="description" column="description"/>
        <result property="createTime" column="create_time"/>
        
    </resultMap>
    
    <resultMap  id="SimpleResultMap" type="User">
        <id property="uid" column="uid" />
        <result property="nickname" column="nickname"/>
        <result property="headimgurl" column="headimgurl"/>
    </resultMap>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM