mybatis研究:select性能对比
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性能对比的更多相关文章
- 再看ExpressionTree,Emit,反射创建对象性能对比
[前言] 前几日心血来潮想研究着做一个Spring框架,自然地就涉及到了Ioc容器对象创建的问题,研究怎么高性能地创建一个对象.第一联想到了Emit,兴致冲冲写了个Emit创建对象的工厂.在做性能测试 ...
- 转://Oracle 复合压缩索引场景及性能对比
摘要:今天为什么提到这个话题,出于一个偶然,一个同事在优化新开发的系统时向我请教如何添加复合压缩索引的问题.我总结了一下,问题有三. 第一:需不需要压缩 第二:对第几列压缩 第三:性能对比,选出最优 ...
- php+mysql预查询prepare 与普通查询的性能对比
prepare可以解决大访问量的网站给数据库服务器所带来的负载和开销,本文章通过实例向大家介绍预查询prepare与普通查询的性能对比,需要的朋友可以参考一下. 实例代码如下: <?php cl ...
- 不同Framework下StringBuilder和String的性能对比,及不同Framework性能比(附Demo)
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 环境搭建 测试用例 MSDN说明 ...
- java数据库连接池性能对比
这个测试的目的是验证当前常用数据库连接池的性能. testcase Connection conn = dataSource.getConnection(); PreparedStatement st ...
- 自己写的轻量级PHP框架trig与laravel5.1,yii2性能对比
看了下当前最热门的php开发框架,想对比一下自己写的框架与这些框架的性能对比.先看下当前流行框架的投票情况. 看结果对比,每个测试脚本做了一个数据库的联表查询并进行print_r输出,查询的sql语句 ...
- SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
原文:SQL点滴10-使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比 今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章 ...
- background-image 与 img 动画性能对比
开发H5常常会用到滑屏,目前大部分滑屏插件都是通过控制页面的transform属性来实现.尽管如此,我总是发现自己的H5滑动起来就是不如网上一些优秀案例流畅,表现为滑动动画会出现卡顿.跳帧. 后来我发 ...
- Net Core下多种ORM框架特性及性能对比
在.NET Framework下有许多ORM框架,最著名的无外乎是Entity Framework,它拥有悠久的历史以及便捷的语法,在占有率上一路领先.但随着Dapper的出现,它的地位受到了威胁,本 ...
随机推荐
- 【转发】关于Java性能的9个谬论
转载请注明出处,感谢大家的支持!本文来自优优码:http://www.uucode.net/201502/9%e4%b8%aa%e8%b0%ac%e8%ae%ba Java的性能有某种黑魔法之称.部分 ...
- 使用非Web方式从CA申请证书
背景介绍:关于从CA申请证书这点事,网上的那些教程基本都是让我们通过访问https://server/certsrv这样的网页来操作的,我一直希望不依赖IIS就把这事干了,于是就有了下面的文章. 1. ...
- 一个简易的四则运算单元...(15.12.15 BUG更新)
网上找的, 没有作者信息, 只能在这里感谢一下了, 支持标准写法的四则运算 --2015-12-15 修改了一个内存泄漏的BUG - Pop方法没有释放申请的内存 unit Base.Calculat ...
- ajax请求成功后新窗口window.open()被拦截的解决方法
ajax 异步请求成功后需要新开窗口打开 url,使用的是 window.open() 方法,但是该操作并不是用户主动触发的,所以它认为这是不安全的就拦截了(不过如果是 _self 的话就不会有这个限 ...
- WordPress基础:Gravatar头像修正
WordPress会根据你的邮箱,从Gravatar获取你的头像,如果没有在Gravatar设置头像,将使用默认的,然而设置了之后显示还可能会出现以下问题: Gravatar头像读取困难,将影响网站读 ...
- Hadoop 运行 yarn jar 单词统计问题解决
测试单词统计时,运行yarn jar XX.jar 出现如下报错: Caused by: java.io.IOException: Initialization of all the collecto ...
- jquery 给指定li添加制定的css样式
$("ul li").eq(1).css({"color":"red"}); //第二个li $("ul li").eq ...
- MSSQL 全表搜索 指定字符串
平时在在MSSql中查询数据的时候,想查找,某个字段在数据库中是否存在,并且查询出在哪个表中,哪个字段下面,在不知道的情况下,操作起来会很麻烦,然后就写了一个sql语句,使用起来感觉挺方便的.当然了, ...
- Android 2D Graphics学习 Region和Canvas裁剪
1.首先介绍Region类 Region,中文意思即区域的意思,它表示的是canvas图层上的某一块封闭的区域. /**构造方法*/ public Region() //创建一个空的区域 publi ...
- Android下Cocos2d创建HelloWorld工程
最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...