Search in sources :

Example 1 with PageList

use of com.optimus.common.mybatis.PageList in project luntan by caoawei.

the class MybatisPageInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    if (PageUtil.needPage() && sqlParser.isSupport(DBType.MYSQL)) {
        Executor executor = (Executor) invocation.getTarget();
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
        Object parameterObject = invocation.getArgs()[1];
        BoundSql boundSql = ms.getBoundSql(parameterObject);
        RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
        ResultHandler resultHandler = (ResultHandler) invocation.getArgs()[3];
        Connection connection = executor.getTransaction().getConnection();
        String countSql = sqlParser.parseWithCountQuery(boundSql.getSql().trim());
        BoundSql countBoundSql = copyBoundSql(ms, boundSql, countSql);
        PreparedStatement ps = connection.prepareStatement(countSql);
        setParameters(ps, ms, countBoundSql);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            PageParam pageParam = resolvePageParam(resultSet);
            PageList pageList = initPageList(pageParam);
            String pageSql = sqlParser.parseWithPageQuery(boundSql.getSql().trim());
            Utils.setFieldValue("sql", boundSql, pageSql);
            List list = query(executor, ms, parameterObject, boundSql, rowBounds, resultHandler);
            pageList.addAll(list);
            return pageList;
        }
        resultSet.close();
        connection.close();
    }
    return invocation.proceed();
}
Also used : Connection(java.sql.Connection) RowBounds(org.apache.ibatis.session.RowBounds) PreparedStatement(java.sql.PreparedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) Executor(org.apache.ibatis.executor.Executor) BaseExecutor(org.apache.ibatis.executor.BaseExecutor) BoundSql(org.apache.ibatis.mapping.BoundSql) ResultSet(java.sql.ResultSet) MetaObject(org.apache.ibatis.reflection.MetaObject) PageList(com.optimus.common.mybatis.PageList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) PageParam(com.optimus.common.mybatis.PageParam) PageList(com.optimus.common.mybatis.PageList)

Aggregations

PageList (com.optimus.common.mybatis.PageList)1 PageParam (com.optimus.common.mybatis.PageParam)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 BaseExecutor (org.apache.ibatis.executor.BaseExecutor)1 Executor (org.apache.ibatis.executor.Executor)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 ResultHandler (org.apache.ibatis.session.ResultHandler)1 RowBounds (org.apache.ibatis.session.RowBounds)1