在MyBatis-Plus中,执行insert操作后返回主键ID是一个常见的需求。MyBatis-Plus提供了多种方式来实现这一功能,下面将分点详细说明: 1. 理解MyBatis-Plus的insert方法 MyBatis-Plus的BaseMapper接口中定义了一个insert方法,该方法用于将实体对象插入到数据库中。如果实体对象的ID属性上使用了@TableId注解,并且指定了主...
1.2、使用UUID自增主键 代码语言:javascript 复制 <insert id="insertUser2"parameterType="com.crush.mybatisplus.entity.User"><selectKey keyProperty="id"order="BEFORE"resultType="String">selectuuid()</selectKey>INSERTINTOtb_user(id,username,password)VALUES(#{id},#{username},#{password});</insert> ...
1.2、使用UUID自增主键 <insert id="insertUser2" parameterType="com.crush.mybatisplus.entity.User"><selectKey keyProperty="id" order="BEFORE" resultType="String">select uuid()</selectKey>INSERT INTO tb_user (id,username,password) VALUES(#{id},#{username},#{password});</insert> 结果和上文...
1)select方式 <insert id="insert"parameterType="com.xx.entity.Dept"> <selectKey resultType="int"keyProperty="deptno"keyColumn="deptno"order="AFTER">selectlast_insert_id()</selectKey>insert into dept(deptname,loc) values(#{deptname},#{loc})</insert> 2)配制方式 <insert id="insert"parameterTy...
userGenerateKeys告诉mybatis使用自增主键,keyProperty指定这个主键名称叫id。 然后再mapper接口定义这个方法 LongtestInsert(MessageMould messageMould); AI代码助手复制代码 调用这个插入语句,information这个实例时没有定义id,创建时间这些字段的,输出结果是数据表修改条数,这里插入一条,所以返回1。
如果我们使用了数据库自增主键并且希望insert方法都返回主键ID,需要配置一下实体类的主键 publicclassWeb_user{@TableId(type=IdType.AUTO)privatelong user_id;privateString user_tel;privateString user_pwd;privateLong createtime;privateLong modifytime;privateLong last_visit_time;} ...
使用 SELECT LAST_INSERT_ID() 函数 在 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...
<insert id="insert" parameterType="com.xx.entity.Dept"> <selectKey resultType="int" keyProperty="deptno" keyColumn="deptno" order="AFTER"> select last_insert_id()</selectKey> insert into dept(deptname,loc) values(#{deptname},#{loc})</insert> 2)配制⽅式 <insert id="insert" ...
</insert> 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. insert可以使用数据库支持的自动生成主键策略,设置useGeneratedKeys=”true”,然后把keyProperty 设成对应的列,就搞定了。比如说上面的StudentEntity 使用auto-generated 为id 列生成主键. ...