Postgresql 从MyBatis映射方法返回值
我有一个Java项目,它使用MyBatis访问PostgreSQL数据库。PostgreSQL允许在INSERT语句之后返回新创建行的字段,我想使用它返回新创建记录的自动生成的BIGSERIAL id。因此,我将XML中的insert命令更改为使用PostgreSQL的特性,将resultType=long属性添加到标记中,并在映射器的Java接口中将insert方法设置为返回long而不是void 当我试着运行这个程序时,我得到一个org.xml.sax.SAXParseException,表示必须为元素类型insert声明属性resultType 现在,当我将标记更改为everything时,一切都可以正常工作,但使用标记执行INSERT语句让我感到困扰Postgresql 从MyBatis映射方法返回值,postgresql,mybatis,Postgresql,Mybatis,我有一个Java项目,它使用MyBatis访问PostgreSQL数据库。PostgreSQL允许在INSERT语句之后返回新创建行的字段,我想使用它返回新创建记录的自动生成的BIGSERIAL id。因此,我将XML中的insert命令更改为使用PostgreSQL的特性,将resultType=long属性添加到标记中,并在映射器的Java接口中将insert方法设置为返回long而不是void 当我试着运行这个程序时,我得到一个org.xml.sax.SAXParseException,表
有没有办法使映射到标记的方法返回结果,或者MyBatis不是为此而设计的,我应该将它们保留为标记?映射插入方法的返回类型可以是void或int,在这种情况下,它将返回插入行的编号。可以通过以下机制返回生成的id:
<insert id="insert" parameterClass="MyParameter">
<selectKey order="AFTER" keyProperty="id" resultType="long">
SELECT currval('my_seq')
</selectKey>
INSERT INTO mytable(col1, col2) VALUES (#{val1}, #{val2})
</insert>
这将把生成的id列设置为参数类的id属性。之后,作为参数传递的对象将在其属性中生成id集。您可以按如下方式使用。在xml中
<insert id="insertNewUser" parameterType="User">
<selectKey keyProperty="userId" resultType="Integer" order="BEFORE">
select NEXTVAL('base.user_id_seq')
</selectKey>
INSERT INTO base.user(
user_id, user_name)
VALUES (#{userId}, #{userName});
</insert>
在调用要插入的方法的Java类中,可以通过调用user.getUserId来获取值
基本上,下一个val存储在对象的变量中。这里是用户内部的userId。您还可以使用生成的密钥:
<insert id="create" parameterType="Skupina" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO ODBOR
(NAZEV, POPIS, ZKRATKA, WEBROLE, JEODBOR, AKTIVNI)
VALUES
(#{nazev}, #{popis}, #{webrole}, #{webrole}, false, #{aktivni})
</insert>
插入后,参数的属性id设置为列id中的值。我知道至少有两种方法可以获取一条插入记录的id: 例如,我们有一个类EntityDao: 1.使用插入标记并返回对象的实例 MyBatis界面
public interface EntityDaoMapper {
EntityDao insert(EntityDao entity);
}
public interface EntityDaoMapper {
Long insert(EntityDao entity);
}
MyBatis XML映射器:
<insert id="insert" parameterType="com.package.EntityDao" useGeneratedKeys="true" keyColumn="entity_id" keyProperty="id">
INSERT INTO some_table (name, type, other_fields, etc)
VALUES (#{name}, #{type}, #{other_fields}, #{etc})
</insert>
<select id="insert" parameterType="com.package.EntityDao" resultType="long">
INSERT INTO some_table (name, type, other_fields, etc)
VALUES (#{name}, #{type}, #{other_fields}, #{etc})
RETURNING entity_id <-- id only or many fields
</select>
2.使用select和resultType标记仅返回记录的ID
MyBatis界面
public interface EntityDaoMapper {
EntityDao insert(EntityDao entity);
}
public interface EntityDaoMapper {
Long insert(EntityDao entity);
}
MyBatis XML映射器:
<insert id="insert" parameterType="com.package.EntityDao" useGeneratedKeys="true" keyColumn="entity_id" keyProperty="id">
INSERT INTO some_table (name, type, other_fields, etc)
VALUES (#{name}, #{type}, #{other_fields}, #{etc})
</insert>
<select id="insert" parameterType="com.package.EntityDao" resultType="long">
INSERT INTO some_table (name, type, other_fields, etc)
VALUES (#{name}, #{type}, #{other_fields}, #{etc})
RETURNING entity_id <-- id only or many fields
</select>
在本例中,使用选项
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface POJOCustomMapper {
@Options(useGeneratedKeys = true, keyProperty = "ID", keyColumn = "ID")
@Insert({
"insert into TABLE_A ( NAME "
"values (#{NAME,jdbcType=VARCHAR})"
})
long insert(POJO record);
我懂了。有没有任何方法可以将其用于PostgreSQL的返回语法?您可以尝试添加useGeneratedKeys=true keyColumn=id keyProperty=id。我从未使用过此方法,但它应该可以工作。不需要在查询中使用returning,让这个任务完成JDBC驱动程序和MyBatis。请参阅以获取更多帮助。好的,我查看了,它看起来基本上做了相同的事情-运行第二个查询来检索密钥。此外,它没有为PostgreSQL定义,这是有意义的,因为在PostgreSQL中,您可以从插入行的同一查询中获取密钥。基本上,当您要执行带有返回的查询时,获取该值的唯一方法是从ResultSet,这意味着您不能使用MyBatis中使用的executeUpdate。我明白了,所以不能用它来执行。有道理。谢谢不为我工作。我必须添加返回id并使用@Select而不使用@Options。