Skip to content

Unable to iterate Cursor if the next element is null #1653

@lysu

Description

@lysu

MyBatis version

3.5.1

Database vendor and version

mysql 5.7.27-0ubuntu0.19.04.1

or

TiDB 3.0 :D

Test case or example project

schema and data:

CREATE TABLE `t` (
  `v` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

insert into t values(null),(1);

mapper:

public interface TiMapper {
    @Select("select v from t")
    Cursor<String> queryTest();
}

test:

    @Resource
    private TiMapper tiMapper;

    @Transactional
    public void testCursorFetch() throws Exception {
        Cursor<String> cc = tiMapper.queryTest();
        final Iterator<String> iterator = cc.iterator();
        System.out.println(iterator.hasNext()); 
     }

Steps to reproduce

run testCursorFetch method

Expected result

        System.out.println(iterator.hasNext()); 

should be true

Actual result

be false...and 1 record has no chance to be output.

and if first record is NOT null, it will be true

we can work around this to change mapper to

    @Select("select v from t")
    Cursor<Map<String, Object>> queryTest();

and set mybatis.configuration.returnInstanceForEmptyRow=true

but IMHO, current cursor' impl is not very suitable, maybe many people meet this question

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions