-
-
Save jeffgbutler/5df477b4f72f5461a8cc32fb7cf4669b to your computer and use it in GitHub Desktop.
@Select({ | |
"${selectStatement}", | |
"LIMIT #{parameters.limit} OFFSET #{parameters.offset}" | |
}) | |
@ResultMap("TLecturesResult") | |
List<TLectures> selectByExampleWithLimitAndOffset(SelectStatementProvider selectStatement); |
@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(); | |
} | |
} | |
); | |
} |
The library now supports limit and offset directly, so you don't need this workaround anymore.
Are you using this pattern for something else?
This directory contains an example of a better pattern for modifying the parameters and generated SQL: https://github.com/mybatis/mybatis-dynamic-sql/tree/master/src/test/java/examples/paging
Thank you so much for the example. It really inspires me.
The problem with the library support is that both limit()
and offset()
return a finisher object. I cannot use both limit and offset functions in one statement. Is there any way I didn't discover?
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.
This gist does not work any more, since the parameters are in an unmutable map. Is there new ways to set the parameters? Or is there better ways to achieve this function?
Thank you!