Home Database Mysql Tutorial How to operate mybaits batch insert

How to operate mybaits batch insert

Jun 21, 2017 pm 04:04 PM
Summarize

When we insert data in batches, we need to get the id of the inserted data.

This way:

 <insert id="insertUser" parameterType="gys.entity.User" keyProperty="userId" useGeneratedKeys="true">INSERT INTO `user`
        (userName)
        VALUES
        (#{userName})</insert>
Copy after login

This is no problem.

But sometimes it involves batch insertion, and to get the inserted id

write like this:

<insert id="insertUserBatch1" keyProperty="userId" useGeneratedKeys="true">INSERT INTO `user`
        (userName)
        VALUES<foreach collection="list" separator="," item="item">(#{item.userName})</foreach></insert>
Copy after login

An exception will occur after running like this.

This is because the version of mybatis you are using is too low. For example, I am using version 3.2.2, which is a bug in mybatis.

If you switch to version 3.4.4, there will be no problem.

If the above sql statement is written differently, an exception will be reported again (enclose insert in foreach)

For example:

<insert id="insertUserBatch2"> <foreach collection="list" separator=";" item="item"> INSERT INTO `user`
                (userName)
            VALUES
                (#{item.userName})         </foreach></insert>
Copy after login

Similarly, there is also update Batch update also has this problem

<update id="updateUserBatch"><foreach collection="list" item="item" separator=";">update `user` set
                userName=#{item.userName}
            where
                userId=#{item.userId}</foreach></update>
Copy after login

This is because mybatis can only execute one sql statement by default.

You can add parameters when linking the path. Multiple sql statements were executed.allowMultiQueries=true

The above is the detailed content of How to operate mybaits batch insert. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Summarize the usage of system() function in Linux system Summarize the usage of system() function in Linux system Feb 23, 2024 pm 06:45 PM

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

Git workflow management experience summary Git workflow management experience summary Nov 03, 2023 pm 06:45 PM

Summary of Git workflow management experience Introduction: In software development, version management is a very important link. As one of the most popular version management tools currently, Git's powerful branch management capabilities make team collaboration more efficient and flexible. This article will summarize and share the experience of Git workflow management. 1. Introduction to Git workflow Git supports a variety of workflows, and you can choose the appropriate workflow according to the actual situation of the team. Common Git workflows include centralized workflow, feature branch workflow, GitF

How to write a ppt summary report and how to write it well How to write a ppt summary report and how to write it well Mar 19, 2024 pm 08:16 PM

Summary reports are an essential skill for survival in the workplace. If you have a well-organized PPT summary report, it can not only save the leader's time, but also provide a focused summary of the work, which will definitely impress the leader. How to write a ppt summary report? Let’s take a look! We open a case file and explain based on this case. This case looks a bit outdated, as shown in the picture below. 2. Since we want to modify the PPT just now, let’s explain its existing problems in a targeted manner, as shown in the figure. 3. Here we introduce to students a [color matching] website, the vanschneider.com website. The website here is still very rich in color matching, as shown in the figure. 4. Here we prepare the PPT text material, and then

Explore the origins of nature! The seventh bullet of Google's 2022 year-end summary: How can 'Biochemical Environmental Materials” reap the dividends of machine learning? Explore the origins of nature! The seventh bullet of Google's 2022 year-end summary: How can 'Biochemical Environmental Materials” reap the dividends of machine learning? Apr 11, 2023 pm 07:19 PM

With huge advances in machine learning and quantum computing, we now have new and more powerful tools to collaborate with researchers across industries in new ways and radically accelerate the progress of groundbreaking scientific discoveries. The theme of this year's Google year-end summary is "Natural Science." The author of the article is John Platt, an outstanding scientist at Google Research. He graduated from the California Institute of Technology with a Ph.D. in 1989. Since joining Google Research eight years ago, I have been fortunate to be part of a community of talented researchers applying cutting-edge computing technologies to advance the possibilities of applied science. The team is currently exploring topics in the physical and natural sciences, from helping organizations The world's protein and genome information to benefit people's lives, to the extent of utilization

Summary of ThinkPHP development experience: Tips to improve development efficiency Summary of ThinkPHP development experience: Tips to improve development efficiency Nov 22, 2023 pm 12:28 PM

ThinkPHP is a popular PHP development framework that is flexible, efficient, and easy to use. It is widely used in the development of web applications. In long-term development practice, I have summarized some techniques to improve development efficiency, which I will share with you below. 1. Make full use of the core functions of ThinkPHP. ThinkPHP provides many powerful core functions, such as models, views, controllers, etc. We need to make full use of these functions to simplify the development process. First of all, we must use the MVC design pattern correctly and integrate the business

Analysis and summary of application cases of Baidu AI interface in Java development Analysis and summary of application cases of Baidu AI interface in Java development Aug 13, 2023 pm 12:10 PM

Analysis and summary of application cases of Baidu AI interface in Java development. With the continuous development and application of artificial intelligence technology, Baidu AI interface, as one of them, is widely used in all walks of life. This article will analyze and summarize the application cases of Baidu AI interface in Java development, and illustrate its specific application methods and effects through code examples. 1. Overview of Baidu AI interface Baidu AI interface is a set of services provided to developers through API calls based on Baidu's artificial intelligence technology. It contains rich functions

Summary of the Margin property in CSS Summary of the Margin property in CSS Feb 18, 2024 pm 10:11 PM

Summary of the margin attribute in CSS The margin attribute in CSS is used to set the margins of an element, which can control the spacing between the element and surrounding elements. This article will summarize the margin attribute and provide some specific code examples for reference. The margin attribute has four values, which represent the top, right, bottom and left margins of the element. You can set the margin value in the following ways: Single value: Set the margins in all directions to be equal. For example: .margin{margin:

Summary and comparison of MySQL storage engines: Which one suits your business needs? Summary and comparison of MySQL storage engines: Which one suits your business needs? Jul 25, 2023 am 11:09 AM

Summary and comparison of MySQL storage engines: Which one suits your business needs? Introduction: MySQL is a widely used relational database management system, and the storage engine is the key module used by MySQL to store and manage data. MySQL provides a variety of storage engines, each of which has its own characteristics and applicable scenarios. This article will summarize and compare the commonly used storage engines in MySQL, and provide corresponding business needs suggestions. 1. InnoDB engine InnoDB is the default storage engine of MySQL

See all articles