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. (转)Silverlight控件关系理解

    原文地址http://www.cnblogs.com/Joetao/articles/1899664.html 本篇学习了Silverlight中的控件继承关系,了解控件的继承关系对应我们操作控件,使 ...

  2. 最简单的Android教程之自定义控件

    新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...

  3. Notes of the scrum meeting(12.5)

    meeting time:18:00~18:30p.m.,December 5th,2013 meeting place:3号公寓一层 attendees: 顾育豪                   ...

  4. SQL Server :理解IAM 页

    原文:SQL Server :理解IAM 页 在以前的文章里,我们讨论了数据页,GAM和SGAM,还有PFS页.今天我们一起来讨论下索引分配映射(Index Allocation Map:IAM)页. ...

  5. 查找oracle自己用户的表

    查找oracle自己用户的表 select table_name from user_tables;

  6. [NOIP2017] 宝藏 【树形DP】【状压DP】

    题目分析: 这个做法不是最优的,想找最优解请关闭这篇博客. 首先容易想到用$f[i][S][j]$表示点$i$为根,考虑$S$这些点,$i$的深度为$j$情况的答案. 转移如下: $f[i][S][j ...

  7. train_test_split数据切分

    train_test_split 数据切分 格式: X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_d ...

  8. P4781 【模板】拉格朗日插值

    P4781 [模板]拉格朗日插值 证明 :https://wenku.baidu.com/view/0f88088a172ded630b1cb6b4.html http://www.ebola.pro ...

  9. 基于Java visualvm的可视化监控的使用

    1 Java visualVM可视化监控工具打开方式 ctrl + r 快捷键调出运行窗口,输出 jvisualvm指令,即可打开java visualVM工具.详情如下图: 2 Java Visua ...

  10. Android 网络编程 API笔记 - java.net 包相关 接口 api

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...