mybatis批量添加修改删除

使用xml配置记得把mapper.xml放入mybatis_mapper.xml

dao层调用方法时,记的别传空List!!!
即使主键是自动递增的也要加上去!!!


    <sql id='addColumn'>
        ID,
    	ATTENDANCE_DATE,
        CARD_NUMBER,
        PERSON_CODE,
        PERSON_NAME,
        DEPT_NAME,
        FIRST_ATTENDANCE_TIME,
        FIRST_ATTENDANCE_TEMP,
        LAST_ATTENDANCE_TIME,
        LAST_ATTENDANCE_TEMP
    </sql>

    <sql id='addValue'>
        #{id,jdbcType=NUMERIC}
    	#{attendanceDate,jdbcType=DATE},
        #{cardNumber,jdbcType=VARCHAR},
        #{personCode,jdbcType=VARCHAR},
        #{personName,jdbcType=VARCHAR},
        #{deptName,jdbcType=VARCHAR},
        #{firstAttendanceTime,jdbcType=VARCHAR},
        #{firstAttendanceTemp,jdbcType=VARCHAR},
        #{lastAttendanceTime,jdbcType=VARCHAR},
        #{lastAttendanceTemp,jdbcType=VARCHAR}
    </sql>

    <sql id='batchAddValue'>
        #{item.id,jdbcType=NUMERIC},
    	#{item.attendanceDate,jdbcType=DATE},
        #{item.cardNumber,jdbcType=VARCHAR},
        #{item.personCode,jdbcType=VARCHAR},
        #{item.personName,jdbcType=VARCHAR},
        #{item.deptName,jdbcType=VARCHAR},
        #{item.firstAttendanceTime,jdbcType=VARCHAR},
        #{item.firstAttendanceTemp,jdbcType=VARCHAR},
        #{item.lastAttendanceTime,jdbcType=VARCHAR},
        #{item.lastAttendanceTemp,jdbcType=VARCHAR}
    </sql>

批量插入

    <insert id="batchAddTimeAttendance" parameterType="TimeAttendance">
        INSERT INTO time_attendance
         (<include refid="addColumn"/>)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
             (
            <include refid="batchAddValue"/>
            )
        </foreach>
    </insert>

批量修改

    <update id="batchUpdateTimeAttendance" parameterType="TimeAttendance">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE time_attendance
            <set>
                <if test="item.attendanceDate != null">
                    ATTENDANCE_DATE = #{item.attendanceDate},
                </if>
                <if test="item.cardNumber != null">
                    CARD_NUMBER = #{item.cardNumber},
                </if>
                <if test="item.personName != null">
                    PERSON_NAME = #{item.personName},
                </if>
                <if test="item.deptName != null">
                    DEPT_NAME = #{item.deptName},
                </if>
                <if test="item.firstAttendanceTime != null">
                    FIRST_ATTENDANCE_TIME = #{item.firstAttendanceTime},
                </if>
                <if test="item.firstAttendanceTemp != null">
                    FIRST_ATTENDANCE_TEMP = #{item.firstAttendanceTemp},
                </if>
                <if test="item.lastAttendanceTime != null">
                    LAST_ATTENDANCE_TIME = #{item.lastAttendanceTime},
                </if>
                <if test="item.lastAttendanceTemp != null">
                    LAST_ATTENDANCE_TEMP = #{item.lastAttendanceTemp}
                </if>
            </set>
            WHERE PERSON_CODE = #{item.personCode}
        </foreach>
    </update>

批量删除

<delete id="" parameterType="Map">
DELETE FROM 表名
<where>
AND ID IN
<foreach item="item" index="index" collection="ids" open="("  separator="," close=")">
#{item}
</foreach>
</where>
</delete>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值