Java Code Examples for org.apache.ibatis.reflection.DefaultReflectorFactory

The following examples show how to use org.apache.ibatis.reflection.DefaultReflectorFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: joyqueue   Source File: PageResultInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    // 目标对象转换
    ResultSetHandler resultSetHandler = (ResultSetHandler) invocation.getTarget();

    // 获取MappedStatement,Configuration对象
    MetaObject metaObject =
            MetaObject.forObject(resultSetHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("mappedStatement");
    String statement = mappedStatement.getId();
    if (!isPageSql(statement,metaObject.getValue("boundSql.parameterObject"))) {
        return invocation.proceed();
    }

    // 获取分页参数
    QPageQuery pageQuery = (QPageQuery) metaObject.getValue("boundSql.parameterObject");

    List<PageResult> result = new ArrayList<PageResult>(1);
    PageResult page = new PageResult();
    page.setPagination(pageQuery.getPagination());
    page.setResult((List) invocation.proceed());
    result.add(page);

    return result;
}
 
Example 2
Source Project: mybatis-test   Source File: MetaClassTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHasSetter() {
    MetaClass authorMeta = MetaClass.forClass(Author.class, new DefaultReflectorFactory());

    System.out.println("\n------------☆ Author ☆------------");
    System.out.println("id -> " + authorMeta.hasSetter("id"));
    System.out.println("name -> " + authorMeta.hasSetter("name"));
    System.out.println("age -> " + authorMeta.hasSetter("age"));
    System.out.println("articles -> " + authorMeta.hasSetter("articles"));
    System.out.println("articles[] -> " + authorMeta.hasSetter("articles[]"));
    System.out.println("title -> " + authorMeta.hasSetter("title"));

    MetaClass articleMeta = MetaClass.forClass(Article.class, new DefaultReflectorFactory());
    System.out.println("\n------------☆ Article ☆------------");
    System.out.println("id -> " + articleMeta.hasSetter("id"));
    System.out.println("title -> " + articleMeta.hasSetter("title"));
    System.out.println("content -> " + articleMeta.hasSetter("content"));
    System.out.println("author.id -> " + articleMeta.hasSetter("author.id"));
    System.out.println("author.name -> " + articleMeta.hasSetter("author.name"));
    System.out.println();
}
 
Example 3
Source Project: taoshop   Source File: MybatisSqlInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 包装sql后,重置到invocation中
 * @param invocation
 * @param sql
 * @throws SQLException
 */
private void resetSql2Invocation(Invocation invocation, String sql) throws SQLException {
    final Object[] args = invocation.getArgs();
    MappedStatement statement = (MappedStatement) args[0];
    Object parameterObject = args[1];
    BoundSql boundSql = statement.getBoundSql(parameterObject);
    MappedStatement newStatement = newMappedStatement(statement, new BoundSqlSqlSource(boundSql));
    MetaObject msObject =  MetaObject.forObject(newStatement, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),new DefaultReflectorFactory());
    msObject.setValue("sqlSource.boundSql.sql", sql);
    args[0] = newStatement;
}
 
Example 4
Source Project: joyqueue   Source File: PageStatementInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler handler = (StatementHandler) invocation.getTarget();

    // 获取MappedStatement,Configuration对象
    MetaObject metaObject =
            MetaObject.forObject(handler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
    String statement = mappedStatement.getId();
    if (!isPageSql(statement,metaObject.getValue("boundSql.parameterObject"))) {
        return invocation.proceed();
    }

    Configuration configuration = (Configuration) metaObject.getValue("delegate.configuration");
    Executor executor = (Executor) metaObject.getValue("delegate.executor");

    // 获取分页参数
    BoundSql boundSql = handler.getBoundSql();
    QPageQuery pageQuery = (QPageQuery) boundSql.getParameterObject();
    String countStatement = buildCountStatement(statement);
    List<Integer> counts = executor.query(configuration.
            getMappedStatement(countStatement), pageQuery, RowBounds.DEFAULT, null);

    int count = 0;
    if (counts != null && !counts.isEmpty()) {
        count = counts.get(0) == null ? 0 : counts.get(0);
    }

    if (pageQuery.getPagination() == null) {
        pageQuery.setPagination(new Pagination());
    }
    pageQuery.getPagination().setTotalRecord(count);

    String sql = boundSql.getSql();
    if (logger.isDebugEnabled()) {
        logger.debug("raw SQL : " + sql);
    }

    if (sql == null || sql.isEmpty() || sql.contains(" limit ")) {
        return invocation.proceed();
    }

    String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
    metaObject.setValue("delegate.boundSql.sql",
            getLimitString(originalSql, pageQuery.getPagination().getStart(),
                    pageQuery.getPagination().getSize()));
    metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);

    if (logger.isDebugEnabled()) {
        logger.debug("pagination SQL : " + sql);
    }
    return invocation.proceed();
}