Mybatis批量操作数据的方法

方法1:

使用for循环在java代码中insert (不推荐)

方法2:

使用 在Mapper.xml当中使用 foreach循环的方式进行insert

PersonDao.java文件

    public interface PersonDao {
        
        //这个是使用 foreach方式的mybatis 批量操作
        public void batchInsert(@Param("list")List<Person>list);
     
    }


PersonDao.xml

复制代码
 <insert id="batchInsert" >
             insert into person (
                 id,
                person_name,
                birthday,
                address,
                age,
                gender    
             )
             values
             <foreach collection="list" item="list" index="index" separator="," >
             (
                 #{list.id},
                 #{list.person_name},
                 #{list.birthday},
                #{list.address},
                #{list.age},
                #{list.gender}    
             )
             </foreach>
         </insert>
复制代码



主测试函数:

复制代码
 public static void main5(String[] args) throws Exception {
            SqlSession session = getSqlSession();
            
            PersonDao pd = session.getMapper( PersonDao.class );
        
            List<Person>pl = new ArrayList<Person>();
            Person p1 = new Person(); p1.setPerson_name("哈哈哈吧");   p1.setAddress("深圳"); p1.setBirthday(new Date());
            Person p2 = new Person(); p2.setPerson_name("您好");  p2.setAddress("上海"); p2.setBirthday(new Date());
            Person p3 = new Person(); p3.setPerson_name("我是张伟");  p3.setAddress("广州"); p3.setBirthday(new Date());
            pl.add(p1);
            pl.add(p2);
            pl.add(p3);
            
            pd.batchInsert(pl);
            
            System.out.println("完成batchInsert");
            
            session.commit();
            
            session.close();
            //pd.batchInsert( pl );
        }
复制代码

方法3:
Mybatis内置的 ExecutorType有三种,默认是Simple,该模式下它为每个语句的执行创建一个新的预处理语句,

单条提交sql,而batch模式 重复使用已经预处理的语句,并且批量执行所有更新语句,显然 batch的性能更优,

中间在提交的过程中还可以设置等待时间,避免数据库压力过大。(获取batch模式下的session)

  

复制代码
 public static void main(String[] args) throws Exception {
            
            InputStream in = new FileInputStream( "F:\myeclipse_workspace\mybatisGeneratortest\src\test\resources\mybatis-config.xml");
            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = ssfb.build(in);
            
            SqlSession session = factory.openSession( ExecutorType.BATCH,false );
            
            PersonDao pd = session.getMapper( PersonDao.class );
            
            int size = 100000;
            
            try {
                
                for(int i=0;i<size;i++){
                    Person p = new Person();
                    p.setPerson_name("小明"); p.setBirthday(new Date());
                    pd.insertPeron(p);
                    if( i % 100 == 0 || i == size - 1 ){ //加上这样一段代码有好处,比如有100000条记录,每超过 100 条提交一次,中间等待10ms,可以避免一次性提交过多数据库压力过大
                        session.commit();
                        session.clearCache();
                        Thread.sleep(10);
                    }
                }
                
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
 
原文地址:https://www.cnblogs.com/nyhhd/p/12592609.html