MyBatis动态sql是做什么的?都有哪些动态sql?能简述一下动态sql的执行原理不?

MyBatis动态sql浏览:2434收藏:1
答案:
Mybatis动态sql可以让我们在Xml映射文件内,以标签的形式编写动态sql,完成逻辑判断和动态拼接sql的功能,Mybatis提供了9种动态sql标签           trim|where|set|foreach|if|choose|when|otherwise|bind。
<!--   
    模糊查询:根据条件查询用户(姓名模糊匹配,年龄在指定的最小值和最大值之间)  
     SQL:select * from d_user where name like '%o%' and age>=13 and age<=18  
 -->  
 <!--定义操作 classes 表的sql 映射文件:userMapper2.xml   -->  
 <mapper namespace="com.mybatis.test6.userMapper2">  
    
  <select id="getUser" parameterType="User"  resultType="User">               
       select * from d_user where age between #{minAge} and  #{maxAge}  
               
       <!--动态sql语句:当输入的name!=null 时就执行下面的语句 -->  
         <if test=' name != "%null%" '>  
             and name like #{name}  
         </if>   
           
     </select>   
       
</mapper> 

其执行原理为,使用OGNL从sql参数对象中计算表达式的值,根据表达式的值动态拼接sql,以此来完成动态sql的功能。