Mybatis-数据插入

Posted floatthy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis-数据插入相关的知识,希望对你有一定的参考价值。

 

传统jdbc数据插入

 

1、在Java代码for循环插入

executeUpdate()

2、批处理方法addBatch(Statement、Prestatement)

addBatch()

executeUpdate()

3、缺点:

1、  插入速度非常慢,需要频繁获取session

2、  代码和SQL耦合大,非业务代码多

 

Mybatis数据插入

 

1、foreach标签(使用较多)

只写一条SQL语句,通过拼接SQL语句添加

2、allowMultiQueries=true(mysql连接属性)+foreach标签

写多条SQL语句,通过MySQL批量添加

3、ExecutorType(常用)

只写一条的SQL语句,通过mybatis批量添加

  在openSession中添加参数ExecutorType.BATCH

以上是关于Mybatis-数据插入的主要内容,如果未能解决你的问题,请参考以下文章