我有下表和序列
CREATE TABLE teacher
(
teacher_id serial NOT NULL,
teacher_name character varying(50),
CONSTRAINT teacher_pkey PRIMARY KEY (teacher_id)
)
CREATE SEQUENCE teacher_teacher_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
并且想在我插入新记录时获取主键。当我运行以下查询时,它会起作用并返回我想要的主键。
INSERT INTO teacher( teacher_name ) VALUES ('John') RETURNING teacher_id;
但是当我使用 myBatis-Spring Mapper 如下
@Insert("INSERT INTO teacher( teacher_name ) " +
"VALUES ( #{teacherName} ) RETURNING teacher_id" )
public int insertTeacher(Teacher teacher);
并使用有效teacher
对象调用函数,它插入了值但返回-1。
我也尝试了以下
@Insert("INSERT INTO teacher( teacher_id, teacher_name ) " +
"VALUES ( #{teacherId}, #{teacherName} )" )
@SelectKey(statement="SELECT nextval('teacher_teacher_id_seq');", keyProperty="teacherId", before=true, resultType=int.class)
public int insertTeacher(Teacher teacher);
无论我调用多少次,此返回 1
谁能告诉我原因并给我解决方案?提前致谢。