만들어가는 세상

[mybatis] thymeleaf, java,mybatis 부등호 검색조건 처리 방법 본문

IT/ETC

[mybatis] thymeleaf, java,mybatis 부등호 검색조건 처리 방법

윤재웅 2019. 10. 25. 13:27

부등호 검색조건 처리 방법 >, <, >=, <=, =

예) 1 >= 2, 1 > 2, 1 < 2 등 검색조건 입니다.

html
<input type="number" min="1" max="100" class="search-input form-control" name="stAverage" />
<select name="operator" class="form-control form-control search-select">
        <option value="=">=</option>
        <option value="&gt;=">&ge;</option>
        <option value="&lt;=">&le;</option>
        <option value="&gt;">&gt;</option>
        <option value="&lt;">&lt;</option>
</select>
<input type="number" min="1" max="100" class="search-input form-control" name="endAverage" />
java
vo.java
private String stAverage;
private String endAverage;    
private String operator;
private String endOperator;
public void setEndOperator(String operator){
    if( "<=".equals(getOperator())) {
        this.endOperator = ">=";
    }else if(">=".equals(getOperator())) {
        this.endOperator = "<=";
    }else if(">".equals(getOperator())) {
        this.endOperator = "<";
    }else if(">".equals(getOperator())) {
        this.endOperator = "<";
    }
}

Controller.java
parameterVo.setEndOperator(parameterVo.getOperator());
mybatis
mybatis.xml
SELECT * FROM
<if test="stAverage != null and stAverage != ''">
    WHERE ASSESSMENT_POINT <![CDATA[${operator}]]> #{stAverage}
</if>
<if test="endAverage != null and endAverage != '' ">
    <if test="endOperator != null and endOperator != '' ">
        AND ASSESSMENT_POINT <![CDATA[${endOperator}]]> #{endAverage}
    </if>
</if>
Comments