mybatis-plus insert返回主键 文心快码BaiduComate 在MyBatis-Plus中,进行insert操作后获取插入记录的主键ID是一个常见的需求。以下是关于如何在MyBatis-Plus中实现这一需求的详细解答: 1. 理解MyBatis-Plus中insert操作后的返回值 MyBatis-Plus的insert方法默认返回插入的行数(即受影响的行数)。 通过配置,可以让...
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> 结果和上文...
insert可以使用数据库支持的自动生成主键策略,设置useGeneratedKeys=”true”,然后把keyProperty 设成对应的列,就搞定了。比如说上面的StudentEntity 使用auto-generated 为id 列生成主键. <insert id="insertStudent" parameterType="StudentEntity" useGeneratedKeys="true" keyProperty="studentID"> 1. 推荐使用这种用法。
Service类调用userInfoDao的insert方法(此方法是来源于BaseMapper)。但是insert成功后没有返回主键userId。上网查了下,其他人都是这样设置,就会有主键返回。 看到dao类里面一条注释,// int insert(UserInfo record); ,心里有个想法。 这条注释对应的insert方法,是使用mybatis generator生成的。但是因为此方法名和mybati...
(1)单纯的insert和insert on duplicate key update 这也是本文最大的特点,查询网上各种阐述Mybatis返回主键的文章,基本只关注insert时Mybatis返回主键的情况,对于插入或更新的sql语句insert on duplicate key update时mybatis返回主键(此时还细分为仅insert,仅update和insert和update混合三种情况)的文章则比较少。
使用 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,需要配置一下实体类的主键 publicclassWeb_user{@TableId(type=IdType.AUTO)privatelong user_id;privateString user_tel;privateString user_pwd;privateLong createtime;privateLong modifytime;privateLong last_visit_time;} ...
<insert id="insert"parameterType="com.xx.entity.Dept"useGeneratedKeys="true"keyProperty="deptno"keyColumn="deptno">insert into dept(deptname,loc) values(#{deptname},#{loc})</insert> 获取: 因为类注解:@Controller. 所有得加上@ResponseBody, 否则会报错。