Mybatis-plus两种批量插⼊⽅法⽐较(带源码)Mybatis-plus两种批量插⼊⽅法⽐较
1 SQL注⼊器⽅式
1.1 SqlInjector 注⼊器类
package;
import AbstractMethod;
import DefaultSqlInjector;
import InsertBatchSomeColumn;
import List;
/**
* @author liyue
* @date 2021/12/8 15:32
*/
public class SqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod>getMethodList(Class<?> mapperClass){
List<AbstractMethod> methodList =MethodList(mapperClass);
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}
nextint()方法1.2 MyBatisPlusConfig 配置类
package;
import MapperScan;
import Bean;
import Configuration;
import EnableTransactionManagement;
/**
* @author liyue
* @date 2021/12/8 15:31
*/
@EnableTransactionManagement
@Configuration
@MapperScan("ample.demo.mapper")
public class MyBatisPlusConfig {
@Bean
public SqlInjector sqlInjector(){
return new SqlInjector();
}
}
1.3 Mapper基础类
import List;
/**
* @author liyue
* @date 2021/12/8 15:35
*/
public interface MyBaseMapper<T>extends BaseMapper<T>{ /**
* 批量插⼊
* @param entityList
* @return
*/
Integer insertBatchSomeColumn(List<T> entityList);
}
1.4 UserMapper 继承MyBaseMapper类package;
import User;
/**
* @author liyue
* @date 2021/12/6 15:32
*/
public interface UserMapper extends MyBaseMapper<User>{ }
2 IService⽅式
2.1 BatchHandlerMapper 接⼝BatchHandlerMapper 接⼝继承IService
package;
import IService; import Dept;
/**
* @author liyue
* @date 2021/12/8 16:42
*/
public interface BatchHandlerMapper extends IService<Dept>{ }
2.2 DeptMapper 接⼝
import Dept;
/**
* @author liyue
* @date 2021/12/7 16:59
*/
public interface DeptMapper extends BaseMapper<Dept>{
}
2.3 BatchHandlerMapperImpl类
BatchHandlerMapperImpl类继承ServiceImpl和实现BatchHandlerMapper 接⼝
package;
import ServiceImpl;
import Dept;
import BatchHandlerMapper;
import DeptMapper;
import Service;
/**
* @author liyue
* @date 2021/12/8 16:44
*/
@Service
public class BatchHandlerMapperImpl extends ServiceImpl<DeptMapper, Dept>implements BatchHandlerMapper { }
3 测试
import QueryWrapper; import Dept;
import User;;
import DeptMapper;
import UserMapper;
import UserService;
import Autowired; import Service;
import ArrayList;
import List;
import Random;
/**
* @author liyue
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private BatchHandlerMapperImpl batchHandlerMapperImpl;
@Override
public void batchInsert(){
System.out.println("======sql注⼊器实现批量插⼊=======");
List<User> userList =new ArrayList<>();
for(int i =0;i <10000;i++){
Random random =new Random();
User user1 =new User();
user1.setAccount("paidaxing"+ Int(10000));
userList.add(user1);
}
long start = System.currentTimeMillis();
System.out.println("开始时间:"+ start);
userMapper.insertBatchSomeColumn(userList);
long end = System.currentTimeMillis();
System.out.println("结束时间:"+ end);
System.out.println("======插⼊完毕=======");
System.out.println("执⾏时间:"+(end - start));
System.out.println("======IService提供的批量插⼊=======");
List<Dept> deptList =new ArrayList<>();
for(int i =0;i <10000;i++){
Random random =new Random();
Dept dept =new Dept();
dept.setName("technology"+ Int(10000));
deptList.add(dept);
}
long start1 = System.currentTimeMillis();
System.out.println("开始时间:"+ start1);
batchHandlerMapperImpl.saveBatch(deptList);
long end1 = System.currentTimeMillis();
System.out.println("结束时间:"+ end1);
System.out.println("======插⼊完毕=======");
System.out.println("执⾏时间:"+(end1 - start1));
}
}
package;
import UserService;
import Assert;
import Test;
import RunWith;
import Autowired; import SpringBootTest; import SpringRunner;
import List;
/**
* @author liyue
* @date 2021/12/6 15:12
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseTest {
@Autowired
private UserService userService;
@Test
public void testBatchInsert(){
userService.batchInsert();
}
}
4 测试结果
4.1 ⼀万条数据
4.2 ⼀百万条数据

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。