Mybatis常用标签
- #{} 和 ${} 的区别:
- 相同点:
#{}:可以获取map中的值或者pojo对象属性的值。 ${}:可以获取map中的值或者pojo对象属性的值。
- 不同点:
#{}:是以预编译的形式,将参数设置到sql语句中,防止sql注入。 ${}:取出的值直接拼装在sql语句中,会有安全问题。 因此大多情况下,我们取参数的值都应该去使用#{},除了一些特定场景,需要在预编译前拼接sql语句的情况,比如按照年份分表查询。 同时#{}它支持更丰富的用法:规定参数的一些规则:javaType、jdbcType、mode(存储过程)、numericScale、 resultMap、typeHandler、jdbcTypeName,例如:#{email,jdbcType=OTHER}。
- if 标签
- if的主要作用就是判断字段是否为null或者为""字符串
<select id="selectEmployee" resultType="com.core.Employee">
select * from employee where
<if test = "id != null and id != ''">
id = #{id}
</if>
<if test = "lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
</select>
-
where 标签
- where标签一般是配合 if 标签使用,主要作用是当if标签中的id为空时,sql语句中就会出现where and last_name like 这种情况,显然是错误sql,where会将多余的and删去,并会加上where语句。
<select id="selectEmployee" resultType="com.core.Employee"> select * from employee <where> <if test="id != null and id != ''"> id = #{id} </if> <if test="lastName != null and lastName != ''"> and last_name like #{lastName} </if> </where> </select>
-
set 标签
- set标签和where标签类似,set标签是将多余的逗号(,)去掉,set标签一般用在更新语句中。
<update id="updateEmployee" resultType="com.core.Employee"> update employee <set> <if test="id != null and id != ''"> id = #{id} </if> <if test="lastName != null and lastName != ''"> and last_name like #{lastName} </if> </set> where code = #{code} and base = #{base} </update>
-
trim 标签
- trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。
<select id="selectEmployee" resultType="com.core.Employee"> select * from employee <!-- trim: prefix:给拼串后的整个字符串加一个前缀 prefixOverrides:去掉整个字符串前面多余的字符,支持或(|) suffix:给拼串后的整个字符串加一个后缀 suffixOverrides:去掉整个字符串后面多余的字符,支持或(|) --> <trim prefix="where" suffixOverrides="AND | OR"> <if test="id != null and id != ''"> id = #{id} and </if> <if test="lastName != null and lastName != ''"> last_name like #{lastName} and </if> </trim> </select>
-
choose 标签
- choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>
- when元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时候,就会跳出 choose,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的内容。所以上述语句的意思非常简单, 当 title!=null 的时候就输出 and titlte = #{title},不再往下判断条件,当title为空且 content!=null 的时候就输出 and content = #{content},当所有条件都不满足的时候就输出 otherwise 中的内容。
-
foreach 标签
- foreach标签经常用于遍历集合,构建in条件语句或者批量操作语句。
foreach: collection:指定要遍历的集合(list类型的参数会特殊处理封装在map中,map的key就叫list item:将当前遍历出的元素赋值给指定的变量 separator:每个元素之间的分隔符 open:遍历出所有结果拼接一个开始的字符串 close:遍历出所有结果拼接一个结束的字符串 index:遍历list的时候,index就是索引,item就是当前值 遍历map的时候,index就是map的key,item就是map[key]的值 #{变量名}就能取出变量的值也就是当前遍历出的元素 <select id="getEmpsByConditionForeach" resultType="com.caochenlei.mybatis.crud.Employee"> select * from employee <foreach collection="ids" item="item_id" separator="," open="where id in(" close=")"> #{item_id} </foreach> </select> <insert id="addEmps"> insert into employee (id,last_name,email,gender,dep_id) values <foreach collection="emps" item="emp" separator=","> (#{emp.id},#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dep.depId}) </foreach> </insert>