当前位置:   article > 正文

mybatis针对Oracle数据库进行(单个或多个条件)批量操作(新增、修改、删除)的sql写法--mysql_oracle更改多条件

oracle更改多条件
1、批量新增:
 
  1. <insert id="addMonthDutyIntoDB" parameterType="java.util.List">
  2. insert into TB_DUTY select SEQ_TB_DUTY.nextval,A.* from(
  3. <foreach collection="list" item="item" index="index" separator="union">
  4. SELECT #{item.dscd}, #{item.unitId},#{item.year},#{item.month},#{item.day},#{item.weekDay},
  5. #{item.morningPeopleIds}, #{item.morningPeopleNames},#{item.afternoonPeopleIds},#{item.afternoonPeopleNames},#{item.eveningPeopleIds},
  6. #{item.eveningPeopleNames},#{item.leaderIds},#{item.leaderNames},#{item.flag},#{item.remark},#{item.day0} FROM DUAL
  7. </foreach>
  8. ) A
  9. </insert>

2、批量修改:
单个条件、单个修改字段:
  1. <update id="auditMultiByIds" parameterType="java.util.List">
  2. update tb_code_name_result_new set state=#{state,jdbcType=INTEGER} where id in
  3. <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
  4. #{item}
  5. </foreach>
  6. </update>

多个条件、多个修改字段:
  1. <update id="updateByMultiConditions" parameterType="java.util.List">
  2. <foreach collection="list" item="item" index="index" open="begin" close="; end;" separator=";">
  3. update TB_DUTY
  4. <set>
  5. <if test="item.morningPeopleIds != null and item.morningPeopleIds != '' " >
  6. MORNING_PEOPLE_IDS=#{item.morningPeopleIds,jdbcType=VARCHAR},
  7. </if>
  8. <if test="item.morningPeopleNames != null and item.morningPeopleNames != '' " >
  9. MORNING_PEOPLE_NAMES=#{item.morningPeopleNames,jdbcType=VARCHAR},
  10. </if>
  11. <if test="item.afternoonPeopleIds != null and item.afternoonPeopleIds != '' " >
  12. AFTERNOON_PEOPLE_IDS=#{item.afternoonPeopleIds,jdbcType=VARCHAR},
  13. </if>
  14. <if test="item.afternoonPeopleNames != null and item.afternoonPeopleNames != '' " >
  15. AFTERNOON_PEOPLE_NAMES=#{item.afternoonPeopleNames,jdbcType=VARCHAR},
  16. </if>
  17. <if test="item.eveningPeopleIds != null and item.eveningPeopleIds != '' " >
  18. EVENING_PEOPLE_IDS=#{item.eveningPeopleIds,jdbcType=VARCHAR},
  19. </if>
  20. <if test="item.eveningPeopleNames != null and item.eveningPeopleNames != '' " >
  21. EVENING_PEOPLE_NAMES=#{item.eveningPeopleNames,jdbcType=VARCHAR},
  22. </if>
  23. <if test="item.leaderIds != null and item.leaderIds != '' " >
  24. LEADER_IDS=#{item.leaderIds,jdbcType=VARCHAR},
  25. </if>
  26. <if test="item.leaderNames != null and item.leaderNames != '' " >
  27. LEADER_NAMES=#{item.leaderNames,jdbcType=VARCHAR},
  28. </if>
  29. </set>
  30. where DUTY_ID=#{item.dutyId,jdbcType=INTEGER}
  31. </foreach>
  32. </update>


3、批量删除:
单个条件:
  1. <delete id="delMultiByIds" parameterType="java.util.List">
  2. delete from TB_CODE_NAME_RESULT_NEW
  3. where ID in
  4. <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
  5. #{item}
  6. </foreach>
  7. </delete>

多个条件:
  1. <delete id="delMultiByIds2" parameterType="java.util.List">
  2. delete from tb_duty A
  3. where exists
  4. (
  5. select 1 from(
  6. <foreach collection="list" item="item" index="index" separator="union all">
  7. select B.* from tb_duty B where 1=1 and B.dscd=${item.dscd} and B.unit_id=${item.unitId} and
  8. B.year=${item.year} and B.month=${item.month} and B.flag=${item.flag}
  9. </foreach>
  10. )S where A.duty_id=S.duty_id
  11. )
  12. </delete>

多个条件第2种形式:
  1. <!-- 成功删除返回的是-1而不是删除的记录数 -->
  2. <delete id="delMultiByIds" >
  3. <foreach collection="list" item="item" index="index" open="begin" close="; end;" separator=";">
  4. delete from tb_duty_statistics a
  5. where a.person_id
  6. in(
  7. select b.person_id from tb_duty_person_info b where b.dscd=#{dscd,jdbcType=CHAR} and b.unit_id=#{unitId,jdbcType=INTEGER}
  8. )
  9. and substr(a.duty_id,1,7)=#{item.dutyId,jdbcType=CHAR}
  10. </foreach>
  11. </delete>

4、批量查询:(未验证)
  1. <select id="selectBySomePoiIds" resultType="list" parameterType="java.util.Map">
  2. SELECT <include refid="Base_Column_List" /> FROM 表名
  3. WHERE poi_id in
  4. <foreach collection="poiIds" item="poiId" index="index" open="(" close=")" separator=",">
  5. #{poiId}
  6. </foreach>
  7. AND pass_uid = #{passUid}
  8. <if test="status != null">
  9. AND status = #{status,jdbcType=BIGINT}
  10. </if>
  11. </select>


5、mapper常用写法
  1. <resultMap id="BaseResultMap" type="com.xyxc.CustomerInfoEntity">
  2. <id column="id" property="id" />
  3. <result column="aa" property="aa" />
  4. <result column="bb" property="bb" />
  5. </resultMap>
  6. <sql id="table_column_no_id">
  7. aa,
  8. bb ,
  9. cc,
  10. dd,
  11. ee
  12. </sql>
  13. <sql id="columns" >
  14. id,<include refid="table_column_no_id"/>
  15. </sql>
  16. <sql id="table_name" >
  17. TableName
  18. </sql>
  19. <insert id="insert" parameterType="com.xyxc.CustomerInfoEntity" useGeneratedKeys="true" keyProperty="installmentId">
  20. <![CDATA[
  21. INSERT INTO T_DDQ_INSTALLMENT_INFO (
  22. aa,
  23. bb ,
  24. cc,
  25. dd,
  26. ee
  27. )
  28. VALUES (
  29. #{sltAccountId},
  30. #{loanPeriodNo},
  31. #{scheduleAmount},
  32. now(),
  33. now()
  34. )
  35. ]]>
  36. <selectKey resultType="Long" keyProperty="installmentId" order="AFTER">
  37. <![CDATA[ SELECT LAST_INSERT_ID() AS installmentId ]]>
  38. </selectKey>
  39. </insert>

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号