Mybatis-plus插入后返回元素id的问题

mybatis-plus插入后返回插入元素的id

有三种方法,第三种最简单。

不想麻烦的直接看第三种

1.mybatis原生

mybaits-plus要使用mybatis原生需要一下配置,指定下mapper文件的位置就好

mybatis-plus:
  mapper-locations: classpath*:mapperxml/*Mapper.xml

直接先看mapper.xml文件,这个insert语句实际上就是插入MouldMessage这个我定义的实体类。


    
        INSERT INTO t_XXXX
        XXXXXX,XXXX,XXXXX
        VALUES XXXX,XXXX,XXXX
    

userGenerateKeys告诉mybatis使用自增主键,keyProperty指定这个主键名称叫id。

然后再mapper接口定义这个方法

Long testInsert(MessageMould messageMould);

调用这个插入语句,information这个实例时没有定义id,创建时间这些字段的,输出结果是数据表修改条数,这里插入一条,所以返回1。

System.out.println(messageMouldMapper.testInsert(information)+“----”);

然后这时候我们输出下information的一些属性,发现本来作为参数的information被mybatis自动填充上了id和创建时间

System.out.println(information.getId()+“*******”+information.getCreateTime());

所以结论就是,插入之后找传入的参数,就能找到新增加元素的id

2.使用mybatis-plus注解

其实跟原生mybatis一样,插入后元素的id会直接映射到参数中,只不过用注解代替了mapper.xml文件

@Insert(value = "INSERT INTO t_XXXX" +
        "XXX,XXX,XXX " +
        "VALUES (XXX,XXX,XXX)")
@SelectKey(statement="select LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Long.class)
Integer testInsert1(MessageMould messageMould);

执行完这条insert操作后,直接拿形参messageMould的id,就能拿到id

3.使用mybatis-plus提供的insert

mybatis只要extends BaseMapper就可以调用他的insert方法。其实也就跟上面2个一样。i调用insert(MessageMould messageMould)后,id会映射到形参messageMould中,直接拿形参messageMould的id,就能拿到id

Mybatis-plus设置id自增,插入数据

没修改前

这是我的实体类。

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Company {
  private Integer id;
  private String cid;
  private String cname;
  private String address;
  private String representation;
  private String phone;
  private String email;
  private String weburl;
  private String introductory;
}

我的数据库设置的是id自增。 添加数据时没有加上id的数据。

然后报错

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@59238b99]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87] was not registered for synchronization because synchronization is not active
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87]
2021-11-07 14:28:06.789 ERROR 5620 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.dk.pojo.Company' with value '1457233381002637313' Cause: java.lang.IllegalArgumentException: argument type mismatch] with root cause

查询得知,当时实体中,plus主键生成方式不设置生成方式时,默认的是自增。而且生成的值就如报错中的‘1457233381002637313’很长的一个值。

而主键的设置类型有:

AUTO(0, “数据库ID自增”),
INPUT(1, “用户输入ID”),
ID_WORKER(2, “全局唯一ID”),
UUID(3, “全局唯一ID”),
NONE(4, “该类型为未设置主键类型”),
ID_WORKER_STR(5, “字符串全局唯一ID”);

所以修改后

第一次:

@TableId( type = IdType.AUTO)
private Integer id;

这样的修改跟修改前一样。我们需要的是12、13这样的序列,所以需要设置id生成方式,就需要在注解设置value的值。

@TableId(value = “id”, type = IdType.AUTO)
private Integer id;

这样指定id的值,我们在用plus插件insert时就不用插入id的值。生成的id值跟数据库对应。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Mybatis-plus插入后返回元素id的问题)