前言
这里我列举了MyBatis和MyBatis-Plus常用的五种批量插入的方式,进行了详细的总结归纳,写的非常详细,整体思路清晰明了,只分享干货。
准备工作
MyBatis利用For循环批量插入
MyBatis的手动批量提交
MyBatis以集合方式批量新增(推荐)
MyBatis-Plus提供的SaveBatch方法
MyBatis-Plus提供的InsertBatchSomeColumn方法(推荐)
总结
mysql mysql-connector-java runtime
org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2
com.baomidou mybatis-plus-boot-starter 3.5.2
org.projectlombok lombok true
2、配置yml文件
server:port: 8080spring:datasource:username: mysql用户名password: mysql密码url: jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivermybatis:mapper-locations: classpath:mapping/*.xml
3、公用的User类
@Data
public class User {private int id;private String username;private String password;
}
1、编写UserService服务类,测试一万条数据耗时情况
@Service
public class UserService {@Resourceprivate UserMapper userMapper;public void InsertUsers(){long start = System.currentTimeMillis();for(int i = 0 ;i < 10000; i++) {User user = new User();user.setUsername("name" + i);user.setPassword("password" + i);userMapper.insertUsers(user);}long end = System.currentTimeMillis();System.out.println("一万条数据总耗时:" + (end-start) + "ms" );}}
2、编写UserMapper接口
@Mapper
public interface UserMapper {Integer insertUsers(User user);
}
3、编写UserMapper.xml文件
INSERT INTO user (username, password)VALUES(#{username}, #{password})
4、进行单元测试
@SpringBootTest
class DemoApplicationTests {@Resourceprivate UserService userService;@Testpublic void insert(){userService.InsertUsers();}}
5、结果输出
一万条数据总耗时:26348ms
@Service
public class UserService {@Resourceprivate UserMapper userMapper;@Resourceprivate SqlSessionTemplate sqlSessionTemplate;public void InsertUsers(){//关闭自动提交SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);UserMapper userMapper = sqlSession.getMapper(UserMapper.class);long start = System.currentTimeMillis();for(int i = 0 ;i < 10000; i++) {User user = new User();user.setUsername("name" + i);user.setPassword("password" + i);userMapper.insertUsers(user);}sqlSession.commit();long end = System.currentTimeMillis();System.out.println("一万条数据总耗时:" + (end-start) + "ms" );}}
2、结果输出
一万条数据总耗时:24516ms
@Service
public class UserService {@Resourceprivate UserMapper userMapper;public void InsertUsers(){long start = System.currentTimeMillis();List userList = new ArrayList<>();User user;for(int i = 0 ;i < 10000; i++) {user = new User();user.setUsername("name" + i);user.setPassword("password" + i);userList.add(user);}userMapper.insertUsers(userList);long end = System.currentTimeMillis();System.out.println("一万条数据总耗时:" + (end-start) + "ms" );}}
2、编写UserMapper接口
@Mapper
public interface UserMapper {Integer insertUsers(List userList);
}
3、编写UserMapper.xml文件
INSERT INTO user (username, password)VALUES(#{user.username}, #{user.password})
4、输出结果
一万条数据总耗时:521ms
@Service
public class UserService extends ServiceImpl implements IService {public void InsertUsers(){long start = System.currentTimeMillis();List userList = new ArrayList<>();User user;for(int i = 0 ;i < 10000; i++) {user = new User();user.setUsername("name" + i);user.setPassword("password" + i);userList.add(user);}saveBatch(userList);long end = System.currentTimeMillis();System.out.println("一万条数据总耗时:" + (end-start) + "ms" );}
}
2、编写UserMapper接口
@Mapper
public interface UserMapper extends BaseMapper {}
3、单元测试结果
一万条数据总耗时:24674ms
public class EasySqlInjector extends DefaultSqlInjector {@Overridepublic List getMethodList(Class> mapperClass, TableInfo tableInfo) {// 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法List methodList = super.getMethodList(mapperClass, tableInfo);methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));return methodList;}}
2、定义核心配置类注入此Bean
@Configuration
public class MybatisPlusConfig {@Beanpublic EasySqlInjector sqlInjector() {return new EasySqlInjector();}
}
3、编写UserService服务类
public class UserService{@Resourceprivate UserMapper userMapper;public void InsertUsers(){long start = System.currentTimeMillis();List userList = new ArrayList<>();User user;for(int i = 0 ;i < 10000; i++) {user = new User();user.setUsername("name" + i);user.setPassword("password" + i);userList.add(user);}userMapper.insertBatchSomeColumn(userList);long end = System.currentTimeMillis();System.out.println("一万条数据总耗时:" + (end-start) + "ms" );}
}
4、编写EasyBaseMapper接口
public interface EasyBaseMapper extends BaseMapper {/*** 批量插入 仅适用于mysql** @param entityList 实体列表* @return 影响行数*/Integer insertBatchSomeColumn(Collection entityList);
}
5、编写UserMapper接口
@Mapper
public interface UserMapper extends EasyBaseMapper {}
6、单元测试结果
一万条数据总耗时:575ms