栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Mybatis】动态sql

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Mybatis】动态sql

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了
解决 拼接SQL语句字符串时的痛点问题。

标签
  • if
  • where
  • trim
  • choose、when、otherwise
  • foreach
  • sql

if

if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中


    
    
        select * from t_emp where 1=1
        
          and  emp_name=#{empName}
        
        
           and age=#{age}
        
        
           and  sex=#{sex}
        
        
           and  email=#{email}
        
    
where

where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现


    
        select * from t_emp
        
            
                emp_name=#{empName} and
            
            
                age=#{age}  and
            
            
              sex=#{sex}   and
            
            
               email=#{email}  and
            
        
    
choose、when、otherwise

choose:
when:
otherwise:
choose标签为父标签,when与otherwise需要写在choose中
这一套标签相当于Java中的if,else if else,when相当于if,else if ,otherwise相当于else
when至少要有一个,otherwise最多只能有一个