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的出现,它的地位受到了威胁,本 ...
随机推荐
- CSS3的高级特性
CSS3对响应式设计非常有用:使用CSS3替代图片,在有带宽限制的网页中可有效减少http请求(从而使网页加载更快),并可使网页更灵活.更容易维护. 在开发CSS3时,要记住添加相关的浏览器私有前缀以 ...
- 清理session的案例
and OPNAME ='Sort Output'; and OPNAME ='Sort Output'; and OPNAME ='Sort Output' ); select 'alter sys ...
- 点击input时,里面默认字体消失显示
点击input时,点击input里面默认字体消失显示: <input type="" name="" id="" value=&quo ...
- LeetCode First Unique Character in a String
原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...
- 二、oracle数据库成功安装步骤 配置监听器
Oracle数据库使用监听器来接收客户端的连接请求,要使客户端能连接Oracle数据库,必须配置监听程序. 在安装Oracle数据库时,如果选择的是"创建和配置数据库",则安装 ...
- SpringSecurity操作指南-在SpringMVC项目上配置Spring Security
- DOM中文本节点索引方法
问题 对于 jquery 接口text()只能取到有标签的 dom对象中 文本内容. 如果索引对象本身就是文本节点,则不好索引到, 没有相关的索引选择器. 例如: 对于<input>aaa ...
- 从 IClassFactory 为 CLSID 为 {00024500-0000-0000-C000-000000000046} 的 COM 组件创建实例失败,原因是出现以下错误: 8001010a解决办法
1.在命令行中输入:dcomcnfg,会显示出“组件服务”管理器 2.打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选 ...
- VS快速生成JSON数据格式对应的实体
有固定好的Json数据格式,你还在手动敲对应的实体吗?有点low了!步入正题,这是一个json字符串,先去验证JSON数据格式(http://www.bejson.com/)如下: { & ...
- XPath、XQuery 以及 XSLT 函数
存取函数 名称 说明 fn:node-name(node) 返回参数节点的节点名称. fn:nilled(node) 返回是否拒绝参数节点的布尔值. fn:data(item.item,...) 接受 ...