动态mybatis xml查询

时间:2018-08-16 02:10:03

标签: java mybatis spring-mybatis

需要以下动态查询的帮助:

if country==null OR country != (spain || Peru)
select * from emp where country NOT IN (spain,Peru)
else
select * from emp where country=#{country}

我尝试了类似的方法,但没有帮助:

<when test="country ==null" or country !='spain' or country != 'Peru'">
            AND country NOT IN ('spain','peru')
            </when>
            <otherwise>
            AND country=#{country}
 </otherwise>

即使country = us,也总是生成NOT IN

3 个答案:

答案 0 :(得分:1)

尝试这个test="country == null or country != 'spain' or country != 'UK' "

答案 1 :(得分:0)

使用双引号对字符串进行

<when test='country == null or !country.equals("spain") or !country.equals("uk")'>
        AND country NOT IN ('spain','uk')
        </when>
        <otherwise>
        AND country=#{country}
 </otherwise>

答案 2 :(得分:0)

<when test="country !='spain' and country != 'uk'">

期望的条件错误,而不是