我有以下表格和序列
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
谁能告诉我原因并给我解决方案? 提前谢谢。