1,数据库status字段为char(1)
2,mybatis.xml要根据status的值做处理
<if test="status != null and status !='' ">
<choose>
<!-- 如果传入的status是0,查询status为0或1的数据 -->
<when test="status == '0'">
AND pa.status IN ('0', '1')
</when>
<when test="status == '1'">
AND pa.status IN ('0', '1')
</when>
<!-- 如果传入的status是2、3、4,直接使用等于条件 -->
<when test="status == '2' or status == '5' or status == '4'">
AND pa.status = #{status}
</when>
<!-- 其他情况,可以选择不添加任何条件,或者根据需要添加默认行为 -->
<otherwise>
AND pa.status = '5'
<!-- 这里可以留空,或者添加你认为合适的默认SQL条件 -->
</otherwise>
</choose>
</if>
3, 这种情况,前端传参为字符串"0"会出现拼接pa.status='5' 前端传参字符串"2"会拼接pa.status='5' 前端传参字符串"1"会拼接pa.status='5'
4,解决办法
<if test="status != null and status !=''.toString() ">
<choose>
<!-- 如果传入的status是0,查询status为0或1的数据 -->
<when test="status == '0'.toString()">
AND pa.status IN ('0', '1')
</when>
<when test="status == '1'.toString()">
AND pa.status IN ('0', '1')
</when>
<!-- 如果传入的status是2、3、4,直接使用等于条件 -->
<when test="status == '2'.toString() or status == '5'.toString() or status == '4'.toString()">
AND pa.status = #{status}
</when>
<!-- 其他情况,可以选择不添加任何条件,或者根据需要添加默认行为 -->
<otherwise>
AND pa.status = '5'
<!-- 这里可以留空,或者添加你认为合适的默认SQL条件 -->
</otherwise>
</choose>
</if>
5,虽然toStirng()绝大数情况都不需要加,但不知道为什么Char比较特殊,查资料无果,故记录。