<select id="count" resultType="java.lang.Integer">
select count(*) from user where <if test="id != null">id = #{id}</if> and username ='xiaoming'</select>
如果传入的 id 不为 null, 那么才会 SQL 才会拼接 id = #{id}
如果传入的 id 为 null,那么最终的 SQL 语句就变成了 select count(*) from user where and username = ‘xiaoming’。这语句就会有问题,这时候 where 标签就该隆重登场了
二、where:有了我,SQL 语句拼接条件神马的都是浮云!
<select id="count" resultType="java.lang.Integer">
select count(*) from user
<where><if test="id != null">id = #{id}</if>
and username ='xiaoming'</where></select>
where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入 WHERE 子句
若语句的开头为 AND、OR,where 元素也会将它们去除。还可以通过 trim 标签去自定义这种处理规则
三、trim:我的地盘,我做主!
trim 标签一般用于拼接、去除 SQL 的前缀、后缀
trim 标签中的属性
属性
描述
prefix
拼接前缀
suffix
拼接后缀
prefixOverrides
去除前缀
suffixOverrides
去除后缀
<select id="count" result="java.lang.Integer">
select count(*) from user
<trim prefix ="where" prefixOverrides="and | or"><if test="id != null">id = #{id}</if><if test="username != null"> and username = #{username}</if></trim></select>
如果 id 或者 username 有一个不为空,则在语句前加入 where。如果 where 后面紧随 and 或 or 就会自动会去除
<select id="count" resultType="java.lang.Integer">
select count(*) from user where id in
<foreach collection="list" item="item" index="index"open="(" separator="," close=")">
#{item}</foreach></select>
如果参数类型为数组,则该值为 array
<select id="count" resultType="java.lang.Integer">
select * from user where id inarray
<foreach collection="array" item="item" index="index"open="(" separator="," close=")">
#{item}</foreach></select>
如果参数类型为 Map,则参数类型为 Map 的 Key
六、choose: 我选择了你,你选择了我!
<select id="count" resultType="Blog">
select count(*) from user
<choose><when test="id != null">
and id = #{id}</when><when test="username != null">
and username = #{username}</when><otherwise>
and age =18</otherwise></choose></select>
当 id 和 username 都不为空的时候, 那么选择二选一(前者优先)
如果都为空,那么就选择 otherwise 中的
如果 id 和 username 只有一个不为空,那么就选择不为空的那个
七、sql:相当于 Java 中的代码提重,需要配合 include 使用
<sql id="table"> user </sql>
八、include:相当于 Java 中的方法调用
<select id="count" resultType="java.lang.Integer">
select count(*) from <include refid=“table(sql 标签中的 id 值)” /></select>
九、bind:对数据进行再加工
<select id="count" resultType="java.lang.Integer">
select count(*) from user
<where><if test="name != null"><bind name="name" value="'%' + username + '%'"
name = #{name}</if></select>