1、foreach
foreach元素的属性主要有item,index,collection,open,separator,close。
item:集合中元素迭代时的别名;index:集合中元素迭代时的索引;
open:常用语where语句中,表示以什么开始,比如以'('开始;
separator:表示在每次进行迭代时的分隔符;
close 常用语where语句中,表示以什么结束;
示例1:
<!--批量增加多条数据--> <insert id="addStudentList" parameterType="java.util.List"> <!--insert into student (name,sex)VALUES (student1.name,student1.sex),(student2.name,student2.sex)...--> INSERT INTO student (student.name,sex,qq,whatType,joinTime,school,student_id,link,wishes,tutorBro,knowFrom,create_at) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.name},#{item.sex},#{item.qq},#{item.whatType},#{item.joinTime},#{item.school},#{item.student_id}, #{item.link},#{item.wishes},#{item.tutorBro},#{item.knowFrom},#{item.create_at}) </foreach> </insert>
对数据库进行批量操作的时候就会用到foreach标签,上示代码就相当于sql语句中的insert into table (id,name)values(?,?),(?,?),(?,?)...,而用foreach就可以将传入的list/array/map进行迭代,然后给对应的占位符赋值,最后执行sql语句,达到一次性插入多条语句的目的。
2、concat
concat可以用用来进行模糊查询,并且可以防止sql注入
示例2:
<!--通过String模糊查询查询--> <select id="getOneByKey" parameterType="java.lang.String" resultType="student"> SELECT * FROM student <where> student.name LIKE CONCAT(CONCAT('%', #{key}),'%') </where> </select>
3、choose
choose标签一般配合when/otherwise标签使用,相当于java中的switch。choose为switch,when为case,otherwise则为 default,也就是说使用choose做条件判断的时候,当when中按顺序执行,满足则跳出;如果都不满足则执行otherwise。即示例3中,如果传入参数student中id不为空并且大于零,则执行student.id=#{id},后续就不再执行了;否则进行判断;若都没有则条件变为where student.id = 2。
示例3:
<select id="select" parameterType="student" resultType="student"> SELECT * FROM student <where> <choose> <when test="id !=null and id >0"> student.id = #{id} </when> <when test="name !=null "> student.name LIKE CONCAT(CONCAT('%', #{name}),'%') </when > <when test="sex != null and sex != '' "> AND student.sex = #{sex,} </when > <when test="qq != null "> AND student.qq = #{qq,} </when > <otherwise> student.id = 2 </otherwise> </choose> </where> </select>
4、if
if标签的作用与java中的if几乎一毛一样,唯一不同的是没有else。所以只能将需要的情况都列出来。if一般与where/set标签组合使用,可以做简单的条件判断,已达到减少代码的目的。
示例4:
<update id="updateOne" parameterType="student"> UPDATE student <set> <!--id为updateSet的sql片段--> <include refid="updateSet"/> </set> <where> <if test="id != null and id != ''"> id = #{id} </if> </where> </update>
<sql id="updateSet"> <if test="name !=null and name !=''"> student.name=#{name}, </if> <if test="sex !=null and sex !=''"> sex=#{sex}, </if> <if test="qq !=null and qq !=''"> qq=#{qq}, </if> <if test="whatType !=null and whatType !=''"> whatType=#{whatType}, </if> <if test="joinTime !=null and joinTime!=''"> joinTime=#{joinTime}, </if> <if test="school !=null and school !=''"> school=#{school}, </if> <if test="student_id !=null and student_id !=''"> student_id=#{student_id}, </if> <if test="link !=null and link !=''"> link=#{link}, </if> <if test="wishes !=null and wishes !=''"> wishes=#{wishes}, </if> <if test="tutorBro !=null and tutorBro !=''"> tutorBro=#{tutorBro}, </if> <if test="knowFrom !=null and knowFrom !=''"> knowFrom=#{knowFrom}, </if> update_at=#{update_at} </sql>
5、if标签与choos标签的区别
if是不管条件成不成立都会执行下一条if;而choose中,如果when中先执行判断的成立了,则不会再往后执行。一个不太准确的比喻:类似“&”与“&&”?