Mybatis return large result with xml configuration in spring

后端 未结 2 1960
心在旅途
心在旅途 2021-02-10 15:59

I need to dump data from a table in oracle to elasticsearch(100 Million record), My memory limit of JVM is 256M, I use the following code and config to get the data from oracle

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 16:27

    There are two options:

    1. use ResultHandler
    2. since 3.4.1 use Cursor

    ResultHandler

    That is how you can use custom ResultHandler:

    PersonMapper.xml

    
      
        
        
      
      
    
    

    PersonMapper.java

    public interface PersonMapper {
         void selectAllPersons(ResultHandler handler);
    }
    

    MyService.java

    class PersonResultHandler implements ResultHandler {
        @Override
        public void handleResult(ResultContext context) { 
            Person person = (Person)context.getResultObject(); 
            // process person here
        }
    };
    PersonResultHandler handler = new PersonResultHandler();
    PersonMapper personMapper = ...;
    personMapper.selectAllPersons(handler);
    

    Cursor

    Starting from mybatis 3.4.1 you can return Cursor which is Iterable and can be used like this (under condition that result is ordered, see above Cursor API java doc for details):

    PersonMapper.java

    public interface PersonMapper {
         Cursor selectAllPersons();
    }
    

    MyService.java

    PersonMapper personMapper = ...;
    try (Cursor persons = personMapper.selectAllPersons()) {
       for (Person person : persons) {
          // process one person
       }
    }
    

提交回复
热议问题