MYSQL Mybatis 動態sql條件查詢、判斷空值和空字符串

MYSQL Mybatis 動態sql條件查詢、判斷空值和空字符串

 

@Select("<script>" +
            "SELECT * FROM table_name WHERE 1=1" +
            "<if test='templateCode!=null'>" +
            "and template_code = #{templateCode}" +
            "</if>" +
            "<if test='templateCode==null'>" +
            "and (template_code  IS NULL or template_code='')" +
            "</script>")

templateCode是傳入的參數名

table_name是表名

template_code是數據庫的列名

實現的功能

當傳入的templateCode爲空時,執行sql

select * from table_name where 1=1 and (template_code  IS NULL or template_code='');

當傳入的templateCode不爲空時,執行sql

select * from table_name where 1=1 and template_code = #{templateCode};

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