如何使用identify as sequence在PostgreSQL 13中获取MyBatis插入主键值
我现在正在使用PostgreSQL 13,在使用MyBatis插入记录之后,我需要获取插入记录的主键。按照旧方法,我使用的是串行,配置如下:如何使用identify as sequence在PostgreSQL 13中获取MyBatis插入主键值,postgresql,mybatis,mybatis-generator,Postgresql,Mybatis,Mybatis Generator,我现在正在使用PostgreSQL 13,在使用MyBatis插入记录之后,我需要获取插入记录的主键。按照旧方法,我使用的是串行,配置如下: <table tableName="article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample=&qu
<table tableName="article"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="SELECT currval('article_id_seq')" identity="true" />
</table>
ALTER TABLE rss_sub_source
ALTER id SET NOT NULL, -- optional
ALTER id ADD GENERATED ALWAYS AS IDENTITY
(START WITH 1233);
而且该表不包含序列,我现在应该如何获取主键?最好的方法是使用本机JDBC支持生成的键。JDBC驱动程序将检索新值。您可以在MyBatis Generator中进行如下设置:
<table tableName="article"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="SELECT currval('article_id_seq')" identity="true" />
</table>
ALTER TABLE rss_sub_source
ALTER id SET NOT NULL, -- optional
ALTER id ADD GENERATED ALWAYS AS IDENTITY
(START WITH 1233);