Returning values from MyBatis mapped methods

前端 未结 5 791
无人及你
无人及你 2020-12-28 08:33

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

相关标签:
5条回答
  • 2020-12-28 08:54

    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
    }
    

    1. Using the insert tag and returning an instance of object

    MyBatis 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());
    

    2. Using select and resultType tags to return the ID of the record only

    MyBatis 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);
    
    0 讨论(0)
  • 2020-12-28 09:09

    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.

    0 讨论(0)
  • 2020-12-28 09:10

    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.

    0 讨论(0)
  • 2020-12-28 09:11

    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.

    0 讨论(0)
  • 2020-12-28 09:14

    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);
    
    0 讨论(0)
提交回复
热议问题