Mybatis+Postgresql实现按主键自增方式插入数据

1.创建Seq(序列)

CREATE SEQUENCE resource_warn_decision_seq_gid
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1 
  CYCLE;

为某个表中的主键启用该序列

alter table resource_warn_decision alter column gid set default nextval('resource_warn_decision_seq_gid'); 

删除已创建的序列

DROP SEQUENCE resource_warn_decision_seq_gid;

2.配置mapper文件实现主键自增插入

 <insert id="insert" parameterType="java.util.List">
        INSERT INTO resource_warn_decision (gid,resource_id,name,path,format,size,last_update_time)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (nextval('resource_warn_decision_seq_gid'),#{item.resourceId},#{item.name},#{item.path},#{item.format},#{item.size},#{item.lastUpdateTime})
        </foreach>

    </insert>

你可能感兴趣的:(sql,Java,mybatis)