foreach 标签插入数据量大时,明显影响效率,个人倾向以下配置
applicationcontext 配置文件
<!-- 批量执行的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
Java代码文章来源:https://www.toymoban.com/news/detail-531195.html
public class TestServiceImpl implements ITestService{
@Autowired
private SqlSession sqlSession;
public void test(){
List<Student> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
Student student = new Student();
student.setId(Utility.getUUID());
student.setIsDelete("0");
list.add(student);
}
//开始批量入库
SysTreeDicMapper mapper = sqlSession.getMapper(Student Mapper.class);
list.stream().forEach(o -> {
mapper.insertSelective(o);
});}
}
mybatis文件没什么可说的,就是普通单条插入的<insert> 标签即可文章来源地址https://www.toymoban.com/news/detail-531195.html
到了这里,关于mybatis配置批量执行SQL的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!