Skip to content

Commit 85426df

Browse files
committed
Better pattern for decorating a select statement
1 parent e357439 commit 85426df

File tree

5 files changed

+82
-97
lines changed

5 files changed

+82
-97
lines changed

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
4040
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
4141
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
42-
import org.mybatis.dynamic.sql.select.SelectModel;
4342
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
4443
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
4544
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;

src/test/java/examples/paging/LimitAndOffsetAdapter.java

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 java.util.HashMap;
19+
import java.util.Map;
20+
21+
import org.jspecify.annotations.NullMarked;
22+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
23+
24+
@NullMarked
25+
public class LimitAndOffsetDecorator implements SelectStatementProvider {
26+
private final Map<String, Object> parameters = new HashMap<>();
27+
private final String selectStatement;
28+
29+
public LimitAndOffsetDecorator(int limit, int offset, SelectStatementProvider delegate) {
30+
parameters.putAll(delegate.getParameters());
31+
parameters.put("limit", limit);
32+
parameters.put("offset", offset);
33+
34+
selectStatement = delegate.getSelectStatement() +
35+
" LIMIT #{parameters.limit} OFFSET #{parameters.offset}";
36+
}
37+
38+
@Override
39+
public Map<String, Object> getParameters() {
40+
return parameters;
41+
}
42+
43+
@Override
44+
public String getSelectStatement() {
45+
return selectStatement;
46+
}
47+
}

src/test/java/examples/paging/LimitAndOffsetMapper.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package examples.paging;
1717

18+
import static examples.animal.data.AnimalDataDynamicSqlSupport.*;
19+
1820
import java.util.List;
1921

2022
import org.apache.ibatis.annotations.Result;
21-
import org.apache.ibatis.annotations.Results;
2223
import org.apache.ibatis.annotations.SelectProvider;
24+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
25+
import org.mybatis.dynamic.sql.select.SelectDSL;
26+
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
2327
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2428
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
2529

@@ -28,11 +32,16 @@
2832
public interface LimitAndOffsetMapper {
2933

3034
@SelectProvider(type=SqlProviderAdapter.class, method="select")
31-
@Results(id="AnimalDataResult", value={
32-
@Result(column="id", property="id", id=true),
33-
@Result(column="animal_name", property="animalName"),
34-
@Result(column="brain_weight", property="brainWeight"),
35-
@Result(column="body_weight", property="bodyWeight")
36-
})
35+
@Result(column="id", property="id", id=true)
36+
@Result(column="animal_name", property="animalName")
37+
@Result(column="brain_weight", property="brainWeight")
38+
@Result(column="body_weight", property="bodyWeight")
3739
List<AnimalData> selectMany(SelectStatementProvider selectStatement);
40+
41+
default List<AnimalData> selectWithLimitAndOffset(int limit, int offset, SelectDSLCompleter completer) {
42+
var dslStart = SelectDSL.select(id, animalName, brainWeight, bodyWeight).from(animalData);
43+
var selectStatement = completer.apply(dslStart).build().render(RenderingStrategies.MYBATIS3);
44+
var decorator = new LimitAndOffsetDecorator(limit, offset, selectStatement);
45+
return selectMany(decorator);
46+
}
3847
}

src/test/java/examples/paging/LimitAndOffsetTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.sql.DriverManager;
2626
import java.util.List;
2727

28+
import examples.animal.data.AnimalData;
2829
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2930
import org.apache.ibatis.jdbc.ScriptRunner;
3031
import org.apache.ibatis.mapping.Environment;
@@ -35,10 +36,8 @@
3536
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3637
import org.junit.jupiter.api.BeforeEach;
3738
import org.junit.jupiter.api.Test;
38-
39-
import examples.animal.data.AnimalData;
4039
import org.mybatis.dynamic.sql.SqlBuilder;
41-
import org.mybatis.dynamic.sql.select.SelectModel;
40+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
4241
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
4342

4443
class LimitAndOffsetTest {
@@ -71,14 +70,27 @@ void testLimitAndOffset() {
7170
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
7271
LimitAndOffsetMapper mapper = sqlSession.getMapper(LimitAndOffsetMapper.class);
7372

74-
SelectModel selectModel = SqlBuilder.select(id, animalName, brainWeight, bodyWeight)
73+
List<AnimalData> rows = mapper.selectWithLimitAndOffset(5, 3, d -> d.orderBy(id));
74+
75+
assertThat(rows).hasSize(5);
76+
assertThat(rows.get(0).getId()).isEqualTo(4);
77+
}
78+
}
79+
80+
@Test
81+
void testLimitAndOffsetDirect() {
82+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83+
LimitAndOffsetMapper mapper = sqlSession.getMapper(LimitAndOffsetMapper.class);
84+
85+
SelectStatementProvider selectStatement = SqlBuilder.select(id, animalName, brainWeight, bodyWeight)
7586
.from(animalData)
7687
.orderBy(id)
77-
.build();
88+
.build()
89+
.render(RenderingStrategies.MYBATIS3);
7890

79-
SelectStatementProvider selectStatement = new LimitAndOffsetAdapter(5, 3).apply(selectModel);
91+
SelectStatementProvider decorator = new LimitAndOffsetDecorator(5, 3, selectStatement);
8092

81-
List<AnimalData> rows = mapper.selectMany(selectStatement);
93+
List<AnimalData> rows = mapper.selectMany(decorator);
8294

8395
assertThat(rows).hasSize(5);
8496
assertThat(rows.get(0).getId()).isEqualTo(4);

0 commit comments

Comments
 (0)