Last active
December 30, 2021 13:18
-
-
Save jeffgbutler/5df477b4f72f5461a8cc32fb7cf4669b to your computer and use it in GitHub Desktop.
MyBatis Dynamic SQL Limit and Offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Select({ | |
"${selectStatement}", | |
"LIMIT #{parameters.limit} OFFSET #{parameters.offset}" | |
}) | |
@ResultMap("TLecturesResult") | |
List<TLectures> selectByExampleWithLimitAndOffset(SelectStatementProvider selectStatement); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void selectByExampleWithLimitAndOffset() throws JsonProcessingException { | |
SelectStatementProvider selectStatement = select(id, ispublish) | |
.from(TLecturesDynamicSqlSupport.TLectures) | |
.where(id, isEqualTo("id")) | |
.orderBy(ispublish.descending()) | |
.build() | |
.render(RenderingStrategy.MYBATIS3); | |
selectStatement.getParameters().put("limit", 1); | |
selectStatement.getParameters().put("offset", 1); | |
List<TLectures> tLectures = tLecturesMapper.selectByExampleWithLimitAndOffset(selectStatement); | |
tLectures.forEach( | |
(tLecture) -> { | |
try { | |
objectMapper.writeValueAsString(tLectures); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} | |
} | |
); | |
} |
Here's an example of using both in one statement: https://github.com/mybatis/mybatis-dynamic-sql/blob/b1fa3a6562c3ccf6798a64651def0e5019d5ac8d/src/test/java/examples/animal/data/LimitAndOffsetTest.java#L69-L74
Turns out I really missed something. Sorry for taking your time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for the example. It really inspires me.
The problem with the library support is that both
limit()
andoffset()
return a finisher object. I cannot use both limit and offset functions in one statement. Is there any way I didn't discover?