if标签
从下面这个查询语句我们可以很容易的看出,if标签其实是做判断使用的,当满足条件的时候执行对应的语句
<select id="getCustomerByNameAndPossition" resultType="com.ctbu.domain.Customer">
select * from `customer`
<where>
<if test="cust_name !=null and cust_name !=''">
cust_name=#{cust_name}
</if>
<if test="cust_profession !=null and cust_profession !=''">
and cust_profession=#{cust_profession}
</if>
</where>
</select>
where标签
可以自动生成sql语句中的where以及删除where之后的第一个and
trim标签
prefixOverrides="and" :前缀覆盖,覆盖第一个条件之前的相应内容
suffixOverrides="and" :后缀覆盖,覆盖最后一个条件之后的相应内容
prefix="" :前缀,在第一个条件之前添加的内容
suffix="":后缀,在最后一个条件之后添加的内容
<trim prefixOverrides="and" suffixOverrides="and">
<if test="cust_name !=null and cust_name !=''">
cust_name=#{cust_name}
</if>
<if test="cust_profession !=null and cust_profession !=''">
and cust_profession=#{cust_profession}
</if>
</trim>
choose标签
判断当前条件如果成立,则不会判断后面的是否成立,如果前面的都不成立,则执行otherwise中的内容
<select id="getCustomerByNameAndPossition" resultType="com.ctbu.domain.Customer">
select * from `customer`
<where>
<choose>
<when test="cust_name != null">
cust_name=#{cust_name}
</when>
<when test="cust_profession != null">
cust_profession=#{cust_profession}
</when>
<otherwise>
cust_name='李白'
</otherwise>
</choose>
</where>
</select>