赞
踩
可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;
可以把包含内容的首部某些内容忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides;
案例:
<trim prefix="where " ,prefixOverrrides =" and |or" , suffix=" set" , suffixOverrides=", " ></trim>
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)
- select * from user
- <trim prefix="WHERE" prefixOverrides="AND |OR">
- <if test="name != null and name.length()>0"> AND name=#{name}</if>
- <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
- </trim>
-
-
-
- update user
- <trim prefix="set" suffixOverrides="," suffix=" where id = #{id} ">
- <if test="name != null and name.length()>0"> name=#{name} , </if>
- <if test="gender != null and gender.length()>0"> AND gender=#{gender} , </if>
- </trim>
-
-
-
- <insert id="save" parameterType="NoticeEntity">
- INSERT INTO S_NOTICE
- <trim prefix="(" suffix=")" suffixOverrides=",">
- ID,
- <if test="title != null">TITLE,</if>
- <if test="content != null">CONTENT,</if>
- <if test="noticeStatus != null">NOTICE_STATUS,</if>
- <if test="createdBy != null">CREATED_BY,</if>
- CREATED_TS,
- <if test="lastUpdBy != null">LAST_UPD_BY,</if>
- LAST_UPD_TS,
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides=",">
- SYS_GUID(),
- <if test="title != null">#{title,jdbcType=VARCHAR},</if>
- <if test="content != null">#{content,jdbcType=VARCHAR},</if>
- <if test="noticeStatus != null">#{noticeStatus,jdbcType=VARCHAR},</if>
- <if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
- systimestamp,
- <if test="lastUpdBy != null">#{lastUpdBy,jdbcType=VARCHAR},</if>
- systimestamp,
- </trim>
- </insert>
<foreach 参数如下.......></foreach>
collection:参数名称,根据Mapper接口的参数名确定,也可以使用@Param注解指定参数名
item:参数调用名称,通过此属性来获取集合单项的值
open:相当于prefix,即在循环前添加前缀
close:相当于suffix,即在循环后添加后缀
index:索引、下标
separator:分隔符,每次循环完成后添加此分隔符
in查询
- mapper接口:
- List<Employees> getEmployeesListParams(@Param("singleList") List<String> employeeIds);
-
- List<Employees> getEmployeesArrayParams(@Param("singleArry") String[] employeeIds);
-
- List<Employees> getEmployeesMapParams(Map<String,Object> params);
-
-
- XML实现:
- <select id="getEmployeesListParams" resultType="Employees">
- select *
- from EMPLOYEES e
- where e.EMPLOYEE_ID in
- <foreach collection="singleList" item="employeeId" index="index"
- open="(" close=")" separator=",">
- #{employeeId}
- </foreach>
- </select>
-
-
-
- <select id="getEmployeesArrayParams" resultType="Employees">
- select *
- from EMPLOYEES e
- where e.EMPLOYEE_ID in
- <foreach collection="singleArry" item="employeeId" index="index"
- open="(" close=")" separator=",">
- #{employeeId}
- </foreach>
- </select>
-
-
-
- <select id="getEmployeesMapParams" resultType="Employees">
- select *
- from EMPLOYEES e
- <where>
- <if test="departmentId!=null and departmentId!=''">
- e.DEPARTMENT_ID=#{departmentId}
- </if>
- <if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
- AND e.EMPLOYEE_ID in
- <foreach collection="employeeIdsArray" item="employeeId"
- index="index" open="(" close=")" separator=",">
- #{employeeId}
- </foreach>
- </if>
- </where>
- </select>
tips:此处没有 @Param("singleLIst") 注解时,XML里只能是 collection="list"
此处没有 @Param("singleArry") 注解时,XML里只能是 collection="array"
此处Map:不单单forech中的collection属性是map.key, 其它所有属性都是map.key,比如下面的departmentId
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
批量插入
- mapper接口
- int insertMultiUsers(@Param("users") List<User> users);
-
- XML实现:
- <insert id="insertMultiUsers">
- insert into user(user_name,gender,email,address,dept_id)
- values
- <foreach collection="users" item="user" separator=",">
- (#{user.userName},#{user.gender},#{user.email},#{user.address},#{user.deptId})
- </foreach>
- </insert>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。