I have a Java project that uses MyBatis to access a PostgreSQL database. PostgreSQL allows to return fields of a newly created row after an INSERT
statement, an
There are two ways (at least that I know) to get the ID of the one inserted record:
For example, we have a class EntityDao
:
public class EntityDao {
private Long id;
private String name;
// other fields, getters and setters
}
insert
tag and returning an instance of objectMyBatis interface
public interface EntityDaoMapper {
EntityDao insert(EntityDao entity);
}
MyBatis XML mapper:
<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>
Sample code:
EntityDao saved = entityDaoMapper.insert(entityToSave);
System.out.println(saved.getId());
select
and resultType
tags to return the ID of the record onlyMyBatis interface
public interface EntityDaoMapper {
Long insert(EntityDao entity);
}
MyBatis XML mapper:
<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>
Sample code:
Long id = entityDaoMapper.insert(entityToSave);
System.out.println(id);
You can use as follows. In 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>
In Java class from where you have called the method to insert, you can get the value by calling user.getUserId()
.
Basically the next val is stored inside the variable of the object. Here userId inside User.
The return type of mapped insert method can be void
or int
(in which case it will return the number of the inserted row). You can do the following mechanism to return the generated 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>
This will set generated id
column to id
property of your parameter class. After that, object you passed as parameter will have generated id
set in its property.
You can also use generated keys:
<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>
After insert, parameter has property id set to value from column id.
In this example using options
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);