新增xml执行效率测试:xml执行时间比sql稍慢一些,50000条数据插入时间约为2000毫秒左右,平均时间是sql的2倍左右。
先贴出执行效果(数字代表执行的时间,单位毫秒):
测试代码:
//测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class Test1 {
@Autowired
UsersMapper usersMapper;
@Autowired
SqlSessionFactory sqlSessionFactory;
public List list = new ArrayList<>();
@Before
public void getList() {
long start = System.currentTimeMillis();
Users user;
for (int i = 1; i <=50000 ; i++) {
user = new Users();
user.setId(i);
user.setName(“java”);
user.setAge(200);
user.setManagerId(222);
list.add(user);
}
System.out.println(“拼装数据 耗时:”+(System.currentTimeMillis()-start));
System.out.println(list.size());
}
@Test
public void batchInsert() {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
System.out.println(“batchInsert 插入开始========”);
long start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
mapper.insert(list.get(i));
if (i%5000==4999) {
sqlSession.flushStatements();
// sqlSession.commit();
// sqlSession.clearCache();
}
}
// sqlSession.commit();
// sqlSession.clearCache();
sqlSession.flushStatements();
System.out.println(“SqlSession 批量插入耗时:”+(System.currentTimeMillis()-start));
}
@Test
public void forEachInsert() {
System.out.println(“forEachInsert 插入开始========”);
long start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
usersMapper.insert(list.get(i));
}
System.out.println(“foreach 插入耗时:”+(System.currentTimeMillis()-start));
}
@Test
public void sqlInsert() {
System.out.println(“sql 插入开始========”);
long start = System.currentTimeMillis();
usersMapper.sqlInsert(list);
System.out.println(“sql 插入耗时:”+(System.currentTimeMillis()-start));
}
//xml批量插入
@Test
public void xmlInsert() {
System.out.println(“xmlInsert 批量插入开始========”);
long start = System.currentTimeMillis();
usersMapper.xmlBatchInsert(list);
System.out.println(“xmlInsert 批量插入耗时:”+(System.currentTimeMillis()-start));
}
}
//sql插入相关类
@Repository
public interface UsersMapper extends BaseMapper {
@InsertProvider(type = UsersProvider.class, method = “insertListSql”)
public void sqlInsert(List list);
public void xmlBatchInsert(@Param(“list”) List list);
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新