JavaEE日常工作经验总结系列(三)-----Mybatis传入多类型参数

Mybatis传入多类型参数

接口定义如下

public List<User> selectUsersByIds(List<Integer> ids, String userName)

Mybatis的xml文件如下

<select id="selectUsersByIds" resultType="User">  
    select * from user where id in   
    <foreach collection="param1" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
    and name = #{param2}  
</select>  

mybatis会自动将多个不同类型的参数改成param1,param2…

当然也可以使用Mybatis注解明确指明传入参数名称

接口定义如下

public List<User> selectUsersByIds(@Param("ids")List<Integer> ids, @Param("user")User user)

Mybatis的xml文件如下

<select id="selectUserInIDs" resultType="User">  
    select * from user   
    <if test="null != ids">  
        where id in   
        <foreach collection="ids" item="item" open="(" separator="," close=")">  
            #{item}  
        </foreach>  
        and name = #{user.name}  
    </if>  
</select>  

猜你喜欢

转载自blog.csdn.net/qq_27922023/article/details/80746594