MyBatis 动态SQL
作用:简化sql语句动态拼串操作
if标签
根据需求拼接sql查询 <br /> test:判断的表达式<br />****

where标签
通过上面的例子可以看到,如果最后一个的值没有成立,倒数第二个的sql语句会多出一个and,导致报错<br /> 所以有了where标签,把if标签的内容放在where标签中,他可以帮我们去除条件不成立所导致的多余的条件(and、or等等)
<where><if test="id!=null">id>#{id}</if><if test="name!=null and !name.equals('');">and name like #{name}</if></where>
trim标签
截取字符串<br /> prefix="",前缀;为我们下面的sql整体添加一个前缀<br /> prefixOverrides="",去除整体字符串前面多余的字符<br /> suffix="",为整体添加一个后缀<br /> suffixOverrides="",后面那个多了可以去掉
<trim prefix="where" prefixOverrides="and" suffixOverrides="and"><where><if test="id!=null">id>#{id}</if><if test="name!=null and !name.equals('');">and name like #{name}</if></where></trim>
foreach标签
遍历集合<br /> collection="",指定要遍历的集合的key<br /> close="",以什么结束<br /> index="i",索引<br /> 如果遍历的是一个list;<br /> index:指定的变量保存了当前的索引<br /> item:保存当前遍历的元素的值<br /> 如果遍历的是一个map:<br /> index:指定的变量就是保存了当前遍历的元素key<br /> item:就是保存当前遍历的元素的值<br /> item="",每次遍历出的元素起一个变量名方便引用<br /> open="",以什么开始<br /> separator="",每次遍历的元素的分隔符
<select>select * from user where id in<!--(#{id_item},#{id_item},#{id_item})select * from user where id in (1,3,5)--><foreach collection="ids" item="id_item" separator="," open="(" close=")">#{id_item}</foreach></select>
choose标签
相当于if else
<select>select * from user<where><choose><when test="id!=null">id=#{id}</when><when test="name!==null and !name.equals('')">name=#{name}</when><when test="birthday!=null">birthday=#{birthday}</when><otherwise>1=1</otherwise></choose></where></select>
set标签
动态修改字段,不用全字段更新
<update>update user<set><if test="name!=null and !name.equals('')">name=#{name},</if><if test="birthday!=null and !birthday.equals('')">birthday=#{birthday},</if><if test="address!=null and !address.equals('')">address=#{address}</if></set></update>
