使用<choose>
注:xxx为某某某的意思。
select * from xxx where 1=1
<choose>
<when test="xxx==xx">
SQL1
</when>
<when test="xx!=xx and xx!=null">
SQL2
</when>
<otherwise>
SQL3
</otherwise>
</choose>
如果 xxx==xx成立
则最终的SQL语句是
select * from xxx where 1=1 SQL1;
如果不成立则继续判断写一个when,
以此类推成则取之,不成则下一个,直到 <otherwise>来擦屁股。
注意: select * from xxx where 1=1 中1=1不能省略,否则报错
完整xxxMapper.xml例子
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.xxx.cccMapper">
<select id="语句id"
parameterType="com.xxx.xxx.ccc"
resultType="com.xxx.xxx.ccc">
select * from xxx where 1=1
<choose>
<when test="xxx==xx">
SQL1
</when>
<when test="xx!=xx and xx!=null">
SQL2
</when>
<otherwise>
SQL3
</otherwise>
</choose>
</select>
</mapper>