Instructions for on duplicate key update (solve batch operation data, update if there is one, add if not) mybatis batch operation data update and add

The ORM framework used for the project is made with springdatajpa. For some batch data operations, the efficiency is too low, so use mybatis to write SQL to optimize it.

In general, we must first query, modify if there is, and add if not. In this case, a single operation is fine. If it is a large amount of data, it needs to be checked and changed every time (if springdatajpa is used, it must be checked before adding) Once again), it consumes too much resources.

So is there a solution for mysql? Of course there are. I checked some information. There are two methods, one is to use replace into, and the other is to solve on duplicate key update. Let me first demonstrate the difference between these two methods.

First create a table (the username column I set is unique, and the username cannot be repeated)

CREATE TABLE user_table(
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(10) UNIQUE KEY,
    address VARCHAR(10)
)

Add a piece of data

INSERT INTO user_table (id, username, address) VALUE (NULL, 'Hu Junjie', 'Beijing')

 Purpose: To query whether the data exists, and then modify it (implemented in one sentence), Hu Junjie is no longer in Beijing, and ran to Heilongjiang

REPLACE INTO user_table (id, username, address) VALUE (NULL, 'Hu Junjie', 'Heilongjiang')

After sql runs, it shows that 2 lines are affected.

 

 

 After observing the data in the database, the name has not changed, the address has changed, and the id has also changed

 

 

 Explain that replace into is to delete the original first, and then add.

This is obviously bad. There is a scenario where the id of this table is a foreign key of a table, so this will cause an error, so we introduce method 2 to solve it.

Method Two:

Now, Hu Junjie is arranged to return to Beijing. The sql statement is as follows

INSERT INTO user_table (id, username, address) VALUE (NULL, 'Hu Junjie', 'Beijing') ON DUPLICATE KEY UPDATE address = 'Beijing' 
INSERT INTO user_table (id, username, address) VALUE (NULL, '胡俊杰', ' Beijing ') ON DUPLICATE KEY UPDATE address = VALUES (address) // Both methods can be used, the following can dynamically bind the value passed

This time there are still two lines changed, let us look at the data in the database

 

 

 

 

 

 The id has not changed, only the address, which is the most practical way.

Next, demonstrate how to do batch modification of mybatis incoming list

@Insert("<script>  insert into user_table (username,age,address)"+
            "  values  <foreach collection ='users' item='item' index= 'index' separator =','> " +
            "(#{item.username},#{item.age},#{item.address}) </foreach >" +
            " ON DUPLICATE KEY UPDATE  username = values(username), age = values(age) ,address = values(commaddr) </script>")
    void insert(@Param("users") List<User> users);

 If you do n’t understand, you can leave a message to me, I hope I can help you! ! !

Guess you like

Origin www.cnblogs.com/super-hu/p/12681749.html