Search in sources :

Example 1 with PageParam

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

the class PageInterceptor method resolvePageParam.

private void resolvePageParam(Object handler, HttpServletRequest request) {
    String $start = request.getParameter(Constant.PAGE_CONFIG_PARAM_START);
    String $limit = request.getParameter(Constant.PAGE_CONFIG_PARAM_LIMIT);
    int start;
    int limit;
    if (Utils.isEmpty($start) || Utils.isEmpty($limit)) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        PageConfig pc = handlerMethod.getMethod().getAnnotation(PageConfig.class);
        start = pc.start();
        limit = pc.limit();
    } else {
        start = Math.max(Integer.valueOf($start), 0);
        limit = Math.min(Integer.valueOf($limit), Integer.MAX_VALUE);
    }
    PageParam pageParam = PageParam.initWithOffset(start, limit);
    ThreadPageUtil.putPageParam(pageParam);
}
Also used : PageConfig(com.optimus.common.mybatis.annotation.PageConfig) PageParam(com.optimus.common.mybatis.PageParam) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 2 with PageParam

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

the class DubboUtil method getPageParam.

public static PageParam getPageParam(DubboKey dubboKey) {
    String val = getAttachment(dubboKey.getKey());
    PageParam pageParam = Utils.fromJson(val, PageParam.class);
    return pageParam;
}
Also used : PageParam(com.optimus.common.mybatis.PageParam)

Example 3 with PageParam

use of com.optimus.common.mybatis.PageParam 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)

Example 4 with PageParam

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

the class MybatisPageInterceptor method resolvePageParam.

private PageParam resolvePageParam(ResultSet resultSet) throws SQLException {
    PageParam pageParam = PageUtil.getPageParam();
    int count = resultSet.getInt(1);
    int pageNumber = (count / pageParam.getLimit()) + 1;
    pageParam.setTotalCount(count);
    pageParam.setPageNumber(pageNumber);
    return pageParam;
}
Also used : PageParam(com.optimus.common.mybatis.PageParam)

Example 5 with PageParam

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

the class MySqlSqlParser method parseWithPageQuery.

@Override
public String parseWithPageQuery(String srcSql) {
    PageParam pageParam = ThreadPageUtil.getPageParam();
    StringBuilder newSql = new StringBuilder(srcSql);
    return newSql.append(" limit ").append(pageParam.getStart()).append(",").append(pageParam.getLimit()).toString();
}
Also used : PageParam(com.optimus.common.mybatis.PageParam)

Aggregations

PageParam (com.optimus.common.mybatis.PageParam)5 PageList (com.optimus.common.mybatis.PageList)1 PageConfig (com.optimus.common.mybatis.annotation.PageConfig)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 HandlerMethod (org.springframework.web.method.HandlerMethod)1