动态sql

动态sql就是给不确定的传入参数的时候用的sql语句,就是通过各种条件判断决定执行那几句sql


1.if条件

select spy_info_stu_base.stu_id,spy_info_stu_base.stu_name,spy_info_stu_base.sex,spy_info_stu_base.age,spy_info_stu_base.birthday,spy_info_stu_base.province,spy_info_stu_base.city,spy_info_stu_base.area,spy_info_stu_base.current_status,spy_info_stu_base.education,spy_info_stu_base.school,spy_info_stu_base.major,spy_info_stu_base.skill,spy_info_stu_extend.extend_info
               ,spy_interaction_ent_follow_stu.id
               from spy_info_stu_base left JOIN spy_info_stu_extend on spy_info_stu_base.stu_id = spy_info_stu_extend.stu_id and spy_info_stu_base.integrity_status = 1
               left JOIN spy_interaction_ent_follow_stu on spy_interaction_ent_follow_stu.stu_id = spy_info_stu_base.stu_id and spy_interaction_ent_follow_stu.ent_id = #{entId} and spy_info_stu_base.integrity_status = 1
               and spy_info_stu_base.education = #{education}
               and spy_info_stu_base.school = #{school}
               and spy_info_stu_base.major = #{major}
               and spy_info_stu_base.stu_name like #{keyWord}
               where spy_info_stu_base.integrity_status = 1 
               Order By spy_info_stu_base.last_update_date Desc
               limit #{startNum},#{pageSize}

跟java里的if判断没啥差别,把要判断的条件写在if标签里,通过就执行写在sql的后面


2.trim条件


  ... 

这个就是where的后面要是紧跟着一个and或者是or,把and或or删了


3.choose条件


    
      AND a like #{a}
    
    
      AND b like #{b}
    
    
      AND featured = 1
    
  

这个就是java里的switch,a不等于1要a,a和b都不等于1要b,上面两个条件都不满足要otherwise标签里的值


你可能感兴趣的:(动态sql)