<select id="countByStatus" resultType="int">
SELECT COUNT(distinct a.id)
FROM task et
inner join task_with_video b on a.id = b.task_id
where a.status !=2
<if test="status != null and status != @com.myweb.enum.TaskEnum@ALL">
and a.status = #{status}
</if>
<if test="@com.myweb.context.WebContextUser@contextUser() != null
and !@com.myweb.UserRoleEnum@isSuperAdmin(@com.myweb.context.WebContextUser@contextUser().userRole)">
<choose>
<when test="@com.myweb.context.WebContextUser@contextUser().videos.size() > 0">
AND b.videoName in
<foreach collection="@com.myweb.context.WebContextUser@contextUser().videos"
index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<otherwise>
and 1=0
</otherwise>
</choose>
</if>
</select>
//task运行状态Enum
package com.myweb.enum;
public enum TaskStatusEnum {
All(0),
...
int type;
}
//用户上下文
package com.myweb.context;
public class TaskStatusEnum {
public static ContextUser contextUser(){}
}
分析
<select id="调用mapper.xml的方法名" resultType="返回类型">
SELECT COUNT(distinct a.id)
FROM task et
inner join task_with_video b on a.id = b.task_id
where a.status !=2
<if test="status != null and status != @com.myweb.enum.TaskEnum@ALL"> //!!!枚举类型@枚举类型的一个value
and a.status = #{status}
</if>
<if test="@com.myweb.context.WebContextUser@contextUser() != null //!!!WebContextUser类@WebContextUser类中的contextUser()方法返回值 !=null
and !@com.myweb.UserRoleEnum@isSuperAdmin(@com.myweb.context.WebContextUser@contextUser().userRole)"> //!!!!UserRoleEnum类@类的isSuperAdmin()方法,参数为 WebContextUser@contextUser() 返回值的 userRole属性
<choose>
//!!!!WebContextUser@contextUser()方法返回值的videos属性的size()方法 返回值>0
<when test="@com.myweb.context.WebContextUser@contextUser().videos.size() > 0">
AND b.videoName in
<foreach collection="@com.myweb.context.WebContextUser@contextUser().videos"
index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<otherwise> //!!!! when 不成立的时候 进入 otherwise
and 1=0
</otherwise>
</choose>
</if>
</select>