Java 获取mybatis中上次插入记录的id
我是mybatis的新手。我正在尝试获取上次插入记录的id。我的数据库是mysql,映射器xml是Java 获取mybatis中上次插入记录的id,java,mysql,ibatis,mybatis,Java,Mysql,Ibatis,Mybatis,我是mybatis的新手。我正在尝试获取上次插入记录的id。我的数据库是mysql,映射器xml是 <insert id="insertSelective" parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" > <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" > SELECT LAS
<insert id="insertSelective" parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" >
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID() as id
</selectKey>
insert into fileAttachment
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="name != null" >
name,
</if>
<if test="attachmentFileSize != null" >
size,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="attachmentFileSize != null" >
#{attachmentFileSize,jdbcType=INTEGER},
</if>
</trim>
</insert>
在我的dao类中,我正在使用
int id=fileAttachmentMapper.insertSelectivefileAttachment
插入新记录时,我得到的Id值始终为1。我的Id字段是自动递增的,记录插入正确。我认为返回的1是指更新/插入的记录数。我认为id是在传递给insertSelective调用的fileAttachment参数上设置的。id被注入到对象中:
int num_of_record_inserted = fileAttachmentMapper.insertSelective(fileAttachment);
int id = fileAttachment.getId();
selectKey所做的是在插入的对象中设置id,在本例中是在其属性id的fileAttachment中以及插入记录后设置id。您只需要使用
<insert id="insert" parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
之所以使用fileAttachment.getId,是因为在insert tag keyColumn=id中是define,您将在fileAttachment的fileAttachment引用中获得所有返回值。我希望在编写器中,您可以有一个自定义复合编写器,在那里您可以获得插入的id。1添加到Ruju的答案中,在insert语句中,您需要定义属性useGeneratedKeys=true、parameterType=object、keyProperty=objectId和keyColumn=objectId,并使用存储对象记录的表中相同的主键列名objectId进行设置。数据库表中的主键列objectId应设置为自动增量
2当insert语句被触发时,新生成的主键objectId将存储在object中,u可以通过使用object.getObjectId或object.objectId方法访问objectId属性来检索它。现在,这应该给出准确的和新生成的主。它对我有用 事实上,MyBatis已经提供了此功能。您可以使用选项:useGeneratedKeys获取最后一个插入id 这是MyBatis的解释。如果您想了解更多详细信息,可以访问MyBatis官方页面 useGeneratedKeys insert and update only这告诉MyBatis使用JDBC getGeneratedKeys方法检索数据库内部生成的密钥,例如MySQL或SQL Server等RDBMS中的自动增量字段。默认值:false 如果您使用的是xml:
<insert id="" parameterType="" useGeneratedKeys="true">
完成插入操作后,将调用fileAttachment的setId方法,并将其设置为上次插入记录的id。您可以使用fileAttachment的getId获取最后一个插入id
我希望这能帮助您。codeGenerator的配置需要:
<table schema="catalogue" tableName="my_table" >
<generatedKey column="my_table_id" sqlStatement="JDBC" identity="true" />
<columnOverride column="my_table_id" isGeneratedAlways="true"/>
</table>
代码生成后,插入包括id字段的自动更新谢谢您的回复Ed,那么我如何才能获得该id?我不知道,但我相信myBatis,所以我会说是的。
After struggling alot, I got the following solution:
Use Case: if you are using sequence for generating primary key. and you want the id inserted to db
in xml:
<insert id="createEmployee"
parameterType="com.test.EmployeeModel">
<selectKey keyProperty="employeeId" keyColumn="EMPLOYEE_ID"
resultType="Long" order="BEFORE">
select EMPLOYEE_SEQ.NEXTVAL FROM DUAL
</selectKey>
INSERT INTO EMPLOYEE(EMPLOYEE_ID,EMPLOYEE_NAME)
VALUES
(#{employeeId,jdbcType=NUMERIC},#{EMPLOYEE_NAME,jdbcType=VARCHAR})
</insert>
in java side:
interface
public void createEmployee(EmployeeModel request);
in dao
getMapper().createEmployee(model);
getClient().commit();
Long employeeId= model.getEmployeeId();
System.out.println("Recent Employee Id: "+employeeId)
@Insert("your sql goes here")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insert(FileAttachment fileAttachment) throws Exception;
<table schema="catalogue" tableName="my_table" >
<generatedKey column="my_table_id" sqlStatement="JDBC" identity="true" />
<columnOverride column="my_table_id" isGeneratedAlways="true"/>
</table>
After struggling alot, I got the following solution:
Use Case: if you are using sequence for generating primary key. and you want the id inserted to db
in xml:
<insert id="createEmployee"
parameterType="com.test.EmployeeModel">
<selectKey keyProperty="employeeId" keyColumn="EMPLOYEE_ID"
resultType="Long" order="BEFORE">
select EMPLOYEE_SEQ.NEXTVAL FROM DUAL
</selectKey>
INSERT INTO EMPLOYEE(EMPLOYEE_ID,EMPLOYEE_NAME)
VALUES
(#{employeeId,jdbcType=NUMERIC},#{EMPLOYEE_NAME,jdbcType=VARCHAR})
</insert>
in java side:
interface
public void createEmployee(EmployeeModel request);
in dao
getMapper().createEmployee(model);
getClient().commit();
Long employeeId= model.getEmployeeId();
System.out.println("Recent Employee Id: "+employeeId)