本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
Mybatis-plus插入后返回元素id的问题
sz199511 · 195浏览 · 发布于2023-03-22 +关注

这篇文章主要介绍了Mybatis-plus插入后返回元素id的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教


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

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

不想麻烦的直接看第三种

1.mybatis原生

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

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

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

<mapper namespace="com.hwz.MessageMouldMapper">
    <insert id="testInsert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO t_XXXX
        XXXXXX,XXXX,XXXXX
        VALUES XXXX,XXXX,XXXX
    </insert>
</mapper>

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.Defaul
tSqlSession@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值跟数据库对应。


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 962浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
不积跬步无以至千里,不积小流无以成江海!
分类专栏
小鸟云服务器
扫码进入手机网页