Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of id automatic update of entities after mybatis-plus executes insert ()

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to solve the problem of id automatic update of entities after the implementation of insert () in mybatis-plus. The article is very detailed and has a certain reference value. Interested friends must read it!

Mybatis-plus executes insert (), and the id of the entity is updated automatically

When mybatis-plus executes baseMapper.insert (), it automatically updates the id of the passed entity to the inserted primary key value.

The business code @ Transactional (rollbackFor = Exception.class) @ Overridepublic String saveCourseInfo (CourseInfoForm courseInfoForm) {/ / saves the basic course information Course course = new Course (); course.setStatus (Course.COURSE_DRAFT); BeanUtils.copyProperties (courseInfoForm, course); System.out.println ("id value before insert: + course.getId ()); baseMapper.insert (course) System.out.println ("id value after insert:" + course.getId ()); / / Save course details CourseDescription courseDescription = new CourseDescription (); courseDescription.setDescription (courseInfoForm.getDescription ()); courseDescription.setId (course.getId ()); courseDescriptionMapper.insert (courseDescription); return course.getId ();} run result

Id value before executing insert: null

Creating a new SqlSession

Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6911ef9a]

JDBC Connection [HikariProxyConnection@2133325606 wrapping com.mysql.cj.jdbc.ConnectionImpl@5e4ceb08] will be managed by Spring

= > Preparing: INSERT INTO edu_course (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 operating system (String), 199,199 (BigDecimal), 32 (Integer), https://guli-file1-wang.oss-cn-beijing.aliyuncs.com/cover/2021/07/19/c5c453cc-0cdb-4c01-a2fc-3973bfe295fa.jpg(String), Draft (String), 2021-07-19 20Rule 31V20.777 (Timestamp), 2021-07-1920Rule 20.777 (Timestamp)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report