Location>code7788 >text

mybatis-plus batch add, batch modify sample + table statement + postman interface

Popularity:574 ℃/2024-09-03 21:36:14

The use of mybatis-plus development will encounter a large amount of data in the case of insertion and modification of low efficiency, the main reason is that the "Add" and "Modify" method is a piece of data processing, if there are 10,000 pieces of data will be and database interaction 10,000 times so the efficiency is low. How to improve the efficiency of the need for batch operation, the following shows batch insertion and batch modification of the code, the database using mysql.

1、Build table statement

CREATE TABLE yc_test_t  ( 
  id                     int    		 	        COMMENT 'Primary Key ID', 
  name 			         VARCHAR(40)  	        	COMMENT 'Name',
  note                   VARCHAR(100)	   		 	COMMENT 'Remarks', 
  PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='Testing';

2. Entity classes

package ;

import ;
import ;


/**
 * <p>.
 * Test form.
 * </p>.
 *
 * @author yc
 * @since 2024-09-03
 */
@TableName(value = "yc_test_t")
public class YcTestT {
    private static final long serialVersionUID = 1L;

    /**
     * ID。
     */
    @TableId
    private Integer id;
    /**
     * Name.
     */
    private String name;
    /**
     * Remarks.
     */
    private String note;

    public void setId(Integer id) {
         = id;
    }

    public Integer getId() {
        return id;
    }

    public void setName(String name) {
         = name;
    }

    public String getName() {
        return name;
    }

    public void setNote(String note) {
         = note;
    }

    public String getNote() {
        return note;
    }


}

3. mapper class

package ;

import ;
import ;

/**
 * <p>
 * Test table Mapper interface.
 * </p>.
 *
 * @author yc
 * @since 2024-09-03
 */
public interface YcTestTMapper extends BaseMapper<YcTestT> {

}

4. Interface

package ;


import ;
import ;

import ;

/**
 * <p>.
 * Test Form Class of Service.
 * </p>.
 *
 * @author yc
 * @since 2024-09-03
 */
public interface IYcTestTService extends IService<YcTestT> {
    //Batch insertion
    boolean saveBatch(Collection<YcTestT> entityList);

    /**
     * Batch update.
     * @param oldNote old
     * @param newNote new
     * @return status
     */
    boolean updateBatch(String oldNote,String newNote);

    /**
     * New test form for single records.
     *
     * @param ycTestT Parameter description
     * @return status
     */
    int insert(YcTestT ycTestT);

}

5、Interface realization

package ;


import ;
import ;
import ;
import ;
import ;
import ;
import ;


import ;


/**
 * <p>.
 * Test table Service implementation class.
 * </p>.
 *
 * @author yc
 * @since 2024-09-03
 */
@Service
public class YcTestTServiceImpl extends ServiceImpl<YcTestTMapper, YcTestT> implements IYcTestTService {
    @Autowired
    YcTestTMapper ycTestTMapper;

    //Batch insertion
    @Override
    public boolean saveBatch(Collection<YcTestT> entityList) {
        return (entityList);
    }

    /**
     * Batch update.
     * @param oldNote old
     * @param newNote new
     * @return status
     */
    @Override
    public boolean updateBatch(String oldNote,String newNote) {
        // Create an instance of UpdateWrapper
        UpdateWrapper<YcTestT> updateWrapper = new UpdateWrapper<>();
        // Set update conditions, e.g. update based on userId.
        ().eq(YcTestT::getNote, oldNote);
        // Set the values of the fields to be updated
        ("note",newNote);

        // Call the update method for bulk updates
        return (updateWrapper);
    }

    /**
     * New test form for single records.
     *
     * @param ycTestT Parameter description
     * @return status
     */
    @Override
    public int insert(YcTestT ycTestT) {
        return (ycTestT);
    }


}

6. Control category

package ;


import ;
import ;
import ;
import ;
import ;
import .*;

import ;
import ;

/**
 * <p>.
 * Test sheet Front end controller.
 * </p>.
 *
 * @author yc
 * @since 2024-09-03
 */

@RestController
@RequestMapping("/test")
public class YcTestTController {


    @Autowired
    private IYcTestTService ycTestTService;

    /**
     * Batch insertion.
     *
     * @return AjaxResult
     */
    @PostMapping(value = "/saveBatch")
    @ResponseBody
    public String saveBatch() {

        List<YcTestT> list = new ArrayList<>();
        for (int i = 0; i < 5000; i++) {
            YcTestT ycTestT = new YcTestT();
            (i);
            ("Zhang San" + (i + ""));
            ("Old.");
            (ycTestT);
            if ((i + 1) % 3000 == 0) {
                try {
                    (list);
                } catch (Exception e) {
                    // Batch insert failed, change to single insert
                    for (int j = 0; j < (); j++) {
                        try {
                            YcTestT testT = (j);
                            //single insertion
                            (testT);
                        } catch (Exception ex) {
                            ();
                        }
                    }
                } finally {
                    ();
                }
            }
        }
        //Processing of data for the remainder of division 3000
        if (() >= 1) {
            (list);
        }

        return "Batch insertion successful!";
    }

    @PostMapping(value = "/updateBatch")
    @ResponseBody
    public String updateBatch() {
        ("Old.","New");
        return "Batch update successful!";
    }

}

7. postman interface

Batch insert: http://127.0.0.1:8080/test/saveBatch

Batch modification: http://127.0.0.1:8080/test/updateBatch

8, the effect - background implementation of the update effect is a batch update

 

Source code access (free):

(1) Login-Registration:/

(2) Sign in to get points

(3) Search: mybatis-plus batch add-modify sample

(4) List of documents