文章目录
前言
今天在维护一个小项目时,发现使用mybatisplus批量插入10000条数据,耗时30秒。看了一下配置文件,发现mysql配置中没有添加rewriteBatchedStatements=true
yml
rewriteBatchedStatements=true
完整配置
yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
参数说明
- 基础连接参数
useUnicode=true - 启用 Unicode 字符集支持
characterEncoding=utf-8 - 设置字符编码为 UTF-8 - 日期时间处理
zeroDateTimeBehavior=convertToNull - 当遇到零日期时间值时转换为 NULL,避免解析异常 - 布尔类型处理
transformedBitIsBoolean=true - 将 BIT 类型字段转换为布尔类型 - 多查询支持
allowMultiQueries=true - 允许在单个语句中执行多个 SQL 查询 - 安全相关
useSSL=false - 禁用 SSL 连接(开发环境常用)
allowPublicKeyRetrieval=true - 允许公钥检索(MySQL 8.0+ 必需) - 性能优化
rewriteBatchedStatements=true - 重写批处理语句以提高性能,将多条 INSERT/UPDATE 语句合并为多值语句