sql基本操作

1.批量删除

<delete id="deleteByCondition" parameterType="java.util.Map">
  DELETE FROM student
  <where>
    <if test="studentIds!=null and studentIds.size > 0">
      AND student_id in

      <foreach collection="studentIds" item="idItem" index="index" open="(" separator="," close=")">
        #{idItem}
      </foreach>
    </if>
    <if test="courseId!=null">
      AND course_id=#{courseId}
    </if>
  </where>
</delete>

 

2.批量新增

<insert id="batchCreate" useGeneratedKeys="false" keyProperty="id" parameterType="java.util.List">
  INSERT INTO student(id,name)
  values
    <foreach collection="students" item="item" index="index" separator=",">
    (#{item.id},#{item.name})
    </foreach>
</insert>

 

3.批量获取

<select id="listByCondition" resultType="student"  parameterType="java.util.Map">
  SELECT * FROM student
  <where>
    <if test="studentIdentityIds!=null and studentIdentityIds.size > 0">
      AND student_identity_id in
      <foreach collection="studentIdentityIds" item="idItem"

      index="index" open="(" separator="," close=")">
        #{idItem}
      </foreach>
    </if>
    <if test="courseId!=null">
      AND course_id=#{courseId}
    </if>
  </where>
</select>

 

4.批量更新

情况一:更新不同对象向的相同字段的相同信息

<update id="batchUpdate" parameterType="java.util.Map">
UPDATE goods
  SET `time` = #{time},
  `status` = #{status}
WHERE id IN
  <foreach collection="goodsIds" item="item" open="("
    separator="," close=")" index="index">
    #{item}
  </foreach>

</update>

 

情况二:更新不同的实体

<update id="batchUpdate" parameterType="java.util.List">
  UPDATE `${businessKey}`
  <trim prefix="set" suffixOverrides=",">

    <trim prefix="reference_number =case" suffix="end,">
      <foreach collection="fileInfos" item="it" index="index">
        <if test="it.referenceNumber!=null">
          when file_uuid=#{it.fileUUID,jdbcType=VARCHAR} then
          #{it.referenceNumber,jdbcType=INTEGER}
        </if>
      </foreach>
    </trim>


    <trim prefix="name =case" suffix="end,">
      <foreach collection="fileInfos" item="it" index="index">
        <if test="it.name!=null">

          when file_uuid=#{it.fileUUID,jdbcType=VARCHAR} then
          #{it.name,jdbcType=VARCHAR}
        </if>
      </foreach>
    </trim>


  </trim>


  WHERE `file_uuid` IN
  <foreach collection="fileInfos" index="index" item="it"
    separator="," open="(" close=")">
      #{it.fileUUID,jdbcType=VARCHAR}
  </foreach>
</update>

 

没有分析和解释请见谅

posted @ 2020-06-04 09:22  千严-Jonty  阅读(166)  评论(0编辑  收藏  举报