parameterType of MyBatis passed in parameter type

In the configuration of mybatis mapping interface, the use of parameterType is mentioned in the elements such as select, insert, update and delete. parameterType is an input parameter. When configuring, configure the corresponding input parameter type. parameterType has basic data types and complex data type configurations.

1. There are two types of parameterType of the incoming parameter of mybatis
   1. 1. Basic data type: int, string, long, Date;
   1. 2. Complex data types: classes (JavaBean, Integer, etc.) and maps

 

2. How to get the value in the parameter:
2.1 basic data type: #{parameter} get the value in the parameter
2.2 #{complex data type: #{attribute name}, and #{key} in map

3. Case:

3.1 incoming Long type

mapper interface code:

public User findUserById(Long id);  

xml code:

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

3.2 # incoming List

mapper interface code:

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

xml code:

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

When using foreach, the most critical and error prone attribute is the collection attribute.

This attribute must be specified, but its value is different in different cases. There are three main cases:

    1. If a single parameter is passed in and the parameter type is a list, the value of the collection property is list

    2. If a single parameter is passed in and the parameter type is an array, the attribute value of collection is array

    3. If there are multiple parameters passed in, we need to encapsulate them into a Map. Of course, a single parameter can also be used

 

 

3.3 incoming array:

mapper interface code:

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

xml code:

<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 incoming map

mapper interface code:

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

 

xml code:

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

When there is a list or array in the MAP, the collection in the foreach must be the variable name of the specific list or array.

For example, the MAP contains a list named idList, so idList is used in the MAP, which is different from that when sending list or array alone.

 

3.5 incoming JAVA object

mapper interface code:

public int findUserList(User user);   

xml code:

 

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

 

 

 

When there is a list or array in a JAVA object, the collection in foreach must be the variable name of a specific list or array.

For example, the User here contains a list named idList, so the User uses idList to get the value, which is different from that when sending list or array alone.

 

3.6 annotation @ Param

 

Example 1:

Annotation single attribute; This is similar to renaming the parameter once

mapper interface code:

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

xml code:

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

Example 2:

Annotation javaBean

mapper interface code:

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

xml code:

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

 
Reprint: https://www.cnblogs.com/leeego-123/p/10725210.html

Tags: Java

Posted by Monkee Of Evil on Sun, 22 May 2022 13:27:16 +0300