1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import org.apache.ibatis.cursor.Cursor; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.stream.StreamSupport;
@Service public class DemoService {
@Autowired private DemoMapper demoMapper; @Autowired private SqlSessionFactory sqlSessionFactory; @Resource PlatformTransactionManager platformTransactionManager;
public List<Demo> getDataFromMysql() {
List<Demo> demos = new ArrayList<>();
SqlSession sqlSession = sqlSessionFactory.openSession(); try (Cursor<Demo> cursor = sqlSession.getMapper(DemoMapper.class).getDataFromMysql(2);){ StreamSupport.stream(cursor.spliterator(), true).forEach( demo -> { demos.add(demo); } ); } catch (Exception e) { throw new RuntimeException(e); }
return demos; } }
|