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);
}
Mybatis插入数据返回主键ID的更多相关文章
- MyBatis框架——mybatis插入数据返回主键(mysql、oracle)
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- mybatis插入是返回主键id
<!-- 插入数据:返回记录的id值 --> <insert id="insertOneTest" parameterType="org.chench. ...
- 关于mybatis插入数据库返回主键id
关于Sequence主键的数据库来说,如: <insert id="add" parameterType="vo.Category"> <se ...
- mybatis插入数据返回主键
原来之前一直用错了... keyProperty是表示将返回的主键设置为该方法参数的对应属性中去,而不是用返回值的形式的去获取.
- Mybatis配置插入数据返回主键ID
需要在insert方法中添加 <insert id="insertSelective" parameterType="com.midou.ott.model.MDA ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了
insertSelective---Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了 https://www.cnblogs.com/xi ...
- myBatis获取批量插入数据的主键id
在myBatis中获取刚刚插入的数据的主键id是比较容易的 , 一般来说下面的一句话就可以搞定了 , 网上也有很多相关资料去查. @Options(useGeneratedKeys = true, k ...
随机推荐
- CSS中的变形、过渡、动画效果
一.变形 .过渡效果 1:元素平移 x方向 y方向 transform:translate(100px 100px); 2:过渡动画效果 a:什么属性参与过渡效果 b:过渡时间 c:过渡的效果 值包含 ...
- Netflix性能监控工具Vector
简介: Vector是Netflix开源的主机级性能监控框架,向每位工程师的浏览器提供精心挑选的高分辨率系统和应用程序指标. 登录到系统并从shell运行大量命令是一种选择,但是通常涉及的复杂性可能成 ...
- Cookie SQL注入
转自http://blog.sina.com.cn/s/blog_6b347b2a0101379o.html cookie注入其原理也和平时的注入一样,只不过说我们是将提交的参数已cookie方式提交 ...
- HTTPS 之 TLS 性能调优
HTTPS(HTTP over SSL)是以安全为目标的 HTTP 通道,可以理解为 HTTP + SSL/TLS,即在 HTTP 下加入 SSL/TLS 层作为安全基础.其中 TLS 的前身是 SS ...
- OAuth 2.0 认证的原理与实践
摘要: 使用 OAuth 2.0 认证的的好处是显然易见的.你只需要用同一个账号密码,就能在各个网站进行访问,而免去了在每个网站都进行注册的繁琐过程. 本文将介绍 OAuth 2.0 的原理,并基于 ...
- client,server,nginx 在使用keepAlive 专题
2. TCP keepalive overview In order to understand what TCP keepalive (which we will just call keepali ...
- JAVA实现Base64编码的三种方式
摘要: Javabase64编码的三种方式 有如下三种方式: 方式一:commons-codec.jar Java代码 1. String base64String="whuang12 ...
- dom操作相关,byebye T T
o = { name: 'aa', price: 11, } function add(items) { var bodys = document.getElementsByTagName('tbod ...
- APNs 推送原理及问题
http://bbs.csdn.net/topics/390461996 在 iOS 平台上,大部分应用是不允许在后台运行并连接网络的.在应用没有被运行的时候,只能通过 Apple Push Noti ...
- django(权限、认证)系统——第三方组件实现Object级别权限控制
在我的系列blog<Django中内置的权限控制>中明确提及到,Django默认并没有提供对Object级别的权限控制,而只是在架构上留了口子.在这篇blog中,我们探讨一个简单流行的Dj ...