MyBatis(十一):Mybatis 参数传递用法

之前文章中对in的用法做过讲解:《MyBatis(四):mybatis中使用in查询时的注意事项

实际上对于多个参数的用法也是这是注意的:

用法1:多参&普通判空&List集合判空&in用法

@Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
    @Select(value = {
            "<script>",
            " SELECT  `id`,`title` ",
            " FROM `tb_article` ",
            " WHERE `category_id`=#{article.categoryId} ",
            "   <if test='article.status!=null'>",
            "   AND `status` = #{article.status} ",
            "   </if>",
            "   <if test='typeList!=null and !typeList.isEmpty()'>",
            "       and `article_type` in",
            "        <foreach collection=\"typeList\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">",
            "           #{item} ",
            "        </foreach>",
            "   </if>",
            "</script>"
            })
    @ResultMap(value = {"articleResultMap"})
    List<AreaPo> queryByCondition(final @Param("article") ArticleModel article, final @Param("typeList") List<Integer> typeList);

1)上边主要对普通参数判断空用法:<if test='article.status!=null'>

2)集合判空的用法:<if test='typeList!=null and !typeList.isEmpty()'>

3)in的用法:<foreach collection=\"typeList\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">";

4)多参数用法,实际上多个参数如果使用@SqlProvider方式是,在ArticleSqlProvider的类中方法中接收的参数对象为Map<String,Object>,该map集合中包含两个对象:key:article的ArticleModel对象;key:typeList的List<Integer>对象。获取方式:ArticleModel aritlce=(ArticleModel)map.get("aritcle");List<Integer> typeList=(List<Integer>)map.get("typeList");。

猜你喜欢

转载自www.cnblogs.com/yy3b2007com/p/12026407.html