您的位置:首页 > 编程语言 > Java开发

mybatis传入混合参数(多个不同类型的参数)

2018-01-02 15:57 1161 查看
当调用接口:[java] view plain copypublic List<User> selectUserInIDs(List<Integer> ids,String name);  

userMapper.xml的书写应该为:[html] view plain copy<select id="selectUserInIDs" 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的Annotation(@Param):
[java] view plain copypublic List<User> selectUserInIDs(@Param("ids")List<Integer> ids,@Param("user")User user);  

[html] view plain copy<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>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java