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.springbatch;
17  
18  import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.mybatis.dynamic.sql.SqlBuilder.isLike;
21  import static org.mybatis.dynamic.sql.SqlBuilder.select;
22  
23  import org.junit.jupiter.api.Test;
24  import org.mybatis.dynamic.sql.util.springbatch.SpringBatchUtility;
25  
26  class SpringBatchRenderingTest {
27  
28      @Test
29      void renderLimit() {
30          var selectStatement = select(person.allColumns())
31                  .from(person)
32                  .where(firstName, isLike("%f%"))
33                  .limit(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE)
34                  .offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
35                  .build()
36                  .render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);
37  
38          assertThat(selectStatement.getSelectStatement())
39                  .isEqualTo("""
40                       select * \
41                       from person \
42                       where first_name like #{parameters.p1,jdbcType=VARCHAR} \
43                       limit #{_pagesize} \
44                       offset #{_skiprows}""");
45      }
46  
47      @Test
48      void renderFetchFirst() {
49          var selectStatement = select(person.allColumns())
50                  .from(person)
51                  .where(firstName, isLike("%f%"))
52                  .offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
53                  .fetchFirst(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE).rowsOnly()
54                  .build()
55                  .render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);
56  
57          assertThat(selectStatement.getSelectStatement())
58                  .isEqualTo("""
59                       select * \
60                       from person \
61                       where first_name like #{parameters.p1,jdbcType=VARCHAR} \
62                       offset #{_skiprows} rows \
63                       fetch first #{_pagesize} rows only""");
64      }
65  }