mybatis 自动增长id保存后得到id

来源:互联网 发布:淘宝上钱包店铺哪家好 编辑:程序博客网 时间:2024/06/05 06:21

1.在mysql+mybatis中实现自动增长id后得到id,代码如下:


<insert id="saveLog" parameterType="com.test.entity.SignLog" useGeneratedKeys="true" keyProperty="id">INSERT INTO db_log(plaintext,certdn) VALUES (#{plaintext,jdbcType=TEXT},#{certdn})</insert>

如上图所示,1.需要在mysql数据库db_log表中将id设置为自动增长列,2.useGenerateKyes=true用于启用自动增长策略3.keyProperty=id,此处的id为java实体对象中的id属性,该操作可将数据库生成的id赋值给java实体类中的id,保存后可通过getId()方法来获取。

2.在oracle+mybatis中实现自动增长后得到id,代码如下:


<insert id="saveLog" parameterType="com.test.entity.SignLog">INSERT INTO db_sign_log(id,plaintext,certdn) VALUES (sq_sign_log_id.NEXTVAL,#{plaintext,jdbcType=BLOB},#{certdn})<selectKey resultType="java.lang.Integer" keyProperty="id"  order="AFTER">        SELECT sq_sign_log_id.CURRVAL AS id FROM DUAL        </selectKey></insert>

如上图所示,1.需要在oracle中为该表的id字段创建序列sq_sign_log_id,2.添加selectKey已经相关的配置,keyProperty属性与mysql一致,order选项可以设置为bofore或after,一般为after,即保存后才给id赋值,并将id映射到java实体中,同样通过getId()方法来获取。



1 0
原创粉丝点击