当前位置:   article > 正文

Mybatis 标签 [ trim / foreach]_mybatis trim foreach

mybatis trim foreach

1、trim(可以用来代替 where

可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;

可以把包含内容的首部某些内容忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides;

案例:

<trim  prefix="where " ,prefixOverrrides =" and  |or" ,  suffix=" set" , suffixOverrides=", " ></trim>

prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)

  1. select * from user
  2.   <trim prefix="WHERE" prefixOverrides="AND |OR">
  3.     <if test="name != null and name.length()>0"> AND name=#{name}</if>
  4.     <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
  5.   </trim>
  6. update user
  7.   <trim prefix="set" suffixOverrides="," suffix=" where id = #{id} ">
  8.     <if test="name != null and name.length()>0"> name=#{name} , </if>
  9.     <if test="gender != null and gender.length()>0"> AND gender=#{gender} , </if>
  10.   </trim>
  11. <insert id="save" parameterType="NoticeEntity">
  12. INSERT INTO S_NOTICE
  13. <trim prefix="(" suffix=")" suffixOverrides=",">
  14. ID,
  15. <if test="title != null">TITLE,</if>
  16. <if test="content != null">CONTENT,</if>
  17. <if test="noticeStatus != null">NOTICE_STATUS,</if>
  18. <if test="createdBy != null">CREATED_BY,</if>
  19. CREATED_TS,
  20. <if test="lastUpdBy != null">LAST_UPD_BY,</if>
  21. LAST_UPD_TS,
  22. </trim>
  23. <trim prefix="values (" suffix=")" suffixOverrides=",">
  24. SYS_GUID(),
  25. <if test="title != null">#{title,jdbcType=VARCHAR},</if>
  26. <if test="content != null">#{content,jdbcType=VARCHAR},</if>
  27. <if test="noticeStatus != null">#{noticeStatus,jdbcType=VARCHAR},</if>
  28. <if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
  29. systimestamp,
  30. <if test="lastUpdBy != null">#{lastUpdBy,jdbcType=VARCHAR},</if>
  31. systimestamp,
  32. </trim>
  33. </insert>

2、foreach(查询in,批量插入)

<foreach  参数如下.......></foreach>

collection:参数名称,根据Mapper接口的参数名确定,也可以使用@Param注解指定参数名

item:参数调用名称,通过此属性来获取集合单项的值

open:相当于prefix,即在循环前添加前缀

close:相当于suffix,即在循环后添加后缀

index:索引、下标

separator:分隔符,每次循环完成后添加此分隔符

 

in查询

  1. mapper接口:
  2. List<Employees> getEmployeesListParams(@Param("singleList") List<String> employeeIds);
  3. List<Employees> getEmployeesArrayParams(@Param("singleArry") String[] employeeIds);
  4. List<Employees> getEmployeesMapParams(Map<String,Object> params);
  5. XML实现:
  6. <select id="getEmployeesListParams" resultType="Employees">
  7. select *
  8. from EMPLOYEES e
  9. where e.EMPLOYEE_ID in
  10. <foreach collection="singleList" item="employeeId" index="index"
  11. open="(" close=")" separator=",">
  12. #{employeeId}
  13. </foreach>
  14. </select>
  15. <select id="getEmployeesArrayParams" resultType="Employees">
  16. select *
  17. from EMPLOYEES e
  18. where e.EMPLOYEE_ID in
  19. <foreach collection="singleArry" item="employeeId" index="index"
  20. open="(" close=")" separator=",">
  21. #{employeeId}
  22. </foreach>
  23. </select>
  24. <select id="getEmployeesMapParams" resultType="Employees">
  25. select *
  26. from EMPLOYEES e
  27. <where>
  28. <if test="departmentId!=null and departmentId!=''">
  29. e.DEPARTMENT_ID=#{departmentId}
  30. </if>
  31. <if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
  32. AND e.EMPLOYEE_ID in
  33. <foreach collection="employeeIdsArray" item="employeeId"
  34. index="index" open="(" close=")" separator=",">
  35. #{employeeId}
  36. </foreach>
  37. </if>
  38. </where>
  39. </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.

 

批量插入

  1. mapper接口
  2. int insertMultiUsers(@Param("users") List<User> users);
  3. XML实现:
  4. <insert id="insertMultiUsers">
  5. insert into user(user_name,gender,email,address,dept_id)
  6. values
  7. <foreach collection="users" item="user" separator=",">
  8. (#{user.userName},#{user.gender},#{user.email},#{user.address},#{user.deptId})
  9. </foreach>
  10. </insert>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/855361
推荐阅读
相关标签
  

闽ICP备14008679号