0
我有以下表和序列返回主鍵在插入時不工作的MyBatis彈簧
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彈簧映射如下
@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,無論多少次,我打電話
誰能告訴我,爲什麼的原因,給我解決? 在此先感謝。
在'insertTeacher'裏面顯示代碼 –
@wingᴇdpᴀnᴛʜᴇʀinsertTeacher裏面沒有代碼,我使用myBatis-Spring框架,我們只需要在界面(Mapper)中聲明函數和sql一樣。 –