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};
本文详细介绍使用MyBatis进行动态SQL条件查询的方法,通过示例代码展示如何根据参数是否为空来动态调整SQL语句,实现高效灵活的数据检索。
7856

被折叠的 条评论
为什么被折叠?



