温馨提示×

如何在MyBatis的注解中使用elseif

小樊
90
2024-08-16 21:58:37
栏目: 编程语言

在MyBatis的注解中,可以使用 <choose> <when> <otherwise> 标签来实现类似于if/elseif/else的条件判断逻辑。具体使用方法如下:

@Select("SELECT * FROM table_name WHERE column = #{value}")
public List<Object> getData(@Param("value") String value) {
    return sqlSession.selectList("getData", value);
}
<select id="getData" resultType="Object">
    SELECT * FROM table_name
    <where>
        <choose>
            <when test="value != null and value != ''">
                AND column = #{value}
            </when>
            <otherwise>
                AND column = 'default_value'
            </otherwise>
        </choose>
    </where>
</select>

在这个示例中,如果传入的value不为空,则查询条件为 AND column = #{value},否则默认条件为 AND column = 'default_value'。这样就实现了在MyBatis的注解中使用类似于elseif的条件判断逻辑。

0