package sss.mybatis_1;

 import java.io.InputStream;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.sql.*; import com.mysql.jdbc.ConnectionImpl;
import com.mysql.jdbc.JDBC4Connection;
import com.mysql.jdbc.JDBC4MySQLConnection;
//import org.apache.commons.lang.time.StopWatch;
import com.sun.javafx.Logging;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.DataSourceConnectionFactory;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import javax.sql.DataSource; /**
* Created by fenggq on 2016/3/2.
*/
public class PerformanceTest { public class Record { public int Times; public long OpenSessionEveryTime_XMLMapper;
public long OpenSessionOneTime_XMLMapper;
public long OpenSessionOneTime_AnnotionMapper;
public long OpenSessionEveryTime_AnnotionMapper;
public long JDBCOneTime;
public long JDBCEveryTime;
public long BDCPOneTime; } public static void main(String[] args) throws Exception {
PerformanceTest performanceTest = new PerformanceTest();
performanceTest.Test();
} private void Test() throws Exception {
{ //org.apache.ibatis.logging.LogFactory.useLog4J2Logging();
// org.apache.ibatis.logging.LogFactory.useJdkLogging(); InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = null;
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
sqlSessionFactory = sqlSessionFactoryBuilder.build(is); is.close();
StopWatch sw1 = new StopWatch(); fgq.mybatis_1.UI ui;
UI2 ui2; List<?> result; SqlSession sqlSession = null; //region 为了公平,SessionFactory预先加载
sqlSession = sqlSessionFactory.openSession();
ui = sqlSession.selectOne("selectbutton", "buttonAdd");
UI2Mapper ui2Mapper = sqlSession.getMapper(UI2Mapper.class);
sqlSession.close();
//endregion Integer times = 10;
ArrayList<Integer> AllTime = new ArrayList<Integer>();
ArrayList<Record> records = new ArrayList<Record>();
Record record; AllTime.add(1);
AllTime.add(2);
AllTime.add(10);
AllTime.add(100);
AllTime.add(200);
AllTime.add(1000); for (int ii = 0; ii < AllTime.size(); ii++) { times = AllTime.get(ii);
record = new Record();
records.add(record);
record.Times = times; /////////////////////////////////////////////// sw1.start();
for (Integer i = 0; i < times; i++) {
sqlSession = sqlSessionFactory.openSession();
ui = sqlSession.selectOne("selectbutton", "buttonAdd" + i);
sqlSession.close();
}
record.OpenSessionEveryTime_XMLMapper = sw1.getTime();
sw1.stop();
sw1.reset(); //////////////////////////////////////////
sw1.start();
sqlSession = sqlSessionFactory.openSession();
for (Integer i = 0; i < times; i++) { ui = sqlSession.selectOne("selectbutton", "buttonAdd" + i); }
sqlSession.close(); record.OpenSessionOneTime_XMLMapper = sw1.getTime();
sw1.stop();
sw1.reset(); ////////////////////////////////////////// sw1.start();
sqlSession = sqlSessionFactory.openSession();
ui2Mapper = sqlSession.getMapper(UI2Mapper.class); for (Integer i = 0; i < times; i++) {
ui2 = ui2Mapper.SelectOne("buttonAdd" + i);
}
sqlSession.close();
//System.out.println("OpenSessionOneTime_AnnotionMapper:" + sw1.getTime());
record.OpenSessionOneTime_AnnotionMapper = sw1.getTime();
sw1.stop();
sw1.reset();
////////////////////////////////////////// sw1.start(); for (Integer i = 0; i < times; i++) {
sqlSession = sqlSessionFactory.openSession();
ui2Mapper = sqlSession.getMapper(UI2Mapper.class);
ui2 = ui2Mapper.SelectOne("buttonAdd" + i);
sqlSession.close();
} //System.out.println("OpenSessionEveryTime_AnnotionMapper:" + sw1.getTime());
record.OpenSessionEveryTime_AnnotionMapper = sw1.getTime();
sw1.stop();
sw1.reset();
////////////////////////////////////////// // 驱动程序名
String driver = "com.mysql.jdbc.Driver";
String uri = "jdbc:mysql://*******:*********/*********"; Properties p = new Properties(); p.setProperty("username", "root");
p.setProperty("password", "oracle"); Connection connection; sw1.reset();
sw1.start();
connection = DriverManager.getConnection(uri, "root", "oracle");
for (Integer i = 0; i < times; i++) { PreparedStatement s = connection.prepareStatement("select e.button_id,e.text_value\n" +
" from kingkoo_wms.ui_button e\n" +
" WHERE e.button_id=?"); s.setString(1, "buttonAdd" + i);
ResultSet rs = s.executeQuery(); while (rs.next()) { ui = new fgq.mybatis_1.UI();
ui.setButton_id(rs.getString("button_id"));
ui.setText_value(rs.getString("text_value")); } }
connection.close(); record.JDBCOneTime = sw1.getTime();
sw1.stop();
sw1.reset(); ////////////////////////////////////////////////////////////////// sw1.start(); for (Integer i = 0; i < times; i++) {
connection = DriverManager.getConnection(uri, "root", "oracle"); PreparedStatement s = connection.prepareStatement("select e.button_id,e.text_value\n" +
" from kingkoo_wms.ui_button e\n" +
" WHERE e.button_id=?"); s.setString(1, "buttonAdd" + i);
ResultSet rs = s.executeQuery(); while (rs.next()) { ui = new fgq.mybatis_1.UI();
ui.setButton_id(rs.getString("button_id"));
ui.setText_value(rs.getString("text_value")); }
connection.close();
} record.JDBCEveryTime = sw1.getTime();
sw1.stop();
sw1.reset(); ////////////////////////////////////////////////////////////////// org.apache.commons.dbcp.BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://*******:*****/*******");
dataSource.setUsername("root");
dataSource.setPassword("oracle"); org.apache.commons.dbcp.DataSourceConnectionFactory dataSourceConnectionFactory = null; dataSourceConnectionFactory = new DataSourceConnectionFactory(dataSource);
connection = dataSourceConnectionFactory.createConnection(); sw1.start();
for (Integer i = 0; i < times; i++) { PreparedStatement s = connection.prepareStatement("select e.button_id,e.text_value\n" +
" from kingkoo_wms.ui_button e\n" +
" WHERE e.button_id=?"); s.setString(1, "buttonAdd" + i);
ResultSet rs = s.executeQuery(); while (rs.next()) { ui = new fgq.mybatis_1.UI();
ui.setButton_id(rs.getString("button_id"));
ui.setText_value(rs.getString("text_value")); } } connection.close(); record.BDCPOneTime = sw1.getTime(); sw1.stop();
sw1.reset(); } System.out.println(String.format("%s %s %s %s %s %s %s %s", "Times", "BDCPOneTime", "JDBCEveryTime", "JDBCOneTime", "OpenSessionEveryTime_AnnotionMapper", "OpenSessionEveryTime_XMLMapper"
, "OpenSessionOneTime_AnnotionMapper", "OpenSessionOneTime_XMLMapper"
)); for (int i = 0; i < records.size(); i++) {
record = records.get(i); System.out.println(String.format("%d %d %d %d %d %d %d %d"
, record.Times, record.BDCPOneTime, record.JDBCEveryTime, record.JDBCOneTime, record.OpenSessionEveryTime_AnnotionMapper, record.OpenSessionEveryTime_XMLMapper
, record.OpenSessionOneTime_AnnotionMapper, record.OpenSessionOneTime_XMLMapper
)); } } }
}
<mapper namespace="org.mybatis.example.BlogMapper">
<resultMap id="BlogMapper" type="fgq.mybatis_1.UI">
<id column="button_id" property="button_id" ></id>
</resultMap>
<select id="selectbutton" resultType="fgq.mybatis_1.UI" useCache="false">
select e.button_id,e.text_value
from mysql_test.ui_button e
WHERE e.button_id=#{id} </select> </mapper>

xml mapper

public interface UI2Mapper {

    @Select("select e.button_id,e.text_value from mysql_test.ui_button e WHERE e.button_id=#{id}")
@Options( useCache = false)
UI2 SelectOne(String id); }

  

从结果看,mybatis方式的select比原生态的代码,速度慢两倍,相比dapper而言差距还是很大的。 (https://github.com/StackExchange/dapper-dot-net)

不过两者可比性不大,毕竟不是同一种东西,功能也不一样。

mybatis研究:select性能对比的更多相关文章

  1. 再看ExpressionTree,Emit,反射创建对象性能对比

    [前言] 前几日心血来潮想研究着做一个Spring框架,自然地就涉及到了Ioc容器对象创建的问题,研究怎么高性能地创建一个对象.第一联想到了Emit,兴致冲冲写了个Emit创建对象的工厂.在做性能测试 ...

  2. 转://Oracle 复合压缩索引场景及性能对比

    摘要:今天为什么提到这个话题,出于一个偶然,一个同事在优化新开发的系统时向我请教如何添加复合压缩索引的问题.我总结了一下,问题有三. 第一:需不需要压缩 第二:对第几列压缩 第三:性能对比,选出最优 ...

  3. php+mysql预查询prepare 与普通查询的性能对比

    prepare可以解决大访问量的网站给数据库服务器所带来的负载和开销,本文章通过实例向大家介绍预查询prepare与普通查询的性能对比,需要的朋友可以参考一下. 实例代码如下: <?php cl ...

  4. 不同Framework下StringBuilder和String的性能对比,及不同Framework性能比(附Demo)

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 环境搭建 测试用例 MSDN说明 ...

  5. java数据库连接池性能对比

    这个测试的目的是验证当前常用数据库连接池的性能. testcase Connection conn = dataSource.getConnection(); PreparedStatement st ...

  6. 自己写的轻量级PHP框架trig与laravel5.1,yii2性能对比

    看了下当前最热门的php开发框架,想对比一下自己写的框架与这些框架的性能对比.先看下当前流行框架的投票情况. 看结果对比,每个测试脚本做了一个数据库的联表查询并进行print_r输出,查询的sql语句 ...

  7. SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比

    原文:SQL点滴10-使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比 今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章 ...

  8. background-image 与 img 动画性能对比

    开发H5常常会用到滑屏,目前大部分滑屏插件都是通过控制页面的transform属性来实现.尽管如此,我总是发现自己的H5滑动起来就是不如网上一些优秀案例流畅,表现为滑动动画会出现卡顿.跳帧. 后来我发 ...

  9. Net Core下多种ORM框架特性及性能对比

    在.NET Framework下有许多ORM框架,最著名的无外乎是Entity Framework,它拥有悠久的历史以及便捷的语法,在占有率上一路领先.但随着Dapper的出现,它的地位受到了威胁,本 ...

随机推荐

  1. 用 Python 排序数据的多种方法

    用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...

  2. GIT 操作

    1. 查看某个文件某次提交修改的内容 git show commitid  a.txt  2. git rm 和 git rm --cached  当我们需要删除暂存区或分支上的文件, 同时工作区也不 ...

  3. reset 单个文件 回退

    git将单个文件恢复到历史版本的正确方法如下: git reset commit_id 文件路径 git checkout -- 文件路径

  4. Java中MyEclipse快捷键整理

    ************************************  MyEclipse 快捷键1(CTRL) ************************************ Ctrl ...

  5. C++ 利用 libxl 将 Excel 文件转化为 Xml 文件

    在游戏开发工作中,策划和运营一般会用Excel来编写配置文件,但是程序读取配置,最方便的还是xml文件.所以最好约定一个格式,然后在二者之间做一个转化. 本文利用libxl来读取Excel文件,利用 ...

  6. PostgreSQL 添加自定义变量

    http://dba.stackexchange.com/questions/97095/set-session-custom-variable-to-store-user-id set sessio ...

  7. Jmeter参数化

    使用jmeter时有时需要访问不同的uri,或POST不同的数据.这时候可以对数据参数化,将数据写入文件,再从文件读取.下面举个例子说明如何使用CSV Data Set Config参数化. 这里有两 ...

  8. js暂停的函数

    // numberMillis 毫秒 function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() ...

  9. ionic build android error when download gradle

    这里我遇到一个问题,当用  ionic build android 的时候,无数次build,无数次失败的时候,我真想骂一句,NND的GNF,我又想起武大的臭鸡蛋,是的,该丢,发明这种东西的人,难道不 ...

  10. Python之路-python(html、css)

    html: 概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样 ...