java
public class OperationLog {
private Long id;
private Long groupId;
private Long userId;
private String crtName;
private Date crtTime;
private String optResult;
private String optType;
private String optContent;
private String optObject;
// Getters and Setters
}
// 插入操作
operationLogMapper.insertPlain(operationLog);
// 获取生成的ID
Long generatedId = operationLog.getId();
XML
<insert id="insertPlain" useGeneratedKeys="true" keyProperty="operationLog.id">
INSERT INTO ${tableName} (group_id, user_id, crt_name, crt_time, opt_result, opt_type, opt_content, opt_object)
VALUES (#{operationLog.groupId}, #{operationLog.userId}, #{operationLog.crtName}, #{operationLog.crtTime}, #{operationLog.optResult}, #{operationLog.optType}, #{operationLog.optContent}, #{operationLog.optObject})
</insert>
解释:
useGeneratedKeys="true": 表示启用自动生成的键值。
keyProperty="operationLog.id": 指定将生成的键值赋值给operationLog对象的id属性。
注意事项:
确保数据库表中的主键字段(通常是id)是自增的。
在Java代码中,插入操作后可以通过operationLog.getId()获取生成的ID值。
另一种方法是,生成雪花算法id,在入库前就已知.