1) SQL Where condition
Select Id= "Find" parametertype= "user" resulttype= "user" >select Id,name, age,address from User_c where 1=1<if test= "Id!=null" >and id=#{id}</if><if test= "Name!=null" >and name like "%" #{name} "%" </if><if test= " Age!=null ">and age=#{age}</if><if test=" Address!=null ">and address=#{address}</if></ Select>
2) <where>...</where> will automatically remove the and in SQL without having to write where 1=1
Select <include refid= "cols"/> from user_c<where><if test= "Name!=null" > and name like "%" #{name} "%" </if><if test= "Age!=null" > and age=#{age}</if><if test= "Address!=null" > and address like #{ Address}</if></where>
3) <set>...</set> Label
Removes the extra comma from the set last condition in update SQL
<update id= "Update" parametertype= "User" >update user_c<set><if test= "Name!=null" >and name like "%" # {name} "%", </if><if test= "Age!=null" >age=#{age},</if><if test= "Address!=null" >address=#{ Address},</if></set>where id=#{id}</update>
MYBATIS_ Dynamic SQL query, update