Refactor: status to statuses using mybatis

1 min read
Before:
<if test="status == 0"> and t.status = 0 and t.status1 = 1 </if>
<if test="status == 1"> and t.status = 0 and t.status2 = 2 </if>
<if test ="status != 2"> and t.status2 != 2 </if>
After:
<if test="statuses != null and statuses.size() > 0">
and ( <foreach collection="statuses" item="status" separator=" OR ">
<choose>
<when test="status == 0">
( (t.status = 0 and t.status1 = 1) and (t.status2 != 2) )
</when>
<when test="status == 1">
( (t.status = 0 and t.status2 = 2) and (t.status2 != 2) )
</when>
<otherwise>1 = 1</otherwise>
</choose>
</foreach>
)
</if>
Points:
seperator = “ OR ”
Add
()
to eachif
statementAdd
otherwise
to the end to prevent other exception
0
Subscribe to my newsletter
Read articles from FENG JUNYUAN directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
