Mybatis如何执行批量操作?

1、设置SqlSession为批量操作类型

sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch);
    try {
     namemapper mapper = sqlsession.getmapper(namemapper.class);
     for (string name : names) {
         mapper.insertname(name);
     }
     sqlsession.commit();
    }catch(Exception e){
     e.printStackTrace();
     sqlSession.rollback(); 
     throw e; 
    }
     finally {
         sqlsession.close();
    }

然后执行insert方法即可

2、通过foreach 遍历需要操作的值


        insert into user(name)
        values
        
            (#{item.name,jdbcType=VARCHAR}
        
    

 

你可能感兴趣的:(Mybatis)