注:xxx为某某某的意思
基础代码
<?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="xxx.xxx.xxxMapper">
<select id="findByx" parameterType="int"
resultType="com.xx.xxx.xx">
select * from xxx
</select>
</mapper>
要对select * from xxx;附加条件筛选,
1,,用where 1=1加筛选标签:select * from xxx 加 where 1=1 加<if><choose><when>等筛选标签。
select * from where 1=1
<if test="xxx!=xx">
and SQL1
</if>
2,,用where标签内嵌筛选标签:select * from xxx 加<where><if><choose><when></where>。
select * from xxx
<where>
<if test="xxx!=xx">
and SQL1
</if>
<if test="xxx==x">
and SQL2
</if>
</where>
如果where内有筛选标签成立,比如xxx!=xx
那么最终SQL为 select * from xxx SQL1;
where标签会对其中的内容检查,让最终SQL不以and和or这些关键字直接作开头接上。
比如 select * from xxx and SQL1;------> select * from xxx SQL1;
如果where内没有筛选标签成立,则最终SQL直接没有where部分,
那么最终SQL为 Lselect * from xxx;
另外<trim>也可以实现上述功能,见下一篇。