Mybatis——动态sql之ifwhereforeach

Posted 红颜莫知己

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis——动态sql之ifwhereforeach相关的知识,希望对你有一定的参考价值。

<!-- 配置查询所有操作 -->
    <select id="findArticleByTitleOrAuthorOrSource" resultType="article">
        select * from t_article
        <where>
            <if test="source != null">
              and source = #source
            </if>
            <if test="author != null">
                and author = #author
            </if>
            <if test="title != null">
                and title = #title
            </if>
        </where>
    </select>
<select id="findById" parameterType="java.util.List" resultType="channel">
--         select * from where cid in (1,2,3,4)
        select * from t_channel
        <where>
            <if test="isd != null and ids.size() > 0 "/>
              <foreach collection="ids" open="cid in (" close=")" item="cid" separator=",">
                #cid
              </foreach>
        </where>
    </select>

测试:

mapper = sqlSession.getMapper(ChannelDao.class);
        List<Integer> ids = Arrays.asList(1,2,3,4);
        List<Channel> list = mapper.findChannelById(ids)

以上是关于Mybatis——动态sql之ifwhereforeach的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis之动态 SQL

mybatis 之动态sql 增删改查

mybatis 之动态 SQL

MyBatis注解之动态SQL

19.Mybatis之动态SQL

MyBatis之动态sql