使用Mybatisplus自带selectById和insert方法 关于使用Mybatisplus自带的selectById和insert方法时的一些问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

使用Mybatisplus自带selectById和insert方法 关于使用Mybatisplus自带的selectById和insert方法时的一些问题

huarenguo   2021-03-16 我要评论
想了解关于使用Mybatisplus自带的selectById和insert方法时的一些问题的相关内容吗,huarenguo在本文为您仔细讲解使用Mybatisplus自带selectById和insert方法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Mybatisplus自带selectById和insert,Mybatisplus,selectById和insert,下面大家一起来学习吧。

一.关于使用Mybatisplus自带的selectById和insert方法时的一些问题

1.selectById的问题

(1).表的主键列名不是id时

查询不到数据,因为Mybatisplus自动生成的sql语句where后面拼接的是where null = ?

这就表示表的主键列名的名字不是id,而Mybatisplus默认的是使用id为主键名的

(2).解决方法

@Id
  @TableId("commodity_id")
  @Column("commodity_id")//设置mybatisplus自动根据id查询时,表的实际id不是默认的id时,找不到id的情况
  @ApiModelProperty(name = "commodityId", value = "商品的id")
  private String commodityId;

在对应映射的实体类里,主键字段的头上加上@TableId("commodity_id"),就表示告诉mybatisplus你的主键列名为commodity_id

2.insert的问题

(1).设置了@TableId("specifications_id")以后

设置了@TableId("specifications_id")以后,并且数据库的主键列是自增的类型而不是我们手段插入的数据,那么使用Mybaitsplus自带的inser方法时,就会导致数据存不进数据库

(2)解决方法

@Id
  @TableId(value = "specifications_id",type = IdType.AUTO)
  @Column("specifications_id")
  @ApiModelProperty(name = "specificationsId", value = "商品的规格id")
  private Integer specificationsId;

要标明当前主键是自增的列,设置type = IdType.AUTO

总结

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们