Example usage for org.apache.ibatis.session RowBounds DEFAULT

List of usage examples for org.apache.ibatis.session RowBounds DEFAULT

Introduction

In this page you can find the example usage for org.apache.ibatis.session RowBounds DEFAULT.

Prototype

RowBounds DEFAULT

To view the source code for org.apache.ibatis.session RowBounds DEFAULT.

Click Source Link

Usage

From source file:com.baidu.oped.apm.plugin.mybatis.SqlSessionTestBase.java

License:Apache License

protected final void testAndVerifySelectMap() throws Exception {
    // Given/*w  w  w  .  java2s.c o m*/
    final String selectMapId = "selectListId";
    SqlSession sqlSession = getSqlSession();
    // When
    sqlSession.selectMap(selectMapId, "key");
    sqlSession.selectMap(selectMapId, new Object(), "key");
    sqlSession.selectMap(selectMapId, new Object(), "key", RowBounds.DEFAULT);
    // Then
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Method selectMap1 = sqlSession.getClass().getDeclaredMethod("selectMap", String.class, String.class);
    Method selectMap2 = sqlSession.getClass().getDeclaredMethod("selectMap", String.class, Object.class,
            String.class);
    Method selectMap3 = sqlSession.getClass().getDeclaredMethod("selectMap", String.class, Object.class,
            String.class, RowBounds.class);
    verifier.verifyTrace(event("MYBATIS", selectMap1, Expectations.cachedArgs(selectMapId)));
    verifier.verifyTrace(event("MYBATIS", selectMap2, Expectations.cachedArgs(selectMapId)));
    verifier.verifyTrace(event("MYBATIS", selectMap3, Expectations.cachedArgs(selectMapId)));
}

From source file:com.baomidou.mybatisplus.plugins.CachePaginationInterceptor.java

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter
 * {@link org.apache.ibatis.session.RowBounds}
 */// w  w  w.j  a  va  2  s . com
public Object intercept(Invocation invocation) throws Throwable {

    Object target = invocation.getTarget();
    if (target instanceof StatementHandler) {
        StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
        RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");

        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
        String originalSql = boundSql.getSql();

        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            boolean orderBy = true;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                orderBy = countOptimize.isOrderBy();
            }
            String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
            originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
        } else {
            // support physical Pagination for RowBounds
            originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
        }

        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
        metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    } else {
        RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Executor executor = (Executor) invocation.getTarget();
        Connection connection = executor.getTransaction().getConnection();
        Object parameterObject = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
        String originalSql = boundSql.getSql();
        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                super.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
                if (page.getTotal() <= 0) {
                    return invocation.proceed();
                }
            }
        }
    }
    return invocation.proceed();
}

From source file:com.baomidou.mybatisplus.plugins.PaginationInterceptor.java

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
 *///from w  ww  .  j a  v  a2 s .  co m
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    // ?SELECT?
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
            .getValue("delegate.mappedStatement");
    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
    /* ??? */
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Connection connection = (Connection) invocation.getArgs()[0];
    if (isDynamicDataSource()) {
        dialectType = JdbcUtils.getDbType(connection.getMetaData().getURL()).getDb();
    }
    if (rowBounds instanceof Pagination) {
        Pagination page = (Pagination) rowBounds;
        boolean orderBy = true;
        if (page.isSearchCount()) {
            CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                    page.isOptimizeCount());
            orderBy = countOptimize.isOrderBy();
            this.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
            if (page.getTotal() <= 0) {
                return invocation.proceed();
            }
        }
        String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
        originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
    } else {
        // support physical Pagination for RowBounds
        originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
    }

    /*
       * <p> ? </p> <p> ???????</p>
     */
    metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
    metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    return invocation.proceed();
}

From source file:com.baomidou.mybatisplus.test.GlobalConfigurationTest.java

License:Apache License

/**
 * ?//  ww w.ja va2s  .  c  o m
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    GlobalConfiguration global = GlobalConfiguration.defaults();
    global.setAutoSetDbType(true);
    // FieldStrategy.Empty
    global.setFieldStrategy(2);
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=UTF-8");
    dataSource.setUsername("root");
    dataSource.setPassword("521");
    dataSource.setMaxTotal(1000);
    GlobalConfiguration.setMetaData(dataSource, global);
    // ?
    InputStream inputStream = GlobalConfigurationTest.class.getClassLoader()
            .getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder factoryBuilder = new MybatisSessionFactoryBuilder();
    factoryBuilder.setGlobalConfig(global);
    SqlSessionFactory sessionFactory = factoryBuilder.build(inputStream);
    SqlSession session = sessionFactory.openSession(false);
    TestMapper testMapper = session.getMapper(TestMapper.class);
    /*Wrapper type = Condition.instance().eq("id",1).or().in("type", new Object[]{1, 2, 3, 4, 5, 6});
    List list = testMapper.selectList(type);
    System.out.println(list.toString());*/
    Test test = new Test();
    test.setCreateTime(new Date());
    // ?
    test.setType("");
    testMapper.insert(test);

    SqlSession sqlSession = sessionFactory.openSession(false);
    NotPKMapper pkMapper = sqlSession.getMapper(NotPKMapper.class);
    NotPK notPK = new NotPK();
    notPK.setUuid(UUID.randomUUID().toString());
    int num = pkMapper.insert(notPK);
    Assert.assertTrue(num > 0);
    NotPK notPK1 = pkMapper.selectOne(notPK);
    Assert.assertNotNull(notPK1);
    pkMapper.selectPage(RowBounds.DEFAULT, Condition.create().eq("type", 12121212));
    NotPK notPK2 = null;
    try {
        notPK2 = pkMapper.selectById("1");
    } catch (Exception e) {
        System.out.println(",");
    }
    Assert.assertNull(notPK2);
    int count = pkMapper.selectCount(Condition.EMPTY);
    Assert.assertTrue(count > 0);
    int deleteCount = pkMapper.delete(null);
    Assert.assertTrue(deleteCount > 0);
    session.rollback();
    sqlSession.commit();
}

From source file:com.baomidou.mybatisplus.test.mysql.NoXMLTest.java

License:Apache License

public static void main(String[] args) {
    /*//from w w w .ja va 2 s .c om
    * ?
    */
    InputStream in = NoXMLTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
    SqlSessionFactory sessionFactory = mf.build(in);
    SqlSession sqlSession = sessionFactory.openSession();
    /**
     * ?
     */
    TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
    testMapper.insert(new Test(IdWorker.getId(), "Caratacus"));
    List<Map<String, Object>> list = testMapper.selectMaps(null);
    List<Map<String, Object>> list1 = testMapper.selectMapsPage(RowBounds.DEFAULT, null);
    List<Map<String, Object>> list2 = testMapper.selectMapsPage(new Page<>(1, 5), null);
    System.out.println(list);
    System.out.println(list1);
    System.out.println(list2);
    Map<String, Object> map = new HashMap<>();
    map.put("type", null);
    map.put("id", null);
    List<Test> tests = testMapper.selectByMap(map);
    if (null != tests) {
        for (Test test : tests) {
            System.out.println("id:" + test.getId() + " , type:" + test.getType());
        }
    } else {
        System.err.println(" tests is null. ");
    }
    testMapper.delete(null);

}

From source file:com.cheyipai.platformservice.thirdparty.core.pager.PageHelper.java

License:Open Source License

/**
 * Mybatis//  w w  w .  ja v a 2s . c  o m
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
@Override
public Object intercept(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    } else {
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        //?
        Page page = getPage(rowBounds);
        //pageSizeZero
        if (pageSizeZero && page.getPageSize() == 0) {
            //?
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            page.setPageNum(1);
            //?pageSize=total
            page.setPageSize(page.size());
            //??total
            page.setTotal(page.size());
            //?Page - ???
            return page;
        }
        //?total??count
        if (page.isCount()) {
            BoundSql boundSql = ms.getBoundSql(parameterObject);
            //?MappedStatement?qs
            args[0] = getMappedStatement(ms, boundSql, SUFFIX_COUNT);
            //
            Object result = invocation.proceed();
            //
            page.setTotal((Integer) ((List) result).get(0));
            if (page.getTotal() == 0) {
                return page;
            }
        }
        //pageSize>0pageSize<=0???count
        if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
                || rowBounds != RowBounds.DEFAULT)) {
            BoundSql boundSql = ms.getBoundSql(parameterObject);
            //?MappedStatement?qs
            args[0] = getMappedStatement(ms, boundSql, SUFFIX_PAGE);
            //parameterObject?
            args[1] = setPageParameter(parameterObject, boundSql, page);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.dao.genericdao.mybatis.plugins.page.support.SqlUtil.java

License:Open Source License

/**
 * Mybatis//from  w  ww .j  a va  2 s  .  c  o m
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
private Object _processPage(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (SqlUtil.getLocalPage() == null && rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    } else {
        //?ms
        MappedStatement ms = (MappedStatement) args[0];
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        //?
        PageHelper page = getPage(rowBounds);
        //pageSizeZero
        if ((page.getPageSizeZero() != null && page.getPageSizeZero()) && page.getPageSize() == 0) {
            //?
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            page.setPageNum(1);
            //?pageSize=total
            page.setPageSize(page.size());
            //??total
            page.setTotal(page.size());
            //?Page - ???
            return page;
        }
        SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource();
        //?total??count
        if (page.isCount()) {
            //?MappedStatement?qs
            processCountMappedStatement(ms, sqlSource, args);
            //
            Object result = invocation.proceed();
            //
            page.setTotal((Integer) ((List) result).get(0));
            if (page.getTotal() == 0) {
                return page;
            }
        }
        //pageSize>0pageSize<=0???count
        if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
                || rowBounds != RowBounds.DEFAULT)) {
            //?MappedStatement?qs
            processPageMappedStatement(ms, sqlSource, page, args);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.dingding.utils.page.PageHelper.java

License:Open Source License

/**
 * Mybatis/*  w w  w. ja v  a2  s . com*/
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
//@Override
public Object intercept(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    } else {
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        //?
        Page page = getPage(rowBounds);
        //pageSizeZero
        if (pageSizeZero && page.getPageSize() == 0) {
            //?
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            page.setPageNum(1);
            //?pageSize=total
            page.setPageSize(page.size());
            //??total
            page.setTotal(page.size());
            //?Page - ???
            return page;
        }
        //?total??count
        if (page.isCount()) {
            BoundSql boundSql = ms.getBoundSql(parameterObject);
            //?MappedStatement?qs
            args[0] = SQLUTIL.getCountMappedStatement(ms, boundSql);
            //
            Object result = invocation.proceed();
            //
            page.setTotal((Integer) ((List) result).get(0));
            if (page.getTotal() == 0) {
                return page;
            }
        }
        //pageSize>0pageSize<=0???count
        if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
                || rowBounds != RowBounds.DEFAULT)) {
            BoundSql boundSql = ms.getBoundSql(parameterObject);
            //?MappedStatement?qs
            args[0] = SQLUTIL.getPageMappedStatement(ms, boundSql);
            //parameterObject?
            args[1] = SQLUTIL.setPageParameter(parameterObject, boundSql, page);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.github.pagehelper.dialect.AbstractRowBoundsDialect.java

License:Open Source License

@Override
public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
    return rowBounds == RowBounds.DEFAULT;
}

From source file:com.github.pagehelper.page.PageParams.java

License:Open Source License

/**
 * ??//w w w. j  a  v a  2 s .c  o  m
 *
 * @param parameterObject
 * @param rowBounds
 * @return
 */
public Page getPage(Object parameterObject, RowBounds rowBounds) {
    Page page = PageHelper.getLocalPage();
    if (page == null) {
        if (rowBounds != RowBounds.DEFAULT) {
            if (offsetAsPageNum) {
                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
            } else {
                page = new Page(new int[] { rowBounds.getOffset(), rowBounds.getLimit() }, rowBoundsWithCount);
                //offsetAsPageNum=falsePageNum?reasonablefalse
                page.setReasonable(false);
            }
        } else {
            try {
                page = PageObjectUtil.getPageFromObject(parameterObject, false);
            } catch (Exception e) {
                return null;
            }
        }
        if (page == null) {
            return null;
        }
        PageHelper.setLocalPage(page);
    }
    //??
    if (page.getReasonable() == null) {
        page.setReasonable(reasonable);
    }
    //truepagesize0RowBoundslimit=0?
    if (page.getPageSizeZero() == null) {
        page.setPageSizeZero(pageSizeZero);
    }
    return page;
}