日期:2014-05-16  浏览次数:20459 次

iBATIS3与JDBC性能对比
spring3.0昨天已经发布了第一个正式版本,看了下API DOC没有对于iBATIS3的支持,还是停留在iBATIS2.3上。进来一直在研究iBATIS,所以在此对iBATIS3和JDBC做了一个测试。同样是想本地的MySql数据库插入10W数据,测试结果以及代码如下:
1.JDBC
Connection con = null;
		PreparedStatement sta = null;
//		Statement sta = null;
		String sql = "INSERT INTO dbbean VALUES('na','nb','nc','nd','ne','nf','ng','nh','ni','nj','nk','nl','nm');";
		try {
			long start = System.currentTimeMillis();
			Class.forName("com.mysql.jdbc.Driver"); 
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
			con.setAutoCommit(false);
			sta = con.prepareStatement(sql);
			System.out.println("连接数据库耗时: "+(System.currentTimeMillis()-start)+"ms");
			for(int i=0;i<100000;i++){
//				sta.addBatch(sql);
				sta.execute();
			}
			con.commit();
			System.out.println("总共用时: "+(System.currentTimeMillis()-start)+"ms");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

2.iBATIS3

public class IBatisDAO{
	
	public SqlSessionFactory sqlMapper;
	
	public String resource = "ibatis_conf.xml";

	public void init(){
		
		Reader reader;
		try {
			reader = Resources.getResourceAsReader(resource);
			sqlMapper = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public SqlSessionFactory getSqlMapper() {
		this.init();
		return sqlMapper;
	}
}

ibatis_mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="net.unison.ivr">
	<insert id="insert">
		insert into dbbean
		values('na','nb','nc','nd','ne','nf','ng','nh','ni','nj','nk','nl','nm')
	</insert>
</mapper>

ibatis_conf.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
	<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/test" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="ibatis_mapper.xml" />
	</mappers>
</configuration>

测试结果:
JDBC耗时:20563ms
iBATIS3耗时:26250ms
结论:看来还是最纯粹的数据库操作性能最好,对于一下orm框架,可能耗时在对xml的文件解析等方面。
1 楼 yzj520hyy 2010-04-27  
结论的解释说不过去吧,相差共6秒,解析XML用了6秒啊?
2 楼 yaohao803 2012-03-23  
在mybatis中加入batch应该性能会提高很多吧!
试试改成:
<insert id="insert" statementType="PREPARED">  
        insert into dbbean  
        values('na','nb','nc','nd','ne','nf','ng','nh','ni','nj','nk','nl','nm')  
    </insert>
3 楼 fuchao01 2012-03-23  
恩,这个是新性能,那时还没有呢