可以使用 <if> 标签和逻辑运算符 and 来判断两个参数都不为空,示例如下:

<select id="findUser" parameterType="map" resultType="User">
  SELECT * FROM user 
  WHERE 
    <if test="name != null and age != null">
      name = #{name} and age = #{age}
    </if>
    <if test="name != null and age == null">
      name = #{name}
    </if>
    <if test="name == null and age != null">
      age = #{age}
    </if>
</select>

上述示例中,使用了三个 <if> 标签分别判断了两个参数都不为空、只有 name 不为空和只有 age 不为空的情况。注意在参数中使用 Map 类型,键为参数名,值为参数值。在 SQL 语句中使用 #{} 占位符表示参数

mybatis xml 判断两个参数都不为空

原文地址: https://www.cveoy.top/t/topic/gRdC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录