mybatis 语句中where 后边要跟必要条件和多个选择条件处理方法

时间:2022-12-29 16:35:19

<select id="serchRelation" resultType="Relation">
SELECT
r.node_one as nodeOne,
r.node_two as nodeTwo,
r.relation as relation,
r.is_delete as isDelete,
r.create_time as createTime,
n.model_name as nodeTwoName
FROM relation r LEFT JOIN node n on r.node_two = n.id

<!-- and或or 只会去掉多余的,不会自动加上 -->
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="@com.zyw.util.TmStringUtils@isNotEmpty(keyWord) and @com.zyw.util.TmStringUtils@isNumber(keyWord)">
r.node_two = #{keyWord} OR
</if>
<if test="@com.zyw.util.TmStringUtils@isNotEmpty(keyWord)">
n.model_name LIKE '%${keyWord}%' OR
</if> 
<if test="@com.zyw.util.TmStringUtils@isNotEmpty(keyWord)">
r.relation LIKE '%${keyWord}%'
</if>
</trim>
group by r.node_one,r.node_two
having r.is_delete = 0 and r.node_one=#{nodeId1}

</select>

选择条件可以用<trim>标签,必要条件,可以group by 分组之后 写在 having 后边