mybatisPlus的一些用法(一)trim、foreach、if、case、when、then

1、在项目开发中遇到一些新的知识点、在这里做以总结。

  (1)

    <trim></trim>标签、

<foreach></foreach>标签
 <update id="updateBatch" parameterType="java.util.Map">
        update salary_insurance_benefit_person
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="remarks=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.remarks != null  and cus.remarks != ''">
                        when id=#{cus.id} then #{cus.remarks}
                    </if>
                </foreach>
            </trim>
            <trim prefix="enabled=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.enabled != null ">
                        when id=#{cus.id} then #{cus.enabled}
                    </if>
                </foreach>
            </trim>

            <trim prefix="tenant_code=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.tenantCode != null and cus.tenantCode != '' ">
                        when id=#{cus.id} then #{cus.tenantCode}
                    </if>
                </foreach>
            </trim>
            <trim prefix="salary_common_type_id=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.salaryCommonTypeId != null ">
                        when id=#{cus.id} then #{cus.salaryCommonTypeId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="salary_accounting_person_id=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.salaryAccountingPersonId != null ">
                        when id=#{cus.id} then #{cus.salaryAccountingPersonId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="seniority=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.seniority != null ">
                        when id=#{cus.id} then #{cus.seniority}
                    </if>
                </foreach>
            </trim>
            <trim prefix="attendance_months=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.attendanceMonths != null ">
                        when id=#{cus.id} then #{cus.attendanceMonths}
                    </if>
                </foreach>
            </trim>
            <trim prefix="endowment_insurance=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.endowmentInsurance != null ">
                        when id=#{cus.id} then #{cus.endowmentInsurance}
                    </if>
                </foreach>
            </trim>
            <trim prefix="medical_insurance=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.medicalInsurance != null ">
                        when id=#{cus.id} then #{cus.medicalInsurance}
                    </if>
                </foreach>
            </trim>
            <trim prefix="unemployment_insurance=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.unemploymentInsurance != null ">
                        when id=#{cus.id} then #{cus.unemploymentInsurance}
                    </if>
                </foreach>
            </trim>
            <trim prefix="physical_welfare=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.physicalWelfare != null ">
                        when id=#{cus.id} then #{cus.physicalWelfare}
                    </if>
                </foreach>
            </trim>
            <trim prefix="spring_welfare=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.springWelfare != null ">
                        when id=#{cus.id} then #{cus.springWelfare}
                    </if>
                </foreach>
            </trim>
            <trim prefix="other_welfare=case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.otherWelfare != null ">
                        when id=#{cus.id} then #{cus.otherWelfare}
                    </if>
                </foreach>
            </trim>
            <trim prefix="last_modified_by =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.lastModifiedBy != null">
                        when id=#{cus.id} then #{cus.lastModifiedBy}
                    </if>
                </foreach>
            </trim>
            <trim prefix="last_modified_date =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.lastModifiedDate != null ">
                        when id=#{cus.id} then #{cus.lastModifiedDate}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="cus">
                id = #{cus.id}
            </foreach>
        </where>
    </update>

2<foreach></foreach>标签

 <foreach collection="list" index="index" item="item" separator="," >
            (
            #{item.organizationId}
          )
</foreach>

<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>

3.在做修改的时候、修改版本(version)字段的。

<update id="updateById">
        UPDATE salary_accounting_person
        SET organization_id=#{et.organizationId},
            salary_common_type_id=#{et.salaryCommonTypeId},
            salary_program_id=#{et.salaryProgramId},
            employee_id=#{et.employeeId},
            salary_year=#{et.salaryYear},
            salary_month=#{et.salaryMonth},
            accounting=#{et.accounting},
            stop_salary_date=#{et.stopSalaryDate},
            overtime_days=#{et.overtimeDays},
            absenteeism_days=#{et.absenteeismDays},
            dimission_absenteeism_days=#{et.dimissionAbsenteeismDays},
            entry_absenteeism_days=#{et.entryAbsenteeismDays},
            leaved=#{et.leaved},
            editable=#{et.editable},
            employee_no=#{et.employeeNo},
            employee_name=#{et.employeeName},
            office_id=#{et.officeId},
            position_id=#{et.positionId},
            job_id=#{et.jobId},
            department_id=#{et.departmentId},
            entry_date=#{et.entryDate},
            regular_date=#{et.regularDate},
            leave_date=#{et.leaveDate},
            mobile_phone=#{et.mobilePhone},
            idcard=#{et.idcard},
            position_name=#{et.positionName},
            created_by=#{et.createdBy},
            last_modified_by=#{et.lastModifiedBy},
            created_date=#{et.createdDate},
            last_modified_date=#{et.lastModifiedDate},
            remarks=#{et.remarks},
            version=#{et.version} + 1,   
            enabled=#{et.enabled},
            tenant_code=#{et.tenantCode}
            WHERE id=#{et.id}
    </update>

以上代码主要是做一个记录、以备后期学习使用

猜你喜欢

转载自www.cnblogs.com/mzlb520/p/12082234.html