MyBatis批量插入
2016-03-01 16:50
316 查看
MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。
MyBatis所说是轻量级的ORM框架,在网上看过一个测试报告,感觉相比于Hibernate来说,优势并不明显。
下面说一下比较有趣的现象,根据MyBatis的官方文档,在获得sqlSession时,它有为批量更新而专门准备的:
Java代码
session = sessionFactory.openSession();//用于普通update
session = sessionFactory.openSession(ExecutorType.BATCH, true);//用于批量update
一般来说,对MYSQL数据库批量操作时速度取决于,是为每一个处理分别建立一个连接,还是为这一批处理一共建立一个连接。按MyBatis的手册说明,选择ExecutorType.BATCH意味着,获得的sqlSession会批量执行所有更新语句。不过我测试了一下,批量插入1000条数据,发觉ExecutorType.BATCH方式的效率居然比普通的方式差很多 。我测试用的Mapper中的insert配置如下,再用for循环插入1000条记录:
Xml代码
<insert id="insert" parameterType="sdc.mybatis.test.Student">
<!-- WARNING - @mbggenerated This element is automatically generated by
MyBatis Generator, do not modify. This element was generated on Mon May 09
11:09:37 CST 2011. -->
insert into student (id, name, sex,
address, telephone, t_id
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
<
4000
li style="font-size:1em;margin:0px 0px 0px 38px;padding:0px 0px 0px 10px;border-left-width:1px;border-left-style:solid;border-left-color:rgb(209,215,220);line-height:18px;background-color:rgb(250,250,250);">
)
</insert>
1、 我不清楚原因在哪里, 就配置了MyBatis的log4j,想查看下日志。下载了log4j.jar和commons-logging.jar并配置到项目的类路径,然后在代码路径下新建文件log4j.properties,内容如下:
Xml代码
log4j.rootLogger=DEBUG, stdout
# SqlMap logging configuration...
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG
log4j.logger.com.ibatis.common.util.StopWatch=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2、然后再次测试普通的sqlSession,发现日志内容中虽然插入了1000条数据,但只新建了一次连接,最后又关闭了该连接(日志如下)。也就是说MyBatis中的普通sqlSession好像已经对批量插入默认是一次连接中完成,那么还提供ExecutorType.BATCH方式干什么,况且该方式好像效率也不行,或者是我使用ExecutorType.BATCH方式不对??3、最后一点是关于数据库批量插入时sql语句级的优化,我特意测试了两种方式,在StudentMapper中配置了两种insert模式。第一种对应insert
value1,insert value2,,,,;第二种对应insert values (value1, value2,....)。发现后者果然比前者快很多啊。下面是两种insert模式,及测试结果对应图:
Xml代码
DEBUG [main] - Created connection 3502256.
DEBUG [main] - ooo Connection Opened
DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
DEBUG [main] - ==> Parameters: 新人0(String), male(String), addr0(String), dd(String), 3(Integer)
DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
DEBUG [main] - ==> Parameters: 新人1(String), male(String),
...............
...............
DEBUG [main] - xxx Connection Closed
DEBUG [main] - Returned connection 3502256 to pool.
3、最后一点是关于数据库批量插入时sql语句级的优化,我特意测试了两种方式,在StudentMapper中配置了两种insert模式。第一种对应insert value1,insert value2,,,,;第二种对应insert values (value1, value2,....)。发现后者果然比前者快很多啊。下面是两种insert模式,及测试结果对应图:
Xml代码
<!-- 在外部for循环调用一千次 -->
<insert id="insert" parameterType="sdc.mybatis.test.Student">
insert into student (id, name, sex,
address, telephone, t_id
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
)
</insert>
<!-- 批量 ,传入一个长度为1000的list -->
<insert id="insertBatch" >
insert into student ( <include refid="Base_Column_List" /> )
values
<foreach collection="list" item="item" index="index" separator=",">
(null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
</foreach>
</insert>
附录:
MyBatis配置文件的DTD文件(与Ibatis3不同):http://mybatis.org/dtd/
MyBatis的中文手册:http://mybatis.googlecode.com/files/MyBatis%203%20User%20Guide%20Simplified%20Chinese.pdf
MyBatis所说是轻量级的ORM框架,在网上看过一个测试报告,感觉相比于Hibernate来说,优势并不明显。
下面说一下比较有趣的现象,根据MyBatis的官方文档,在获得sqlSession时,它有为批量更新而专门准备的:
Java代码
session = sessionFactory.openSession();//用于普通update
session = sessionFactory.openSession(ExecutorType.BATCH, true);//用于批量update
一般来说,对MYSQL数据库批量操作时速度取决于,是为每一个处理分别建立一个连接,还是为这一批处理一共建立一个连接。按MyBatis的手册说明,选择ExecutorType.BATCH意味着,获得的sqlSession会批量执行所有更新语句。不过我测试了一下,批量插入1000条数据,发觉ExecutorType.BATCH方式的效率居然比普通的方式差很多 。我测试用的Mapper中的insert配置如下,再用for循环插入1000条记录:
Xml代码
<insert id="insert" parameterType="sdc.mybatis.test.Student">
<!-- WARNING - @mbggenerated This element is automatically generated by
MyBatis Generator, do not modify. This element was generated on Mon May 09
11:09:37 CST 2011. -->
insert into student (id, name, sex,
address, telephone, t_id
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
<
4000
li style="font-size:1em;margin:0px 0px 0px 38px;padding:0px 0px 0px 10px;border-left-width:1px;border-left-style:solid;border-left-color:rgb(209,215,220);line-height:18px;background-color:rgb(250,250,250);">
)
</insert>
1、 我不清楚原因在哪里, 就配置了MyBatis的log4j,想查看下日志。下载了log4j.jar和commons-logging.jar并配置到项目的类路径,然后在代码路径下新建文件log4j.properties,内容如下:
Xml代码
log4j.rootLogger=DEBUG, stdout
# SqlMap logging configuration...
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG
log4j.logger.com.ibatis.common.util.StopWatch=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2、然后再次测试普通的sqlSession,发现日志内容中虽然插入了1000条数据,但只新建了一次连接,最后又关闭了该连接(日志如下)。也就是说MyBatis中的普通sqlSession好像已经对批量插入默认是一次连接中完成,那么还提供ExecutorType.BATCH方式干什么,况且该方式好像效率也不行,或者是我使用ExecutorType.BATCH方式不对??3、最后一点是关于数据库批量插入时sql语句级的优化,我特意测试了两种方式,在StudentMapper中配置了两种insert模式。第一种对应insert
value1,insert value2,,,,;第二种对应insert values (value1, value2,....)。发现后者果然比前者快很多啊。下面是两种insert模式,及测试结果对应图:
Xml代码
DEBUG [main] - Created connection 3502256.
DEBUG [main] - ooo Connection Opened
DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
DEBUG [main] - ==> Parameters: 新人0(String), male(String), addr0(String), dd(String), 3(Integer)
DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
DEBUG [main] - ==> Parameters: 新人1(String), male(String),
...............
...............
DEBUG [main] - xxx Connection Closed
DEBUG [main] - Returned connection 3502256 to pool.
3、最后一点是关于数据库批量插入时sql语句级的优化,我特意测试了两种方式,在StudentMapper中配置了两种insert模式。第一种对应insert value1,insert value2,,,,;第二种对应insert values (value1, value2,....)。发现后者果然比前者快很多啊。下面是两种insert模式,及测试结果对应图:
Xml代码
<!-- 在外部for循环调用一千次 -->
<insert id="insert" parameterType="sdc.mybatis.test.Student">
insert into student (id, name, sex,
address, telephone, t_id
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
)
</insert>
<!-- 批量 ,传入一个长度为1000的list -->
<insert id="insertBatch" >
insert into student ( <include refid="Base_Column_List" /> )
values
<foreach collection="list" item="item" index="index" separator=",">
(null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
</foreach>
</insert>
附录:
MyBatis配置文件的DTD文件(与Ibatis3不同):http://mybatis.org/dtd/
MyBatis的中文手册:http://mybatis.googlecode.com/files/MyBatis%203%20User%20Guide%20Simplified%20Chinese.pdf
相关文章推荐
- 在父页面查找html元素
- 提高编码速度的一些myeclipse快捷键
- spring 学习小结10—泛型依赖注入
- 面试题之算法与编程
- iOS多线程的初步研究(四、五、六)--NSTimer--NSURLConnection子线程中运行 ---- NSOperation
- 安装程序常用脚本
- java的interface总结
- LeetCode题解:Binary Tree Preorder Traversal
- java redis使用参考文档
- 我的软件工程目标
- 我的软件工程课目标
- java实现获取字符串strSub在父字符串strFather中出现的次数
- linux下vim学习笔记
- 基于VS平台的多维数据集的建立
- java运行jar文件中某个类
- 一些关于Console的API函数
- 浅议Github的注册和使用
- 作业
- hdoj The first place of 2^n 3215 (数学技巧&预处理)
- spark streaming kafka1.4.1中的低阶api createDirectStream使用总结