@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();
}
There are three actuators mybatis
Package org.apache.ibatis.session; public enum ExecutorType { the SIMPLE, // default common actuator REUSE, // reuse prepared statements (Prepared Statement) BATCH; // perform bulk updating and reusing statements Private ExecutorType () { } }