Search in sources :

Example 1 with BoundSqlInterceptor

use of com.github.pagehelper.BoundSqlInterceptor in project Mybatis-PageHelper by pagehelper.

the class PageBoundSqlInterceptors method setProperties.

public void setProperties(Properties properties) {
    // 初始化 boundSqlInterceptorChain
    String boundSqlInterceptorStr = properties.getProperty("boundSqlInterceptors");
    if (StringUtil.isNotEmpty(boundSqlInterceptorStr)) {
        String[] boundSqlInterceptors = boundSqlInterceptorStr.split("[;|,]");
        List<BoundSqlInterceptor> list = new ArrayList<BoundSqlInterceptor>();
        for (int i = 0; i < boundSqlInterceptors.length; i++) {
            try {
                list.add((BoundSqlInterceptor) Class.forName(boundSqlInterceptors[i]).newInstance());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (list.size() > 0) {
            chain = new BoundSqlInterceptorChain(null, list);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BoundSqlInterceptor(com.github.pagehelper.BoundSqlInterceptor) BoundSqlInterceptorChain(com.github.pagehelper.BoundSqlInterceptorChain)

Example 2 with BoundSqlInterceptor

use of com.github.pagehelper.BoundSqlInterceptor in project Mybatis-PageHelper by pagehelper.

the class TestProviderInteceptor method testInterceptor.

@Test
public void testInterceptor() {
    SqlSession sqlSession = MybatisInterceptorHelper.getSqlSession();
    final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        PageHelper.startPage(1, 10).boundSqlInterceptor(new BoundSqlInterceptor() {

            @Override
            public BoundSql boundSql(Type type, BoundSql boundSql, CacheKey cacheKey, Chain chain) {
                System.out.println("[" + Thread.currentThread().getName() + "] - before: " + boundSql.getSql());
                BoundSql doBoundSql = chain.doBoundSql(type, boundSql, cacheKey);
                System.out.println("[" + Thread.currentThread().getName() + "] - after: " + doBoundSql.getSql());
                if (type == Type.ORIGINAL) {
                    Assert.assertTrue(doBoundSql.getSql().contains(TestBoundSqlInterceptor.COMMENT));
                }
                return doBoundSql;
            }
        });
        final String str = "飞";
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
    } finally {
        SqlCache.remove();
        sqlSession.close();
    }
}
Also used : UserMapper(com.github.pagehelper.mapper.UserMapper) SqlSession(org.apache.ibatis.session.SqlSession) BoundSql(org.apache.ibatis.mapping.BoundSql) BoundSqlInterceptor(com.github.pagehelper.BoundSqlInterceptor) ProviderMethod(com.github.pagehelper.mapper.ProviderMethod) CacheKey(org.apache.ibatis.cache.CacheKey) Test(org.junit.Test)

Aggregations

BoundSqlInterceptor (com.github.pagehelper.BoundSqlInterceptor)2 BoundSqlInterceptorChain (com.github.pagehelper.BoundSqlInterceptorChain)1 ProviderMethod (com.github.pagehelper.mapper.ProviderMethod)1 UserMapper (com.github.pagehelper.mapper.UserMapper)1 ArrayList (java.util.ArrayList)1 CacheKey (org.apache.ibatis.cache.CacheKey)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 SqlSession (org.apache.ibatis.session.SqlSession)1 Test (org.junit.Test)1