Search in sources :

Example 1 with ProviderMethod

use of com.github.pagehelper.mapper.ProviderMethod 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)1 ProviderMethod (com.github.pagehelper.mapper.ProviderMethod)1 UserMapper (com.github.pagehelper.mapper.UserMapper)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