Mybatis动态sql条件查询、判断空值和空字符串

书欣 MyBatis 发布时间:2022-08-06 19:38:52 阅读数:2566 1
下文笔者讲述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};
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/MyBatis/202208/4161.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者