美文网首页
mybatis使用xml配置mapper

mybatis使用xml配置mapper

作者: 风一样的存在 | 来源:发表于2020-04-17 14:27 被阅读0次

版本:mybatis-3.5.4.jar
当使用foreach标签时出现:


出现的问题

编写的xml配置如下:

<select id="selectPeriodByCompanyNames" resultMap="result">
        select id,
        company_name,
        `type`,
        publish_time,
        create_time,
        title,
        web_site,
        source from company_news_sum
        <where>
            publish_time <![CDATA[>=]]> #{startTime}
            and company_name in
            <foreach collection="list" item="company_name" index="index" open="(" close=")" separator=",">
                #{company_name}
            </foreach>
        </where>
</select>
  • 在只有一个list参数或者array参数的时候,这样配置是好的,但是如果多个参数再这样配置就出错了

解决办法:collection配置修改为参数名称

<select id="selectPeriodByCompanyNames" resultMap="result">
        select id,
        company_name,
        `type`,
        publish_time,
        create_time,
        title,
        web_site,
        source from company_news_sum
        <where>
            publish_time <![CDATA[>=]]> #{startTime}
            and company_name in
            <foreach collection="companyNames" item="company_name" index="index" open="(" close=")" separator=",">
                #{company_name}
            </foreach>
        </where>
</select>

相关文章

网友评论

      本文标题:mybatis使用xml配置mapper

      本文链接:https://www.haomeiwen.com/subject/bkoevhtx.html