MyBatis中動態sql 傳入枚舉類型與String字段對比

sql如果不想將邏輯寫在sql或代碼中分離出來的話可以使用mybatis標籤    
mybatis 做簡單條件判斷的時候可以選用<choose/> <when/>標籤
當傳入的封裝對象中有枚舉類型的時候是無法將枚舉類型直接和字符串繼續比較的,如下圖

<choose>
    <when test="queryType == 'DAY'">
        to_char(createTime,'YYYY-MM-dd')
    </when>
    <when test="queryType == 'HOUR'">
        to_char(createTime,'YYYY-MM-dd hh24')
    </when>
    <otherwise>
        to_char(createTime,'YYYY-MM')
    </otherwise>
</choose>


想要可以進行比較的花可以

<choose>
    <when test="queryType.toString == 'DAY'">
        to_char(createTime,'YYYY-MM-dd')
    </when>
    <when test="queryType.toString == 'HOUR'">
        to_char(createTime,'YYYY-MM-dd hh24')
    </when>
    <otherwise>
        to_char(createTime,'YYYY-MM')
    </otherwise>
</choose>    


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章