mybatis-plus 在执行bamapper.inrt()的时候,会将传进去的实体的id自动更新为插入的主键值。
@trans政府机构改革actional(rollbackfor = exception.class)@overridepublic string savecour网学在线info(courinfoform courinfoform) { //保存课程基本信息 cour cour = new cour(); cour.tstatus(cour.cour_draft); beanutils.copyproperties(courinfoform, cour); system.out.println("执行inrt之前的id值:" + cour.getid()); bamapper.inrt(cour); system.out.println("执行inrt之后的i建筑学专业学什么d值:" + cour.getid()); //保存课程详情信息 courdescription courdescription = new courdescription(); courdescription.tdescription(courinfoform.getdescription()); courdescription.tid(cour.getid()); courdescriptionmapper.inrt(courdescription); return cour.getid();}
执行inrt之前的id值:null
creating a new 画椭圆sqlssion
registering transaction synchronization for sqlssion [org.apache.ibatis.ssion.defaults.defaultsqlssion@6911ef9a]
jdbc connection [hikariproxyconnection@2133325606 wrapping com.mysql.cj.jdbc.connectionimpl@5e4ceb08] will be managed by spring
==> preparing: inrt into edu_cour ( id, teacher_id, subject_id, subject_parent_id, title, price, lesson_num, cover, status, gmt_create, gmt_modified ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> parameters: 1417099738586558466(string), 1189426464967995393(string), 1416677134608371713(string), 1416677134537068547(string), linux操作系统(string), 199(bigdecimal), 32(integer), /d/file/titlepic/c5c453cc-0cdb-4c01-a2fc-3973bfe295fa.jpg(string), draft(string), 2021-07-19 20:31:20.777(timestamp), 2021-07-19 20:31:20.777(timestamp)
<== updates: 1
releasing transactional sqlssion [org.apache.ibatis.ssion.defaults.defaultsqlssion@6911ef9a]
执行inrt之后的id值:1417099738586558466
所以说,在业务需要存储信息到多个表并且记录的id是一一对应的时候,就可以利用这个特性。而没有必要去执行sql语句查询id值。
我在添加的时候,无缘无辜的给我报
java.sql.sqlexception: field ‘id’ doesn’t have a default value
如图:
后来了解到 使用 mybatis-plus的inrt方法,在底层会默认生成一个long类型的uuid,这就导致跟数据库里面类型不一致导致错误,我们首先要做的是要把这个默认自增的主键给禁了
@tableid(value = "id",type = idtype.input)private string id;
@datapublic class jtbtransactionrecord extends model<jtbtransactionrecord> implements rializable{ private static final long rialversionuid = 1l; /** * 交易主键 */ @tableid(value = "id",type = idtype.input) private string id;}
大功告成。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 00:16:40,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/19fca62ac0bc292991b927d549fde853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:mybatis.doc
本文 PDF 下载地址:mybatis.pdf
留言与评论(共有 0 条评论) |