Mybatis resultmap resultType初識 二 :xml 文件的使用 多參數 單參數傳值

先了解下查詢 如下sql 語句

說明 sql 的ID 要唯一!!!


<resultMap id="BaseResultMap" type="map" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="passWord" property="password" jdbcType="VARCHAR" />
        <result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>

    </resultMap>


//resultMap 將返回值映射到自定義到的map中
 <select id="findAll" resultMap="BaseResultMap">
        select
         id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
   </select>

//也可以不寫resultMap 直接用resultType

 <select id="findAll"resultType="map">
        select
         id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
   </select>

也有人會把resultMap 寫成如下的方式

//type 就是將返回值映射成一個pojo實體
<resultMap id="GuserResultMap" type="com.chris.apecircle.domain.Guser.Guser" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="passWord" property="password" jdbcType="VARCHAR" />
        <result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>

    </resultMap>

   <select id="getAll" resultMap="GuserResultMap">
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
    </select>

//這裏我反而覺得有點雞肋,原因如下
//1.這裏我如果只需上述的4 個參數,這樣寫會將實體中沒有映射的屬性也返回,返回值臃腫
//2.如果一定要返回的話 我們可以採用 resultType 的方式,少寫了一個resultMap 代碼也整潔了
//3.這種情況在有別名的狀態下才能體現價值所在

resultType方式
 <select id="getAll" resultType="com.chris.apecircle.domain.Guser.Guser">
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
 </select>
//MARK: resultType 和 resultMap 不能同時使用

傳參數情況下

 List<Guser>  getGuserbyUserName(String userName);


//parameterType 輸入的參數類型
 <select id="getGuserbyUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName}
    </select>

當有兩個參數的時候 就不能用parameterType 了這時用幾種方法來處理,這裏我只展示一種常用的方法

一:採用下標的方式
 Object getGuser(String userName,String pwd);

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{arg0} and passWord = #{arg1}
    </select>

//這種對於mapper 修改的較少但是需要和參數一一對應,對於參數多時不友好

二:採用註解的方式
 Object getGuser(@Param("userName") String userName,@Param("pwd") String pwd );

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName} and passWord = #{pwd}
    </select>
//這種方式 相較於前者 明確傳值名稱,順序不需要一一對應, 缺點 對於參數多時依然不友好

三:map 封裝
Object getGuser(HashMap map);

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName} and passWord = #{pwd}
    </select>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章