parameterType傳入參數類型


Mybatis的Mapper文件中的select、insert、update、delete元素中都有一個parameterType屬性,用於對應的mapper接口方法接受的參數類型。

parameterType (不是必須)如果不指定,自動識別,指的就是傳入的參數

MyBatis的傳入參數parameterType類型分兩種:

  • 簡單數據類型:int、string、long、Date;
  • 復雜數據類型:類(JavaBean、Integer等)和Map

如何獲取參數中的值:

  • 簡單數據類型:#{參數} 獲取參數中的值
  • 復雜數據類型:#{屬性名}  ,map中則是#{key}

傳入Long型

mapper接口代碼:

public User findUserById(Long id);  

xml代碼:

<select id="findUserById" parameterType="java.lang.Long" resultType="User">    
        select * from user where  id = #{id};    
</select>  

傳入List

mapper接口代碼:

public List<User> findUserListByIdList(List<Long> idList);    

xml代碼:

<select id="findUserListByIdList" parameterType="java.util.List" resultType="User">    
    select * from user user    
    <where>    
        user.id in (    
          <foreach collection="list"  item="id" index="index" separator=",">   
             #{id}   
          </foreach>    
        )    
    </where>    
</select>

在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性。

該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:

  1. 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list
  2. 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array
  3. 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可

傳入數組

mapper接口代碼:

public List<User> findUserListByIdList(int[] ids);

xml代碼: 

<select id="findUserListByIdList" parameterType="java.util.List" resultType="User">    
    select * from user user    
    <where>    
        user.id in (    
           <foreach collection="array"  item="id" index="index"  separator=",">   
                #{id}   
           </foreach>    
        )    
    </where>    
</select>

傳入map 

mapper接口代碼:

public boolean exists(Map<String, Object> map);

xml代碼: 

<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">    
        SELECT COUNT(*) FROM USER user    
        <where>    
            <if test="code != null">     
                and user.code = #{code}     
            </if>    
            <if test="id != null">     
                and user.id = #{id}     
            </if>    
            <if test="idList !=null ">    
                and user.id in (    
                   <foreach collection="idList" item="id" index="index" separator=",">   
                        #{id}   
                   </foreach>    
                )    
            </if>    
        </where>    
</select>

map中有list或array時,foreach中的collection必須是具體list或array的變量名。

比如這里map含有一個名為idList的list,所以map中用idList取值,這點和單獨傳list或array時不太一樣。

傳入JAVA對象

mapper接口代碼:

public int findUserList(User user);   

xml代碼:

<select id="findUserList" parameterType="User" resultType="java.lang.Integer">    
        SELECT COUNT(*) FROM USER user    
        <where>    
            <if test="code != null">     
                and user.code = #{code}     
            </if>    
            <if test="id != null">     
                and user.id = #{id}     
            </if>    
            <if test="idList !=null ">    
                and user.id in (    
                    <foreach  collection="idList" item="id" index="index" separator=",">   
                         #{id}   
                    </foreach>    
                )    
            </if>    
        </where>    
</select>

JAVA對象中有list或array時,foreach中的collection必須是具體list或array的變量名。 

比如這里User含有一個名為idList的list,所以User中用idList取值,這點和單獨傳list或array時不太一樣。

注解@Param

 ① 注解單一屬性;這個類似於將參數重命名了一次

mapper接口代碼:

List<User> selectUserByTime(@Param(value="startdate")String startDate);  

xml代碼:

<select id="selectUserByTime" resultType="User" parameterType="java.lang.String">    
    select * from t_user   
    where  create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')  
</select>  

② 注解javaBean

mapper接口代碼:

List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);  

xml代碼: 

<select id="selectUserByTime" resultType="User" parameterType="DateVO">    
    select *  
    from t_user  
    where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')   
          and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')   
 </select>   

 


免責聲明!

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



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