我的表插件只有两列:ID和DTYPE。我想在DTYPE列中存储一个String。 ID应自动递增并由insert方法返回。为此,我手动创建了“id_sequence”,如下所示:
CREATE SEQUENCE id_sequence
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
OWNED BY plugin.id;
这是我的mapper.xml文件:
<insert id="insert" useGeneratedKeys="true" parameterType="String">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
SELECT CAST(nextval('id_sequence') as INTEGER)
</selectKey>
INSERT INTO plugins (ID, DTYPE)
VALUES ( #{id}, #{plugin})
RETURNING ID;
</insert>
和相应的mapper.java:
int insert(String plugin);
问题陈述:1)我无法获得正确的ID值(它为空),2)我收到错误
SQL: INSERT INTO public.ucpoplugin (ID, DTYPE) VALUES ( ?, CORRECT_STRING)
Cause: org.postgresql.util.PSQLException: FEHLER: Column »id« is of type bigint, but the sentence is has Type character varying.
答案 0 :(得分:0)
我建议在该id列上使用自动增量,然后不要只传递字符串作为参数,而是传递整个对象。插入后,该属性将被填充。
请参阅:http://edwin.baculsoft.com/2010/12/beginning-mybatis-3-part-3-how-to-get-tables-generated-ids/