View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package examples.paging;
17  
18  import static examples.animal.data.AnimalDataDynamicSqlSupport.id;
19  import static org.assertj.core.api.Assertions.assertThat;
20  
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.sql.Connection;
24  import java.sql.DriverManager;
25  import java.util.List;
26  
27  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
28  import org.apache.ibatis.jdbc.ScriptRunner;
29  import org.apache.ibatis.mapping.Environment;
30  import org.apache.ibatis.session.Configuration;
31  import org.apache.ibatis.session.SqlSession;
32  import org.apache.ibatis.session.SqlSessionFactory;
33  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  import examples.animal.data.AnimalData;
39  
40  class LimitAndOffsetTest {
41  
42      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
43      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
44  
45      private SqlSessionFactory sqlSessionFactory;
46  
47      @BeforeEach
48      void setup() throws Exception {
49          Class.forName(JDBC_DRIVER);
50          InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql");
51          assert is != null;
52          try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
53              ScriptRunner sr = new ScriptRunner(connection);
54              sr.setLogWriter(null);
55              sr.runScript(new InputStreamReader(is));
56          }
57  
58          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
59          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
60          Configuration config = new Configuration(environment);
61          config.addMapper(LimitAndOffsetMapper.class);
62          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
63      }
64  
65      @Test
66      void testLimitAndOffset() {
67          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68              LimitAndOffsetMapper mapper = sqlSession.getMapper(LimitAndOffsetMapper.class);
69  
70              List<AnimalData> rows = mapper.selectWithLimitAndOffset(5, 3)
71                      .orderBy(id)
72                      .build()
73                      .execute();
74  
75              assertThat(rows).hasSize(5);
76              assertThat(rows.get(0).id()).isEqualTo(4);
77          }
78      }
79  }