@Test
public void batch() throws IOException {
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
//读取mybatis 配置文件创建sqlsessionFactory
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
//获取sqlsession
SqlSession sqlSession=sqlSessionFactory.openSession(ExecutorType.BATCH,false); //默认 single
//获取对应mapper
userMapper mapper=sqlSession.getMapper(userMapper.class); user userobj1=new user();
userobj1.setId(1);
userobj1.setName("11");
mapper.insert(userobj1); user userobj2=new user();
userobj2.setId(1);
userobj2.setName("11");
mapper.insert(userobj2); sqlSession.commit();
}

mybatis  有三种执行器

package org.apache.ibatis.session;

public enum ExecutorType {
SIMPLE, //默认 普通执行器
REUSE, // 重用预处理语句 (prepared statement)
BATCH; //重用语句并执行批量更新 private ExecutorType() {
}
}
05-27 01:17