mybatis 自动增长id保存后得到id
mybatis 自动增长id保存后得到id
·
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()方法来获取。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)