温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis的parameterType传入参数类型有哪些

发布时间:2021-09-29 10:49:56 来源:亿速云 阅读:669 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关MyBatis的parameterType传入参数类型有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

    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>

    mybatis 之parameterType="Long"

     <select id="selectByPrimaryKeyByArrayMemberId"  resultType="memberModel" parameterType="Long">
               select 
               <include refid="Base_Column_List"/>
                  from member m
               where
               m.IS_DELETE = 'N'
               and m.member_id IN
               <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
                 #{item,jdbcType=DECIMAL}  
            </foreach>
        </select>
    public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(
                List<Long> memberIds)
        {
            try
            {
                if (memberIds == null || memberIds.size()==0){
                    return super.returnParamsError("参数为空!");
                }
                List<Member> list = memberMapper
                .selectByPrimaryKeyByArrayMemberId(memberIds);
                return super.returnCorrectResult(list);
            }
            catch (Throwable e)
            {
                return super.returnException(e);
            }
        }
    
        public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
        List<Member> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
        @Test
        public void testSelectByPrimaryKeyByArrayMemberId()
        {
            InternalMemberService internalMemberService = J1SOAHessianHelper.getService(url,InternalMemberService.class);
            List<Long> memberIds = new ArrayList<Long>();
            memberIds.add(1l);
            memberIds.add(2l);
            memberIds.add(1855l);
            ServiceMessage<List<Member>> sm = internalMemberService.selectByPrimaryKeyByArrayMemberId(memberIds);
            System.out.println(sm.getResult());
        }

    感谢各位的阅读!关于“MyBatis的parameterType传入参数类型有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI