1 创建数据库表
-- phpMyAdmin SQL Dump -- version 4.2.11 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: 2016-08-02 18:13:50 -- 服务器版本: 5.6.21 -- PHP Version: 5.6.3 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `mybatis` -- -- -------------------------------------------------------- -- -- 表的结构 `Student` -- CREATE TABLE IF NOT EXISTS `Student` ( `id` int(10) NOT NULL, `name` varchar(256) NOT NULL, `age` int(4) NOT NULL ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; -- -- 转存表中的数据 `Student` -- INSERT INTO `Student` (`id`, `name`, `age`) VALUES (1, ‘zhansan‘, 20); -- -- Indexes for dumped tables -- -- -- Indexes for table `Student` -- ALTER TABLE `Student` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `Student` -- ALTER TABLE `Student` MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
2. 测试代码如下
package mybatisInsertDataDemo;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import mybatisInsertDataDemo.Student;
import mybatisInsertDataDemo.demo;
public class demo {
	
	public static void main(String[] args) {
		
	    String resource = "mybatisInsertDataDemo/conf.xml";
		InputStream is = demo.class.getClassLoader().getResourceAsStream(resource);
		
		if(null == is)
		{
			System.out.println("inputstream is null");
			return;
		}
		
		
		 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		
	     
	     
	     
	     long start = System.currentTimeMillis();
	     
	     for(int i=0;i<10000;i++)
	     {
	    	 SqlSession session = sessionFactory.openSession();
	 	    
		     String statement = "mybatisInsertDataDemo.studentMapper.insertStudent";
	    	  Student s = new Student();
		     s.setAge(20);
		     s.setName("lisi");
		     
		     session.insert(statement, s);
		     
		     session.close();
	     }
	     
	     System.out.println("waste time  "+(System.currentTimeMillis()-start));
	     
	     
	}
}
3. 打开与关闭连接池 配置
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="UNPOOLED">  <!--UNPOOLED 是关闭连接池,POOLED 是打开连接池-->
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="mybatisInsertDataDemo/studentMapper.xml"/>
    </mappers>
    
</configuration>
4. 对比结果
插入一万条数据
打开连接池: 6s
关闭连接池: 36s
原文:http://www.cnblogs.com/mtour/p/5735242.html