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