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};