MySQL insert returns primary key

1. Automatically generated by mybatis generator

Insert into the generator configuration file (generatorConfig.xml) <generatedKey column="id" sqlStatement="MySql" identity="true"/>, use the generated insert method, the primary key value is wrapped in the parameter object, and obtained through user.getId()
such as :

<table tableName="t_approval_process" domainObjectName="ApprovalProcess" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

2. Manually add

1. The insert is modified as follows, and the primary key value is wrapped in the parameter object.

user.getId() get

<insert id="insert" parameterType="user" keyProperty="id" useGeneratedKeys="true">

2. Insert the selectKey tag to get the same as above

<insert id="insert" parameterType="com.mmall.pojo.ApprovalProcess" >
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into a (name, type)
    values(#{name}, #{type})
  </insert>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326753742&siteId=291194637