MyBatis的parameterType传入参数类型

   在 mybatis 映射接口的配置中, 有 select,insert,update,delete 等元素都提到了 parameterType 的用法,parameterType 为输入参数,在配置的时候,配置相应的输入参数类型即可。parameterType 有基本数据类型和复杂的数据类型配置。

1. MyBatis 的传入参数 parameterType 类型分两种
   1. 1. 基本数据类型:int、string、long、Date;
   1. 2. 复杂数据类型:类(JavaBean、Integer 等)和 Map

 

2. 如何获取参数中的值:
   2.1  基本数据类型:#{参数} 获取参数中的值
   2.2  复杂数据类型:#{属性名}  ,map 中则是 #{key}

3. 案例:

 3.1 传入 Long 型

mapper 接口代码:

public User findUserById(Long id);  

xml 代码:

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

3.2  传入 List

mapper 接口代码:

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

xml 代码:

<select id="findUserListByIdList" parameterType="java.util.ArrayList" 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 了,当然单参数也可

 

 

3.3 传入数组:

mapper 接口代码:

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

xml 代码:

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

3.4 传入 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 时不太一样。

 

3.5 传入 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 时不太一样。

 

3.6 注解 @Param

 

例子 1:

注解单一属性;这个类似于将参数重命名了一次

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>  

例子 2:

注解 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>   

 
---------------------

 作者:Remember_Ray 来源:

CSDN 原文:https://blog.csdn.net/q343509740/article/details/80578832