# 常见SQL SELECT * FROM goods where id in (1,2,3) SELECT * FROM goods where id in <foreach item="item" index="index" collection="ids.split(',')" open="(" separator="," close=")"> #{item} </foreach>
动态SQL
选择语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#choose、when、otherwise 类型其他语言中 switch select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
1 2 3 4 5 6 7 8
<choose> <when test="status == 0"> AND status = 0 </when> <otherwise> AND status >= #{status} </otherwise> </choose>
1 2 3 4 5 6 7 8 9 10
<if test="status != null and status != ''"> <choose> <when test="status == 0"> AND status = 0 </when> <otherwise> AND status >= #{status} </otherwise> </choose> </if>
> #gt,gte,lt,lte缩写的含义 > ## 这几个单词在范围查询的时候会用到 > gt: greater than 大于 > gte: greater than or equal 大于等于 > lt: less than 小于 > lte: less than or equal 小于等于 >
1 2 3 4 5 6 7 8 9 10 11 12 13
<!-- 范围查询, 注意这里大于小于符号 --> <where> <iftest="username != null and username != ''"> AND username like concat('%', #{username}, '%') </if> <iftest="beginTime != null and beginTime != ''"> and create_time >= #{beginTime} </if> <iftest="endTime != null and endTime != ''"> and create_time <= #{endTime} </if> AND del_flag = 0 </where>