- 浏览: 646052 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (153)
- 多线程 (13)
- 数据库连接池 (3)
- maven (9)
- jquery (2)
- struts (5)
- spring (6)
- hibernate (10)
- mybatis (15)
- eclipse插件 (6)
- 短信SI行业 (3)
- oracle数据库 (6)
- junit (1)
- H2数据库 (1)
- sql server 数据库 (2)
- java通讯 (9)
- java序列化与反序列化 (3)
- java分布式缓存 (2)
- 线程池 (3)
- linux系统 (5)
- 企业级问题解决方案 (7)
- JDBC (1)
- uml (2)
- 设计思想与实现 (10)
- 面试 (3)
- ant (2)
- xml (1)
- soa (1)
- 设计模式 (7)
- java垃圾回收机制 (1)
- 工具和框架 (2)
- 其他 (8)
- 程序规范 (1)
- google code 建立svn (1)
- netty (1)
- JPA (1)
- jboss (2)
- RMI (1)
最新评论
-
nnxiaod:
hackpro 写道如果你这里有多个参数,怎么传值了selec ...
MyBatis中Association联合select使用 -
nnxiaod:
引用不管输入参数名称是什么,mybatis最终会执行: 效果为 ...
MyBatis中Association联合select使用 -
a942010:
哪里调用了返回的结果集是menuMap啊?只有后面一点。
MyBatis中Association联合select使用 -
Java-feifeiok:
弄个全点的例子啊!@
MyBatis中Association联合select使用 -
Java-feifeiok:
弄个全点的例子啊
MyBatis中Association联合select使用
mybatis插件配置:
<plugins> <plugin interceptor="com.xuanwu.sms.smstask.webmanager.common.crud.plugins.PaginationInterceptor"> <property name="dialectClass" value="org.hibernate.dialect.SQLServer2005Dialect"/> </plugin> </plugins>
插件处理类:
package com.xuanwu.sms.smstask.webmanager.common.crud.plugins; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; import org.apache.ibatis.builder.SqlSourceBuilder; import org.apache.ibatis.builder.xml.dynamic.DynamicContext; import org.apache.ibatis.builder.xml.dynamic.SqlNode; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.parameter.DefaultParameterHandler; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.hibernate.dialect.Dialect; import com.xuanwu.sms.smstask.webmanager.common.crud.entity.IPage; import com.xuanwu.sms.smstask.webmanager.common.crud.entity.Pagination; @Intercepts( { @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) }) public class PaginationInterceptor implements Interceptor { Dialect dialect; public Object intercept(Invocation invocation) throws Throwable { Object parameterObject = invocation.getArgs()[1]; if(parameterObject != null && parameterObject instanceof IPage){ IPage page =(IPage)parameterObject; if (page.getPagination() == null|| page.getPagination().isAvailable()) { return pageIntercept(invocation); } } return invocation.proceed(); } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { String dialectClass = properties.getProperty("dialectClass"); try { dialect = (Dialect) Class.forName(dialectClass).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private Object pageIntercept(Invocation invocation) throws Throwable{ int rowCount = 0; MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameterObject = invocation.getArgs()[1]; SqlSource sqlSource = getCountSqlSource(mappedStatement, parameterObject); MappedStatement newMappedStatement = copyMappedStatementBySqlSource(mappedStatement, sqlSource); Connection connection = newMappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); DefaultParameterHandler dp = new DefaultParameterHandler(mappedStatement,parameterObject,sqlSource.getBoundSql(parameterObject)); PreparedStatement countStmt = connection.prepareStatement(sqlSource.getBoundSql(parameterObject).getSql()); dp.setParameters(countStmt); ResultSet rs= countStmt.executeQuery(); if(rs.next()){ rowCount =rs.getInt(1); } rs.close(); countStmt.close(); connection.close(); IPage page =(IPage)parameterObject; if (page.getPagination() == null) { page.setPagination(new Pagination()); } page.getPagination().setRowCount(rowCount); page.getPagination().pagination(); sqlSource = getPageLimitSqlSource(mappedStatement, parameterObject); newMappedStatement = copyMappedStatementBySqlSource(mappedStatement, sqlSource); invocation.getArgs()[0] = newMappedStatement; return invocation.proceed(); } private String getMapperSQL(MappedStatement mappedStatement,Object parameterObject) throws Throwable{ SqlSource nowSqlSource = mappedStatement.getSqlSource(); Class<?> sqlSourceType = nowSqlSource == null ? Object.class : nowSqlSource.getClass(); Field rootSqlNodeField = sqlSourceType.getDeclaredField("rootSqlNode"); rootSqlNodeField.setAccessible(true); SqlNode sqlNode = (SqlNode) rootSqlNodeField.get(nowSqlSource); DynamicContext context = new DynamicContext(mappedStatement.getConfiguration(), parameterObject); sqlNode.apply(context); return context.getSql(); } private SqlSource getCountSqlSource(MappedStatement mappedStatement,Object parameterObject) throws Throwable{ SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(mappedStatement.getConfiguration()); String mapperSQL = getMapperSQL(mappedStatement,parameterObject); Class<?> parameterType = parameterObject == null ? Object.class: parameterObject.getClass(); String newSql = "select count(1) from ( "+ mapperSQL+" ) t"; SqlSource sqlSource = sqlSourceParser.parse(newSql, parameterType); return sqlSource; } private SqlSource getPageLimitSqlSource(MappedStatement mappedStatement,Object parameterObject) throws Throwable { SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(mappedStatement.getConfiguration()); String mapperSQL = getMapperSQL(mappedStatement,parameterObject); Class<?> parameterType = parameterObject == null ? Object.class: parameterObject.getClass(); IPage page =(IPage)parameterObject; String newSql = getLimitString(mapperSQL, page.getPagination().getCurrentMinRow(), page.getPagination().getCurrentMaxRow()); SqlSource sqlSource = sqlSourceParser.parse(newSql, parameterType); return sqlSource; } private MappedStatement copyMappedStatementBySqlSource(MappedStatement mappedStatement,SqlSource sqlSource){ MappedStatement.Builder builder = new MappedStatement.Builder(mappedStatement.getConfiguration(), mappedStatement.getId(),sqlSource, mappedStatement.getSqlCommandType()); builder.resource(mappedStatement.getResource()); builder.fetchSize(mappedStatement.getFetchSize()); builder.statementType(mappedStatement.getStatementType()); builder.keyGenerator(mappedStatement.getKeyGenerator()); builder.keyProperty(mappedStatement.getKeyProperty()); builder.timeout(mappedStatement.getTimeout()); builder.parameterMap(mappedStatement.getParameterMap()); builder.resultMaps(mappedStatement.getResultMaps()); builder.cache(mappedStatement.getCache()); MappedStatement newMappedStatement = builder.build(); return newMappedStatement; } private String getLimitString(String sql, int offset, int limit) { String whereLowerCase = null; String finalSql = null; String sqlLowerCase = sql.trim().toLowerCase(); int whereIndex = sqlLowerCase.indexOf("where"); if (whereIndex > 0) { whereLowerCase = sqlLowerCase.substring(whereIndex); } String limitArgs[] = new String[] { String.valueOf(offset),String.valueOf(limit)}; String newSql = dialect.getLimitString(sqlLowerCase, offset, limit); String sqls[] = newSql.split("\\?"); String querySQL = ""; for (int i = 0; i < 2; i++) { querySQL = querySQL + " " + sqls[i] + limitArgs[i]; } querySQL = querySQL.trim(); if (whereLowerCase == null || "".endsWith(whereLowerCase)) { return querySQL; } int finalWhereIndex = querySQL.indexOf(whereLowerCase); if (finalWhereIndex > 0) { String begin = querySQL.substring(0, finalWhereIndex); String end = querySQL.substring(finalWhereIndex + whereLowerCase.length()); finalSql = begin + sql.substring(whereIndex) + end; } return finalSql; } }
分页接口:
package com.xuanwu.sms.smstask.webmanager.common.crud.entity; import java.io.Serializable; public interface IPage extends Serializable { public Pagination getPagination(); public void setPagination(Pagination pagination); }
分页实体类:
package com.xuanwu.sms.smstask.webmanager.common.crud.entity; import java.io.Serializable; public class Pagination implements Serializable{ private static final long serialVersionUID = -6579435031214235862L; private int rowCount;// 总记录数 private int pageSize = 15;// 每页记录数 private int pageCount;// 总页数 private int currentPage;// 当前页数 private boolean next;// 是否能下一页 private boolean previous;// 是否能上一页 private boolean available = true;//是否分页 public Pagination(){ } public Pagination(String currentPage, int rowCount) { if (currentPage == null) { this.currentPage = 1; } else { this.currentPage = Integer.parseInt(currentPage); } this.rowCount = rowCount; // 计算总页数 this.pageCount = (int) Math.ceil(this.rowCount / (double) this.pageSize); // 计算是否能上一页和下一页 this.next = this.currentPage < this.pageCount; this.previous = this.currentPage > 1; } public Pagination pagination() { if (currentPage == 0) { this.currentPage = 1; } // 计算总页数 this.pageCount = (int) Math .ceil(this.rowCount / (double) this.pageSize); // 计算是否能上一页和下一页 this.next = this.currentPage < this.pageCount; this.previous = this.currentPage > 1; return this; } public int getPageCount() { return pageCount; } public int getCurrentPage() { return currentPage; } public int getRowCount() { return rowCount; } public void setRowCount(int rowCount) { this.rowCount = rowCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentMinRow() { return (currentPage-1) * pageSize; } public int getCurrentMaxRow() { return currentPage * pageSize - 1; } public int getNextPage() { if (next) { return currentPage + 1; } return currentPage; } public int getPreviousPage() { if (previous) { return currentPage - 1; } return currentPage; } public boolean isNext() { return next; } public boolean isPrevious() { return previous; } public int nextPage() { if (next) { currentPage = currentPage + 1; } return currentPage; } public int previousPage() { if (previous) { currentPage = currentPage - 1; } return currentPage; } public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; } }
pom.xml
mybatis版本:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.6</version>
<scope>compile</scope>
</dependency>
hibernate版本
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.8.Final</version>
</dependency>
评论
2 楼
cczakai
2012-04-09
性能调优,对于千万级别的数据表,查询使用mybatis是用性能瓶颈的,因为mybatis本身是逻辑分页,所以效率很慢,使用物理分页就是为了解决这个问题的。
1 楼
hillfallshore
2012-03-25
这个分页有什么优点?
发表评论
-
mybatis Executor插件加载源码
2012-03-12 17:19 2777//插件扩展实现类: org.apache.ibatis.s ... -
MyBatis:缓存使用及理解
2011-11-28 16:27 1885一、MyBatis的Cache配置1、全局开关:默认是true ... -
MyBatis动态SQL中trim标签的使用
2011-11-28 15:46 76123My Batis 官方文档 对 动态SQL中使用trim标签的 ... -
MyBatis延迟加载及在spring中集成配置
2011-11-24 14:18 20249当你要使用one to one,many to one 就会碰 ... -
MyBatis中Association联合select使用
2011-11-24 14:03 90934Association联合select使用容易出差问题地方 ... -
MyBatis:Parameter Maps collection does not contain value for 的问题解决
2011-11-23 16:17 46318报错: org.apache.ibatis.builder. ... -
从 iBatis 到 MyBatis
2011-11-18 11:39 1219从 iBatis 到 MyBatis,你准备好了吗? 对于从 ... -
mybatis中TypeHandles使用与扩展
2011-11-18 11:19 10276无论是MyBatis 在预处理语句中设置一个参数,还是 ... -
mybatis多参数传入问题
2011-11-17 11:49 4443package com.demo.sys.mapper; ... -
mybatis大于小于及其它 运算符操作
2011-08-12 16:07 21492在ibatis配置文件写SQL语句的时候对于一些比如“< ... -
MyBatis ExecutorType.BATCH 报ORA-01000: 超出打开游标的最大数的解决
2011-08-11 17:34 10812MyBatis ExecutorType.BATCH 报ORA ... -
MyBatis报Error setting null parameter 的解决方法
2011-08-11 17:10 16904mybatis论坛解决: http://code.googl ... -
MyIbatis代码自动生成工具
2011-07-12 15:39 4394参考官方文档:http://code.google.com/p ... -
ibatis和hibernate区别
2011-03-15 19:42 1265ibatis:sql需要自己写hibernate:sql自动生 ... -
sqlMapConfig.xml之参数配置
2011-02-12 09:28 143<?xml version="1.0" ...
相关推荐
Spring3.0.6 +mybatis 3.0.6配置文档
完整mybatis3.0.6与spring3.0整合例子,带有数据库生成文件。,特别是其中的事务处理,包括回滚。
The mybatis data mapper framework makes it easier to use a relational database with object-oriented applications. mybatis couples objects with stored procedures or SQL statements using a XML ...
mybatis 3.0.6 all + 中文指导手册
mybatis-3.0.6-官方资料,很全的!
mybatis-3.0.6-javadoc.jar
这是一个小的Demo 通过Mybatis的生成器自动生成实体类接口和Sql语句等。。。
安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够...
NULL 博文链接:https://love2java.iteye.com/blog/1128192
spring+mybatis实现了物理分页的
mybatis-3.0.6-SNAPSHOT
一个简单的demo,里面有一些关于DhtmlxGrid的操作,注释什么的都有,数据库脚本也有,在src下面有一个sql文件...当然,mybatis的核心包和oracle数据库的驱动包都是有的,所以大家可以直接运行看效果...
06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis
mybatis 框架关于mysql、oracle、db2等数据库的sql方言包。
mybatis-3.0.6-sources.jar
mybatis-3.0.6-javadoc.chm 无法显示-》右键-》属性解除锁定
springboot(4) 整合mybatis和hibernate 请删除项目中本地仓库的配置,或者改成国内的公共仓库,否则将无法启动