postgresql + mybatis insert主键自增方法


postgresql + mybatis插入记录时设置自增主键方法:

一、数据库设置主键自增

1.数据库中id字段选择serial4类型后,会在默认值中生成 nextval('app_id_seq'::regclass),即从序列中取下一个值

2.在AppDO类中包含字段:id,app_id,app_name

3.在mapper.xml中设置insert语句:

<insert id="insert" parameterType="appdo">
    insert into app
    (app_id,app_name,create_time,modify_time)
    values
    ( #{appId}, #{appName}, now(), now() )
</insert>

也可以像下面这样:

<insert id="insert" parameterType="appdo" >
      <selectKey keyProperty="id" resultType="int" order="BEFORE">
        SELECT nextval('app_id_seq'::regclass) as id  
    </selectKey>
    insert into app (id, app_id,app_name,create_time,modify_time)
    values (#{id},#{app_id},#{app_name},now(),now())
  </insert>

都能实现主键自增。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM