写代码的时候,在查询单人聊天时,可能会涉及到人员1和人员2的结合(且)查询功能。
其中的人员1既可能是发送方,也可能是接受方,同理,人员2也既可能是接受方,也可能是发送方。
逻辑其实可以归为以下几点:
① 人员1不为空 并且 人员2为空
<if test="(user !=null and user !='') and (user2==null or user2=='')">
and
(
t.msgfrom= #{user,jdbcType=VARCHAR} or t2.name = #{user,jdbcType=VARCHAR}
or
t.msgto= #{user,jdbcType=VARCHAR} or t3.name = #{user,jdbcType=VARCHAR}
)
</if>
②人员2不为空 并且 人员1为空
<if test="(user2!=null and user2!='') and (user ==null or user =='')">
and
(
t.msgfrom= #{user2,jdbcType=VARCHAR} or t2.name = #{user2,jdbcType=VARCHAR}
or
t.msgto= #{user2,jdbcType=VARCHAR} or t3.name = #{user2,jdbcType=VARCHAR}
)
</if>
③人员1和人员2都不为空
<if test="(user !=null and user !='') and (user2!=null and user2!='')">
and
(
(
(t.msgfrom= #{user,jdbcType=VARCHAR} or t2.name = #{user,jdbcType=VARCHAR})
and
(t.msgto= #{user2,jdbcType=VARCHAR} or t3.name = #{user2,jdbcType=VARCHAR})
)
or
(
(t.msgfrom= #{user2,jdbcType=VARCHAR} or t2.name = #{user2,jdbcType=VARCHAR})
and
(t.msgto= #{user,jdbcType=VARCHAR} or t3.name = #{user,jdbcType=VARCHAR})
)
)
</if>
④人员1和人员2都为空 (这种相当于查询全部)。