搬砖中的小事之代码(四)--useGeneratedKeys的理解与使用

调试centerBank接口,业务实现类里面想要通过获取表格中主键id,来进行逻辑判断,从而进行更新;
直接撸代码–mapper.xml中的插入方法:

<insert id="insertSelective" parameterType="com.baidu.example.entity.CenterBankRecordsEntity" useGeneratedKeys="true" keyProperty="id">
        insert into center_bank_records
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                id,
            </if>
            <if test="startDate != null" >
                start_date,
            </if>
            <if test="endDate != null" >
                end_date,
            </if>
            <if test="orderId != null" >
                order_id,
            </if>
            <if test="returnCode != null" >
                return_code,
            </if>
            <if test="returnMessage != null" >
                return_message,
            </if>
            <if test="createTime != null" >
                create_time,
            </if>
            <if test="lastModifyTime != null" >
                last_modify_time,
            </if>
            <if test="status != null" >
                status,
            </if>
            <if test="mac != null" >
                mac,
            </if>
            <if test="userId != null" >
                user_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                #{id,jdbcType=BIGINT},
            </if>
            <if test="orderId!= null" >
                #{orderId,jdbcType=VARCHAR},
            </if>
            <if test="startDate != null" >
                #{startDate,jdbcType=BIGINT},
            </if>
            <if test="endDate != null" >
                #{endDate,jdbcType=BIGINT},
            </if>
            <if test="returnCode != null" >
                #{returnCode,jdbcType=VARCHAR},
            </if>
            <if test="returnMessage != null" >
                #{returnMessage,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                #{createTime,jdbcType=BIGINT},
            </if>
            <if test="lastModifyTime != null" >
                #{lastModifyTime,jdbcType=BIGINT},
            </if>
            <if test="status != null" >
                #{status,jdbcType=VARCHAR},
            </if>
            <if test="mac != null" >
                #{mac,jdbcType=VARCHAR},
            </if>
            <if test="userId != null" >
                #{userId,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

然后想通过service实现类里面的判断id是否存在,此时的id的存在与否,就在于我们在上方代码中是否设置useGeneratedKeys的值为true,
实现类中的代码(entity也是CenterBankRecordsEntity定义的对象,只是之前赋值过部分变量):

CenterBankRecordsEntity updateEntity=new CenterBankRecordsEntity();
        if( entity.getId() != null) {
            updateEntity.setId(entity.getId());
            if (Constants.SUCCESS.equals(resultInfo.getRspCode())) {
                updateEntity.setStatus(StatusEnum.SUCCESS.toString());
            } else {
                updateEntity.setStatus(StatusEnum.FAILED.toString());
            }
            updateEntity.setReturnCode(result.getResult_code());
            updateEntity.setReturnMessage(result.getResult_msg());
            updateEntity.setLastModifyTime(DateUtil.getCurrentTime());
            centerBankRecordsDao.updateByPrimaryKeySelective(updateEntity);
        }
        return resultInfo;
    }

综述:
useGeneratedKeys 取值是boolean 类型,要么是true,要么是false; 默认值是:false。
它的含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的model属性中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值