MyBatis,如何获取插入的自动生成密钥?[MySQL的]

use*_*526 31 java mysql insert mybatis

如何使用MyBatis获取插入的生成密钥?我读了很多关于这个问题的网页,但我仍然被封锁,有人可以帮帮我吗?这是我的代码:

桌子:

ID_ERROR long primary key
DATE timestamp
TYPE varchar
MESSAGE varchar
SOURCE varchar
Run Code Online (Sandbox Code Playgroud)

道:

Long returnedId = 0L;
MyMapper myMapper = this.sqlSession.getMapper(MyMapper.class);
myMapper.insertRecord(returnedId, Utils.now(), t.getClass().getName(), t.getMessage(), c.getName());
return returnedId;
Run Code Online (Sandbox Code Playgroud)

mapper.java:

public void insertRecord(@Param("returnedId") Long returnedId, @Param("timestamp")Timestamp timestamp,@Param("type") String type,@Param("message") String message,@Param("source") String source);
Run Code Online (Sandbox Code Playgroud)

mapper.xml

 <insert id="insertRecord" parameterType="map" useGeneratedKeys="true"  keyProperty="ID_ERROR">
    INSERT INTO errors (
        DATE,
        TYPE,
        MESSAGE,
        SOURCE
    )
    VALUES (
        #{timestamp},
        #{type},
        #{message},
        #{source}
    )
    <selectKey resultType="long" order="AFTER" keyProperty="returnedId">
        SELECT LAST_INSERT_ID() as returnedId
    </selectKey>
</insert>
Run Code Online (Sandbox Code Playgroud)

怎么了?如何获取此插入的生成密钥?谢谢!

T M*_*T M 50

对我来说它是这样工作的(mybatis 3.x)..必须在mysql表中设置id自动增量

<insert id="createEmpty" parameterType="Project" useGeneratedKeys="true" keyProperty="project.projectId" keyColumn="PROJECT_ID">
    INSERT INTO PROJECT (TITLE,DESCRIPTION)
    VALUES
    (#{title},#{description})
</insert>
Run Code Online (Sandbox Code Playgroud)

注意 keyProperty="project.projectId"useGeneratedKeys="true"

我的界面是:

public int createEmpty(@Param("project") Project project, @Param("title") String title,
    @Param("description") String description);
Run Code Online (Sandbox Code Playgroud)

最后获取值(将自动分配给pojo的id属性)我使用:

projectRepository.createEmpty(p, "one", "two");
System.err.print(p.getProjectId() + "\n");
Run Code Online (Sandbox Code Playgroud)


bha*_*ran 13

你可以通过两种方式实现这一目标,

  1. 通过使用 useGeneratedKeys="true", keyProperty="id", keyColumn="id"

    keyProperty引用POJO变量名称并keyColumn引用数据库中生成的列名称

  2. 通过使用<selectKey/>内部插入标记


Md.*_*lam 5

简易方案:

使用KeyProperty属性如objectName.AutoincrementId 下面的...

useGeneratedKeys="true", KeyProperty="person.id", KeyColumn="id"


Aud*_*val 5

如果您查看MyBatis 文档useGeneratedKeyskeyProperty是您获取自动增量数据至少需要的东西(对于某些数据库,您需要添加keyColumn)。

如您所见, useGeneratedKeys 取决于数据库的 JDBC 的 getGeneretadKeys 方法是否/如何实现。

例如,对于mysql 或 H2,getGeneretadKeys 仅支持一列。最后生成的键将是 getGeneretadKeys 返回的键。

总之,在您的情况下,您只需要添加 useGeneratedKeys 和 keyProperty (带有 ID_ERROR auto_increment):

映射器.xml

<resultMap type='pathToJavaClass/Error' id='error'>
    <id property='id' column='ID_ERROR' />
    <result property='timestamp' column='DATE' />
    <result property='type' column='TYPE'/>
    <result property='message' column='MESSAGE'/>
    <result property='source' column='SOURCE'/>
</resultMap>
<insert id="insertRecord" parameterType="error" useGeneratedKeys="true" keyProperty="id">
INSERT INTO errors (
    DATE,
    TYPE,
    MESSAGE,
    SOURCE
)
VALUES (
    #{timestamp},
    #{type},
    #{message},
    #{source}
)
</insert>
Run Code Online (Sandbox Code Playgroud)

接口.java

public void insertRecord(@Param("error") Error error);
Run Code Online (Sandbox Code Playgroud)

如果您仍然在检索生成的密钥时遇到问题,请查看 mysql 的 JDBC 文档(旧版本可能没有实现 getGeneretadKeys)。


Lar*_*y.Z -5

Map如果你想获取生成的主键,你应该通过或传递参数POJO Object

public void insertRecord(Map<String, Object> map);
Run Code Online (Sandbox Code Playgroud)

当调用映射方法时,将值放入映射中。

Map<String, Object> map = new HashMap<String, Object>();
map.put("returnedId", 0);
map.put("message", message);
// other paramters
mapper.insertRecord(map);
return map.get("returnedId");
Run Code Online (Sandbox Code Playgroud)