日期:2014-05-16 浏览次数:20697 次
今天 在网站上看到了一篇博客,说的是mybatis批量插入的有一些问题,于是自己做了一个简单的demo,做了下测试
mybatis测试代码
?
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.mybatis.config.User" alias="User" /> </typeAliases>package com.mybatis.config; <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /><!--事务管理驱动 --> <dataSource type="POOLED"><!-- 数据源配置 --> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="UserDaoMapper.xml"/> </mappers> </configuration> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mybatis 映射文件 --> <mapper namespace="com.mybatis.config.UserDao"> <cache readOnly="true" /> <insert id="insert" parameterType="User" > insert into user(username,password,comment) values(#{username},#{password},#{comment}) </insert> </mapper>
?
?java测试代码:
?
package com.mybatis.config; import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.log4j.xml.DOMConfigurator; /** * mybatis测试 * @author CHENLEI * */ public class UserDaoTest { public static void main(String[]args){ try { userDaoTest(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void userDaoTest() throws Exception { DOMConfigurator.configure("res/log4j.xml"); File file=Resources.getResourceAsFile("mybatisBase.xml"); SqlSessionFactoryBuilder Factorybuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory=Factorybuilder.build(new FileInputStream(file)); SqlSession session1 = factory.openSession();//普通的操作 SqlSession session2 = factory.openSession(ExecutorType.BATCH, false);//是否自动提交事务 UserDao userDao1 = session1.getMapper(UserDao.class);// UserDao userDao2 = session2.getMapper(UserDao.class);// User user = new User(); user.setUsername("test"); user.setPassword("123456"); user.setComment("comment"); try{ long t1=System.currentTimeMillis(); for(int i=0;i<1000;i++){ userDao2.insert(user); } System.out.println(System.currentTimeMillis()-t1+"ms"); }finally{ // session1.commit(); // session1.close(); } } }
?运行结果:(结果来自三次测试的结果,取的一个范围,因为外界因素,下同)
?1000次:632---650ms
?
下面是传统的jdbc:
?
?
?
package com.mybatis.config; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class commonjdbcBatch { /**