Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 08:28:24 09月30日 Update

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)

Tags: Entity update information time problem business content article type course consistency necessity one-to-one correspondence operating system code value interest multiple guys buddies. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi OPPO Reno Huawei Shulou Tech Info