您的位置:首页 > 数据库

Mybatis框架-23:动态SQL——if标签、where标签、trim标签、choose标签

2019-06-09 16:55 639 查看

if标签

从下面这个查询语句我们可以很容易的看出,if标签其实是做判断使用的,当满足条件的时候执行对应的语句

[code]<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标签

[code]prefixOverrides="and" :前缀覆盖,覆盖第一个条件之前的相应内容

suffixOverrides="and" :后缀覆盖,覆盖最后一个条件之后的相应内容

prefix="" :前缀,在第一个条件之前添加的内容

​​​​​​​​​​​​​​suffix="":后缀,在最后一个条件之后添加的内容
[code]<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中的内容

[code]<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>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐