• 博客访问: 14680
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-25 13:56
  • 认证徽章:
文章分类
文章存档

2015年(8)

我的朋友
微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

订阅
热词专题

分类: Java

<span style="font-size:18px;">Mybatis封装分页查询的java公用类<br /> </span> <div align="left"> <span style="font-size:18px;">&nbsp; <strong>分页----对于数据量很大的查询中,是必不可少的。mybatis底层的分页sql语句由于需要我们自己去手动写。而实现分页显示的时候我们需要根据分页查询条件查询符合条件的总记录数和记录的详细情况。因此,若是不去实现封装一下的话,我们需要写两条SQL语句去实现它。一次用于查询记录数目。一次用于查询分页显示的详细记录。当项目中碰到很多需要分页的时候,我们便对于每一个Mapper.xml文件都需要去写两条SQL语句。极其麻烦,代码重用----必须重用。所以,一个公共方法的分页需求应运而生。<br /> <br /> &nbsp; 直接上分页公共代码,其实现的原理是使用了拦截器的拦截作用。拦截一类分页查询的请求。我们根据传进来的参数是否是需要interceptor()方法中拦截的参数,是的话则拦截,并执行相应的SQL追加,否则,不进行追加。直接放行。视作普通查询。<br /> &nbsp;<br /> &nbsp;&nbsp; 需要在Mybatis的配置文件中配置加载服务器的时候加载该公共类:mybatis-config.xml<br /> &nbsp;&nbsp;&nbsp; &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!DOCTYPE configuration<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PUBLIC "-//mybatis.org//DTD Config 3.0//EN"<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;configuration&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;properties&gt;&lt;/properties&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;plugins&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#FF0000;">&lt;plugin interceptor="com.iboxpay.clear.filter.PaginationInterceptor"&gt;&lt;/plugin&gt;</span><br /> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &lt;/plugins&gt;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/configuration&gt;<br /> <br /> </strong><br /> import java.sql.Connection;<br /> import java.sql.PreparedStatement;<br /> import java.sql.ResultSet;<br /> import java.sql.SQLException;<br /> import java.util.Properties;&nbsp;&nbsp; &nbsp;<br /> <br /> import org.apache.ibatis.executor.parameter.ParameterHandler;<br /> import org.apache.ibatis.executor.statement.StatementHandler;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.mapping.BoundSql;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.mapping.MappedStatement;<br /> import org.apache.ibatis.plugin.Interceptor;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.plugin.Intercepts;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.plugin.Invocation;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.plugin.Plugin;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.plugin.Signature;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.reflection.MetaObject;&nbsp;&nbsp; &nbsp;<br /> import org.apache.ibatis.reflection.factory.DefaultObjectFactory;<br /> import org.apache.ibatis.reflection.factory.ObjectFactory;<br /> import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;<br /> import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;<br /> import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;<br /> import org.apache.ibatis.session.RowBounds;<br /> import org.slf4j.Logger;<br /> import org.slf4j.LoggerFactory;<br /> <br /> /**<br /> &nbsp;* 分页拦截器<br /> &nbsp;* @since 10.20.2014<br /> &nbsp;*/<br /> @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})&nbsp;&nbsp; &nbsp;<br /> public class PaginationInterceptor implements Interceptor {<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;private final Logger logger = LoggerFactory.getLogger(PaginationInterceptor.class);<br /> <br /> &nbsp;&nbsp; &nbsp;private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory();<br /> &nbsp;&nbsp; &nbsp;private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;public Object intercept(Invocation invocation) throws Throwable { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#FF0000;">// 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环 &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 可以分离出最原始的的目标类) &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (metaStatementHandler.hasGetter("h")) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object object = metaStatementHandler.getValue("h"); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY,&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT_OBJECT_WRAPPER_FACTORY); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF6666;"> // 分离最后一个代理对象的目标类 &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (metaStatementHandler.hasGetter("target")) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object object = metaStatementHandler.getValue("target"); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY,&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT_OBJECT_WRAPPER_FACTORY); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MappedStatement mappedStatement = (MappedStatement)&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler.getValue("delegate.mappedStatement"); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF0000;"> // 只重写需要分页的sql语句。通过MappedStatement的ID匹配,默认重写以Page结尾的 &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; MappedStatement的sql &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object parameterObject = boundSql.getParameterObject(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(parameterObject instanceof&nbsp; PageParam){<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (parameterObject == null) {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new NullPointerException("parameterObject is null!"); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PageParam page = (PageParam)parameterObject; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String sql = boundSql.getSql(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF0000;">&nbsp;&nbsp; // 重写sql &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String pageSql = buildPageSql(sql, page); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler.setValue("delegate.boundSql.sql", pageSql); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler.setValue("delegate.rowBounds.offset",&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RowBounds.NO_ROW_OFFSET); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connection connection = (Connection) invocation.getArgs()[0]; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#FF0000;">// 重设分页参数里的总页数等 &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setPageParameter(sql, connection, mappedStatement, boundSql, page); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<span style="color:#FF0000;">&nbsp;&nbsp; // 将执行权交给下一个拦截器 &nbsp;</span><br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return invocation.proceed(); &nbsp;<br /> &nbsp;&nbsp; &nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;private String buildPageSql(String sql, PageParam page) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if (page != null) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder pageSql = new StringBuilder(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pageSql = buildPageSqlForOracle(sql, page); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pageSql.toString(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } else {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sql; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;} &nbsp;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;public StringBuilder buildPageSqlForOracle(String sql, PageParam page) {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; StringBuilder pageSql = new StringBuilder(100);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String beginrow = String.valueOf((page.getCurrentPage() - 1) * page.getPageSize()); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String endrow = String.valueOf(page.getCurrentPage() * page.getPageSize()); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; pageSql.append("select * from ( select temp.*, rownum row_id from ( "); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; pageSql.append(sql); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; pageSql.append(" ) temp where rownum &lt;= ").append(endrow);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; pageSql.append(") where row_id &gt; ").append(beginrow);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; return pageSql; &nbsp;<br /> &nbsp;&nbsp; &nbsp;} &nbsp;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;/** <br /> &nbsp;&nbsp; &nbsp; * 从数据库里查询总的记录数并计算总页数,回写进分页参数&lt;code&gt;PageParam&lt;/code&gt;,这样调用 &nbsp;<br /> &nbsp;&nbsp; &nbsp; * 者就可用通过 分页参数&lt;code&gt;PageParam&lt;/code&gt;获得相关信息。 <br /> &nbsp;&nbsp; &nbsp; * &nbsp;<br /> &nbsp;&nbsp; &nbsp; * @param sql <br /> &nbsp;&nbsp; &nbsp; * @param connection <br /> &nbsp;&nbsp; &nbsp; * @param mappedStatement <br /> &nbsp;&nbsp; &nbsp; * @param boundSql <br /> &nbsp;&nbsp; &nbsp; * @param page <br /> &nbsp;&nbsp; &nbsp; * @throws SQLException <br /> &nbsp;&nbsp; &nbsp; */ &nbsp;<br /> &nbsp;&nbsp; &nbsp;private void setPageParameter(String sql, Connection connection, MappedStatement mappedStatement, &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BoundSql boundSql, PageParam page) throws SQLException { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; // 记录总记录数 &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String countSql = "select count(0) from (" + sql + ")"; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; PreparedStatement countStmt = null; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ResultSet rs = null; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; try { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; countStmt = connection.prepareStatement(countSql); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boundSql.getParameterMappings(), boundSql.getParameterObject()); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setParameters(countStmt, mappedStatement, countBS, boundSql.getParameterObject()); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs = countStmt.executeQuery();<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int totalCount = 0; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (rs.next()) {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totalCount = rs.getInt(1); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; page.setTotalCount(totalCount); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int totalPage = totalCount / page.getPageSize() + ((totalCount % page.getPageSize() == 0) ? 0 : 1); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; page.setTotalPage(totalPage);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.error("exception", e); <br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } finally {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs.close(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.error("exception", e); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; countStmt.close(); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.error("exception", e); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;<br /> &nbsp;&nbsp; &nbsp;} &nbsp;<br /> &nbsp;&nbsp; &nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object parameterObject) throws SQLException { &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; parameterHandler.setParameters(ps); &nbsp;<br /> &nbsp;&nbsp; &nbsp;} &nbsp;<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; @Override<br /> &nbsp;&nbsp;&nbsp; public Object plugin(Object target) {&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (target instanceof StatementHandler) { &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Plugin.wrap(target, this);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return target;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; @Override<br /> &nbsp;&nbsp;&nbsp; public void setProperties(Properties arg0) {&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; }</span><span style="font-size:18px;"><br /> }<br /> <span style="color:#FF0000;">将日志的过滤模式调到DEBUG模式,控制台可以打印出SQL<br /> 使用上述方法处理的分页查询,其只需要一条SQL语句就可以(Mapper.xml文件的SQL)</span><br /> &lt; select id="selectChannelSettleByParam" parameterType="PageParam" resultMap="RS_CHANNELSETTLE"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;![CDATA[<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;select&nbsp; *<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;]]&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;from channlsettle where 1=1 <br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;if test="params.channelSettleModel.channelId != null and 1000 != params.channelSettleModel.channelId"&gt;AND CHANNL_ID=#{params.channelSettleModel.channelId}&lt;/if&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;if test="params.channelSettleModel.clearStartTime != null and ‘‘ != params.channelSettleModel.clearStartTime"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;![CDATA[ &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;AND to_number(CLEAR_DATE) &gt;= to_number(substr(#{params.channelSettleModel.clearStartTime},0,8))&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;]]&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/if&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;if test="params.channelSettleModel.clearEndTime != null and ‘‘ != params.channelSettleModel.clearEndTime"&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;![CDATA[ &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;AND to_number(CLEAR_DATE) &lt;= to_number(substr(#{params.channelSettleModel.clearEndTime},0,8))&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;]]&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;/if&gt;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;order by INSTDATE desc<br /> &nbsp;&nbsp; &nbsp;&lt;/select&gt;<br /> <br /> <span style="color:#FF0000;">控制台打印的SQL:</span><br /> <span style="color:#FF0000;">第一条:</span>select count(0) from (select *&nbsp; from channlsettle where 1=1 AND CHANNL_ID=? AND to_number(CLEAR_DATE) &gt;= to_number(substr(?,0,8)) AND to_number(CLEAR_DATE) &lt;= to_number(substr(?,0,8)) order by INSTDATE desc) <br /> <br /> <span style="color:#FF0000;">第二条:</span>select * from ( select temp.*, rownum row_id from ( select *&nbsp; from channlsettle where 1=1 AND CHANNL_ID=? AND to_number(CLEAR_DATE) &gt;= to_number(substr(?,0,8)) AND to_number(CLEAR_DATE) &lt;= to_number(substr(?,0,8)) order by INSTDATE desc ) temp where rownum &lt;= 20) where row_id &gt; 0 <br /> <br /> </span><span style="font-size:18px;"><strong>从而让公共类实现了我们需要在Mapper.xml配置文件中重复写入两条SQL的工作,以后没一个分页查询都可以使用。</strong> <p> java企业级通用权限安全框架源码 SpringMVC mybatis or hibernate+ehcache shiro druid bootstrap HTML5 <wbr> </p> <a href="http://blog.sina.com.cn/s/blog_12a0d1f5f0102vpfh.html" target="_blank"></a> <p> <a href="http://blog.sina.com.cn/s/blog_12a0d1f5f0102vpfh.html" target="_blank">【java框架源码下载】</a> </p> <br /> </span> </div>
阅读(3432) | 评论(0) | 转发(2) |
0

上一篇:没有了

下一篇:Springmvc 默认首页的问题

给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册