Mybatis-plus 带条件的查询select语句示例
程序员文章站
2022-03-11 18:57:04
...
通常在查询某个列表时需要根据条件过滤一下数据,下面是示例:
<select id="getUserList" parameterType="java.util.Map" resultMap="UserResultMap">
SELECT
*
FROM
user u
<where>
<if test="userId != null">
and u.user_id = #{userId}
</if>
<if test="code != null">
and u.code = #{code}
</if>
<!--查询多个状态下的数据-->
<if test="status != null">
and u.status in
<foreach item="status" collection="orderStatus" separator="," open="(" close=")" index="">
#{status}
</foreach>
</if>
<!--查询某个区间内的数据-->
<if test="startTime != null and startTime !=''.toString()">
<![CDATA[ and DATE_FORMAT(u.created_at,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d') ]]>
</if>
<if test="endTime != null and endTime !=''.toString()">
<![CDATA[ and DATE_FORMAT(#{endTime},'%Y-%m-%d') > DATE_FORMAT(u.created_at,'%Y-%m-%d') ]]>
</if>
</where>
ORDER BY u.created_at DESC
</select>
知足且坚定,温柔且上进
上一篇: 浅拷贝和深拷贝