seaswalker / posts

0 stars 0 forks source link

Mybatis cursor #25

Open seaswalker opened 3 years ago

seaswalker commented 3 years ago
@Test
public void clean() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    Cursor<Object> cursor = sqlSession.getMapper(SomeMapper.class).selectCursor(2333);
    try {
        cursor.iterator().forEachRemaining(o -> {
            // ...
        });
    } finally {
        IOUtils.closeQuietly(cursor, sqlSession);
    }
}

SqlSessionFactory可以直接注入:

@Autowired
private SqlSessionFactory sqlSessionFactory;

如果不用这种手动开启session的方式,在查询完了后cursor会被立马关闭,从而导致遍历报错。 参考: 新技能 MyBatis 千万数据表,快速分页!