Insert data into MyBatis many-to-many intermediate table

When doing this demo of the employee management system, because user and role are in many-to-many relationship, and the user primary key is self-increasing, so we have no way to know this user_id in advance, so when inserting, we need to insert user first, and then Find the id you just inserted, take it out, and insert it into the middle table user_role, so that the table relationships can be matched and a complete insertion process can be considered.

So the question now is how to know this user_id, how to take it out, and insert it into the intermediate table user_role.

Method: 3 attributes under the insert and update elements are used in MyBatis:
useGeneratedKeys (only useful for insert and update) This will make MyBatis use the getGeneratedKeys method of JDBC to retrieve the primary keys generated internally by the database (such as: MySQL and Auto-increment field of relational database management system like SQL Server), default value: false.
keyProperty (only useful for insert and update) uniquely marks a property, MyBatis will set its key value through the return value of getGeneratedKeys or through the selectKey sub-element of the insert statement. Default: unset. If you want multiple generated columns, it can also be a comma-separated list of attribute names.
keyColumn (only useful for insert and update) Set the column name in the table by the generated key value. This setting is only necessary in some databases (like PostgreSQL). It needs to be set when the primary key column is not the first column in the table . If you want multiple generated columns, it can also be a comma-separated list of attribute names.

Specific implementation:
1. Insert the user table, you can see that we did not insert the user_id attribute, because it is self-increasing.

<insert id="insert" useGeneratedKeys="true" keyProperty="user_id" keyColumn="user_id">
        insert into user
        (user_name,user_gender,user_email,user_phone,user_address,user_birthday,department_id)
        values(#{
   
   user_name},#{
   
   user_gender},#{
   
   user_email},#{
   
   user_phone},#{
   
   user_address},#{
   
   user_birthday},#{
   
   department_id})
    </insert>

2. The focus is on the intermediate table user_role. As you can see, we directly use the user_id under user, and we have not done any query operations, so we don't know its specific value.

<insert id="insertUserRole">
        insert into user_role values(#{
   
   user.user_id},#{
   
   role.role_id})
    </insert>

3. Test class, in order to facilitate everyone's better understanding, we can do a test.
You can see from the following code: After inserting user, we can print out user_id and insert it into the intermediate table without doing other operations such as query.

Date date = new Date();
        User user = new User("mike33", "male", "[email protected]", "183xxxxxxxx", "chengdu", date, 1);
        Role role = new Role();
        role.setRole_id(4);
        //插入user
        service.insert(user);
        //打印user_id
        System.out.println("user_id----->>>>" + user.getUser_id());
        //插入中间表
        service.insertUserRole(user, role);

Result: we can see that after we insert directly, we can get user_id, and then insert the intermediate table, which solves this problem.

Of course, since inserting a new user, the mapping of user and role must be satisfied at the same time, so Spring transaction is used here to ensure the integrity of the insertion process.
Use annotations to achieve.

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
    public void insertUserRole(User user, Role role) {
        this.insert(user);
        mapper.insertUserRole(user, role);
    }

Guess you like

Origin blog.csdn.net/u010857795/article/details/71512044