1. 数据库连接配置
 在使用 MyBatis Plus 进行批量插入之前,首先需要配置数据库连接。在连接 URL 中添加 &rewriteBatchedStatements=true,以提高批量插入的性能。以下是一个示例:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true 
 2. 创建实体类
 
 我们将创建一个名为 Product 的实体类,包含多个字段,以便演示批量插入的功能。以下是 Product 类的示例代码:
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
@Data
@TableName("product") // 假设数据库中对应的表名为 product
public class Product {
    private Long id;                // 产品ID
    private String name;            // 产品名称
    private String description;     // 产品描述
    private BigDecimal price;       // 产品价格
    private Integer stock;          // 库存数量
    private String category;         // 产品类别
    private String brand;           // 产品品牌
    private String sku;             // 库存单位
    private Date createTime;        // 创建时间
    private Date updateTime;        // 更新时间
    private Boolean isActive;        // 产品是否激活
    private String imageUrl;         // 产品图片URL
    private String supplier;         // 供应商信息
    private String tags;             // 产品标签
    private String color;            // 产品颜色
    private String size;             // 产品尺寸
    private String material;         // 产品材质
    private Date expirationDate;     // 产品过期时间
    private Integer rating;          // 产品评分
    private Integer reviewCount;     // 产品评价数量
    private String warranty;         // 产品保修信息
} 
 3. 批量插入示例代码
 
 接下来,我们将编写批量插入的代码。以下是使用 MyBatis Plus 批量插入 10000 条 Product 数据的示例:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {
    @Autowired
    private ProductMapper productMapper;
    public void batchInsertProducts() {
        List<Product> productList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            Product product = new Product();
            product.setId((long) i);
            product.setName("Product" + i);
            product.setDescription("Description for product " + i);
            product.setPrice(BigDecimal.valueOf(random.nextDouble() * 100));
            product.setStock(random.nextInt(100));
            product.setCategory("Category" + (random.nextInt(10) + 1));
            product.setBrand("Brand" + (random.nextInt(5) + 1));
            product.setSku("SKU" + i);
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            product.setIsActive(random.nextBoolean());
            product.setImageUrl("http://example.com/images/product" + i + ".jpg");
            product.setSupplier("Supplier" + (random.nextInt(5) + 1));
            product.setTags("tag1,tag2,tag" + (random.nextInt(3) + 1));
            product.setColor("Color" + (random.nextInt(5) + 1));
            product.setSize("Size" + (random.nextInt(3) + 1));
            product.setMaterial("Material" + (random.nextInt(4) + 1));
            product.setExpirationDate(new Date(System.currentTimeMillis() + (random.nextInt(365) * 24 * 60 * 60 * 1000L))); // 随机过期时间
            product.setRating(random.nextInt(6)); // 评分范围 0-5
            product.setReviewCount(random.nextInt(100)); // 随机评论数量
            product.setWarranty("Warranty for " + i + " years");
            productList.add(product);
        }
        long startTime = System.currentTimeMillis();
        productMapper.insertBatchSomeColumn(productList); // 批量插入方法
        long endTime = System.currentTimeMillis();
        System.out.println("插入10000条数据耗时: " + (endTime - startTime) + "毫秒");
    }
} 
 4. 性能测试
 
 通过上述代码,我们可以看到,批量插入 10000 条数据的时间大大减少。使用 &rewriteBatchedStatements=true 的情况下,插入时间通常在 2.5 秒左右,而不使用时可能需要超过 127 秒。这种性能提升对于数据密集型应用程序来说至关重要。
5. 总结
 本文介绍了如何使用 MyBatis Plus 框架进行批量写入数据库,并通过设置数据库连接 URL 中的 &rewriteBatchedStatements=true 来优化性能。通过合理的配置和代码实现,我们能够显著提高数据插入的效率,提升应用程序的整体性能。




















