在MyBatis中,当执行INSERT操作时,如果表的主键是自增的,并且你希望在插入数据后获取到这个自增的主键值,可以通过配置useGeneratedKeys和keyProperty属性来实现。以下是详细的步骤和示例代码: 1. 编写MyBatis的insert映射语句 在MyBatis的mapper XML文件中,编写INSERT语句,并配置useGeneratedKeys和keyProperty属性。 xml <...
对于自增主键在某些业务中保存一个对象后,需要使用到这个主键完成后续的业务逻辑,就需要获取该主键值。 1、在接口中定义新增方法 int addStudent(Student student); 2、在mapper中配置新增配置 方式一: <insert id="addStudent&q
2.插入数据返回自增主键ID方法(一) 在映射器中配置获取记录主键值 xml映射: 在xml中定义useGeneratedKeys为true,返回主键id的值,keyProperty和keyColumn分别代表数据库记录主键字段和java对象成员属性名 代码语言:javascript 复制 <!--插入数据:返回记录主键id值--><insert id="insert"useGeneratedKeys="true"keyProperty...
在 insert 元素结束后,我们可以使用 SELECT LAST_INSERT_ID() 函数获取插入记录的主键 ID:<insert id="insertUser" parameterType="User" useGeneratedKeys="false"> insert into user (name, age) values (#{name}, #{age}) <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">...
1.执行完insert语句,返回自增列最新的值。 两种方式 <insert id="create"parameterType="com.xcg.webapp.model.entity.Production"useGeneratedKeys="true"keyProperty="production_id">insert into production(production_code,production_name,img_url,spec,purchase_price,sales_price,production_status) values(#{produc...
不支持生成自增主键的数据库,使用<selectKey>(例如oracle不支持自增主键)。 1、使用useGenerateKeys和keyProperty 来返回插入后的主键: 在insert标签中,parameterType可以是一个实体类,也可以是map类型,如下: <insertid=“doSomething" parameterType = "map" useGeneratedKeys = "true" keyProperty = “yourId"> ...
Mybatis注解方式insert时获取返回的自增主键 使用@options注解 @Insert("insert into scenario_storage " + "(user_id , scenario_name , nodes_name , publish_subscribe_name , test_design_name , type_define_name , upload_url , create_time ,remark)" +...
// 返回主键字段id值 @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") @Insert("insert into t_user (name,age) values (#{name},#{age})") void insert(Student stu); 3 Mybatis Plus 中 调用BaseMapper 的 insert方法后 ,默认将自增主键封装在 插入对象中 4 聊一聊 ...
这是最近在实现perfect-ssm中的一个功能时碰到的一个小问题,觉得需要记录一下,向MySQL数据库中插入一条记录后,需要获取此条记录的id值,以生成对应的key值存入到redis中,id为自增int主键。 修改 原代码为: 代码语言:javascript 复制 <insert id="insertArticle"parameterType="Article">insert intossm_article(artic...