Mybatis where절 if문으로 처리

2025. 1. 19. 18:44Java/mybatis

<select id="getFilteredRecords" parameterType="map" resultType="YourResultType">
    SELECT *
    FROM your_table
    <where>
        <!-- Check if parameter 'param1' is not null or empty -->
        <if test="param1 != null and param1 != ''">
            AND column1 = #{param1}
        </if>
        
        <!-- Check if parameter 'param2' is not null or empty -->
        <if test="param2 != null and param2 != ''">
            AND column2 = #{param2}
        </if>
        
        <!-- Add additional conditions as needed -->
        <if test="param3 != null and param3 != ''">
            AND column3 LIKE CONCAT('%', #{param3}, '%')
        </if>
    </where>
</select>