mybatis插入uuid主键同时返回主键id

经过尝试下面是一种可以实现的方式,分享一下

1 java 代码

public String insertChildReport(ChildReport childReport) {
    childReport.setCreateTime(DateUtils.getNowDate());
    LoginUser loginUser = getLoginUser();
    childReport.setCreateBy(loginUser.getUsername());
    childReport.setId(IdUtils.fastUUID());
    int i = childReportMapper.insertChildReport(childReport);
    return childReport.getId();
}

2 mybatis部分

  

<insert id="insertChildReport" parameterType="ChildReport" useGeneratedKeys="true" keyProperty="id">
    insert into child_report
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">id,</if>
        <if test="userId != null">user_id,</if>
        <if test="userName != null">user_name,</if>
        <if test="cid != null">cid,</if>
        <if test="name != null">name,</if>
        <if test="url != null">url,</if>
        <if test="createTime != null">create_time,</if>
        <if test="createBy != null">create_by,</if>
        <if test="updateTime != null">update_time,</if>
        <if test="updateBy != null">update_by,</if>
        <if test="manualReportApp != null">manual_report_app,</if>
        <if test="manualReport != null">manual_report,</if>
        <if test="machineReport != null">machine_report,</if>
        <if test="screeningChildId != null">screening_child_id,</if>
        <if test="phone != null">phone,</if>
        <if test="type != null">type,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">#{id},</if>
        <if test="userId != null">#{userId},</if>
        <if test="userName != null">#{userName},</if>
        <if test="cid != null">#{cid},</if>
        <if test="name != null">#{name},</if>
        <if test="url != null">#{url},</if>
        <if test="createTime != null">#{createTime},</if>
        <if test="createBy != null">#{createBy},</if>
        <if test="updateTime != null">#{updateTime},</if>
        <if test="updateBy != null">#{updateBy},</if>
        <if test="manualReportApp != null">#{manualReportApp},</if>
        <if test="manualReport != null">#{manualReport},</if>
        <if test="machineReport != null">#{machineReport},</if>
        <if test="screeningChildId != null">#{screeningChildId},</if>
        <if test="phone != null">#{phone},</if>
        <if test="type != null">#{type},</if>
    </trim>
</insert>
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatis-plus支持通过`save()`方法插入数据并返回主键。示例代码如下: ``` User user = new User(); user.setName("张三"); user.setAge(20); user.setEmail("zhangsan@example.com"); user.setManagerId(1088248166370832385L); user.setCreateTime(LocalDateTime.now()); user.setDeleted(0); userMapper.insert(user); Long id = user.getId(); ``` 其中,`userMapper`是Mybatis-plus生成的mapper类,`user`是实体类,`insert()`方法用于执行插入操作,并将主键设置到实体类中。 注意: 如果数据库表的主键是自增的,这里获取的就是自增的主键。 如果表的主键UUID,那么可以在实体类上添加注解 @TableId(type = IdType.UUID)来实现。 ### 回答2: Mybatis-Plus 是一款基于 Mybatis 进行封装增强的开发框架,提供了一系列的操作数据库的增删改查方法,又不失原有的 Mybatis 灵活性,其使得开发者可以更加方便快捷的操作数据库。在使用Mybatis-Plus进行插入操作时,有时候需要获取插入后生成的主键ID,这时我们就可以使用 Mybatis-Plus 的插入返回主键的功能。 在Mybatis-Plus中,插入返回主键的方法比较简单,只需要在实体类中加上 @TableId 注解,并且指定其为数据库中的主键,然后在插入数据时即可返回生成的主键ID。 示例代码如下: 第一步:在实体类中加上 @TableId 注解并指定其为数据库中的主键 ``` @Data @TableName("user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id; // 用户ID private String username; // 用户名 private String password; // 密码 private String email; // 邮箱 } ``` 第二步:调用 Mybatis-Plus 的相应方法进行插入操作,并获取用 @TableId 注解指定的主键ID ``` // 获取 Mybatis 的 SqlSession 对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); // 获取 UserMapper 的代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 创建 User 对象 User user = new User(); user.setUsername("test"); user.setPassword("123456"); user.setEmail("test@example.com"); // 执行插入操作,并返回影响的行数 int result = userMapper.insert(user); // 获取插入自动生成的主键ID Long generatedId = user.getId(); // 提交事务并关闭连接 sqlSession.commit(); sqlSession.close(); ``` 以上就是使用 Mybatis-Plus 进行插入操作并返回主键ID的方法。需要注意的是,使用 Mybatis-Plus 进行插入操作时,插入 SQL 语句默认是使用 MySQL 的 replace into 语法,如果需要使用 insert into 语法,可以在 application.properties 中设置 mybatis-plus.global-config.db-config.insert-strategy 的值为 not_null。 ### 回答3: Mybatis-plus是Mybatis的增强版,提供了更为便捷的操作和处理方式。在插入数据时,Mybatis-plus提供了多种方式来返回主键。 1. 使用Mapper自带的insert方法插入数据后,通过获取插入后的主键值来返回主键。示例代码如下: ``` User user = new User(); user.setName("Tom"); user.setAge(18); userMapper.insert(user); Long id = user.getId(); // 获取插入后的主键值 ``` 2. 使用Mapper自带的insert方法插入数据后,通过使用KeyGenerator来返回主键。示例代码如下: ``` public interface UserMapper extends BaseMapper<User> { @Insert("insert into user(name, age) values(#{name}, #{age})") @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class) int insertUser(User user); } User user = new User(); user.setName("Tom"); user.setAge(18); userMapper.insertUser(user); // 调用自定义的insertUser方法 Long id = user.getId(); // 获取插入后的主键值 ``` 3. 使用Mybatis-plus提供的insert方法插入数据时,通过EntityWrapper来封装实体数据并返回主键。示例代码如下: ``` User user = new User(); user.setName("Tom"); user.setAge(18); boolean success = user.insert(); // 调用insert方法 Long id = user.getId(); // 获取插入后的主键值 ``` 需要注意的是,以上方法只适用于MySQL数据库。对于其他数据库的主键生成方式,需要使用不同的KeyGenerator来实现。在实际开发中,根据具体情况选择最适合的方式来返回主键

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值