Mybatis插入数据返回主键ID

<insert id="add" parameterType="com.dsa.core.base.model.ProductSync">
        insert into tm_sync_product(
            <if test="productId!=null">product_id,</if>
            <if test="mainLimit!=null">main_limit,</if>
            <if test="productName!=null">product_name,</if>
            <if test="productBrand!=null">product_brand,</if>
            <if test="profitType!=null">profit_type,</if>
            <if test="deadLine!=null">dead_line,</if>
            <if test="productStartDay!=null">product_start_day,</if>
            <if test="productEndDay!=null">product_end_day,</if>
            <if test="buyStartDay!=null">buy_start_day,</if>
            <if test="buyEndDay!=null">buy_end_day,</if>
            <if test="riskRank!=null">risk_rank,</if>
            <if test="redeemFlg!=null">redeem_flg,</if>
            <if test="productStatus!=null">product_status,</if>
            <if test="startAmount!=null">start_amount,</if>
            <if test="addAmount!=null">add_amount,</if>
            <if test="yearRate!=null">year_rate,</if>
            <if test="productNote!=null">product_note,</if>
            <if test="otherBankBuy!=null">other_bank_buy,</if>
            <if test="otherBankAmt!=null">other_bank_amt,</if>
            <if test="rateVal!=null">rate_val,</if>
            <if test="recordFlg!=null">record_flg</if>
        ) values (
            <if test="productId!=null">#{productId},</if>
            <if test="mainLimit!=null">#{mainLimit},</if>
            <if test="productName!=null">#{productName},</if>
            <if test="productBrand!=null">#{productBrand},</if>
            <if test="profitType!=null">#{profitType},</if>
            <if test="deadLine!=null">#{deadLine},</if>
            <if test="productStartDay!=null">#{productStartDay},</if>
            <if test="productEndDay!=null">#{productEndDay},</if>
            <if test="buyStartDay!=null">#{buyStartDay},</if>
            <if test="buyEndDay!=null">#{buyEndDay},</if>
            <if test="riskRank!=null">#{riskRank},</if>
            <if test="redeemFlg!=null">#{redeemFlg},</if>
            <if test="productStatus!=null">#{productStatus},</if>
            <if test="startAmount!=null">#{startAmount},</if>
            <if test="addAmount!=null">#{addAmount},</if>
            <if test="yearRate!=null">#{yearRate},</if>
            <if test="productNote!=null">#{productNote},</if>
            <if test="otherBankBuy!=null">#{otherBankBuy},</if>
            <if test="otherBankAmt!=null">#{otherBankAmt},</if>
            <if test="rateVal!=null">#{rateVal},</if>
            <if test="recordFlg!=null">#{recordFlg}</if>
        )
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
             select last_insert_id() as id
        </selectKey>
    </insert>
注意:(此处为mysql)这里的selectKey的resultType 必须和实体对象中的id类型一致,否则报错。

oracle的如下:

<selectKey resultType="java.lang.Long" keyProperty="ID" order="AFTER">
SELECT IBOKEE_COMM_TAG_LIBRARY_SEQ.nextval AS Id FROM DUAL
</selectKey>



实体类
public class ProductSync implements Serializable {
         private Long id;
         ...
         ...

}

dao Mapper
public interface ProductSyncMapper {
   
    /**
     * @param product Object
     * 新增纪录
     */
    public void add(ProductSync product);
   
   
}

posted @   JimmyShan  阅读(3275)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示