Mybatis插入数据返回主键ID
程序员文章站
2022-07-11 10:40:29
<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);
}
上一篇: apollo入门demo实战(二)
下一篇: eureka服务搭建