mybatis插入的同时获取主键id

it2022-07-01  91

<insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole" useGeneratedKeys="true" keyProperty="id"> insert into app_cms_role <trim prefix="(" suffix=")" suffixOverrides=","> <if test="name != null"> name, </if> <if test="createTime != null"> create_time, </if> <if test="updateTime != null"> update_time, </if> <if test="appId != null"> app_id, </if> <if test="roleName != null"> role_name, </if> <if test="rolePrivilege != null"> role_privilege, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null"> #{updateTime,jdbcType=TIMESTAMP}, </if> <if test="appId != null"> #{appId,jdbcType=INTEGER}, </if> <if test="roleName != null"> #{roleName,jdbcType=VARCHAR}, </if> <if test="rolePrivilege != null"> #{rolePrivilege,jdbcType=VARCHAR}, </if> </trim> </insert>

需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。

方法:在mapper中指定keyProperty属性,示例如上:

AppCmsRole role=new AppCmsRole();appCmsRoleExMapper.insertAndReturnId(role);role.getId();//就能获取到id了,很神奇吧

 

转载于:https://www.cnblogs.com/plf112233/p/4167889.html

相关资源:mybatis 批量插入返回主键

最新回复(0)