Skip to content

Commit 0816aa3

Browse files
committed
Add a helper function to select all rows and specify order
1 parent 19f1dd0 commit 0816aa3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.function.Function;
2020

21+
import org.mybatis.dynamic.sql.SortSpecification;
2122
import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter;
2223
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
2324
import org.mybatis.dynamic.sql.util.Buildable;
@@ -86,4 +87,16 @@ public interface MyBatis3SelectByExampleHelper<T> extends
8687
static <T> MyBatis3SelectByExampleHelper<T> allRows() {
8788
return h -> h;
8889
}
90+
91+
/**
92+
* Returns a helper that can be used to select every row in a table with a specified sort order.
93+
*
94+
* @param <T> the type of row returned
95+
* @param columns sort columns
96+
*
97+
* @return the helper that will select every row in a table in the specified order
98+
*/
99+
static <T> MyBatis3SelectByExampleHelper<T> allRowsOrderdBy(SortSpecification...columns) {
100+
return h -> h.orderBy(columns);
101+
}
89102
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.mybatis.dynamic.sql.update.UpdateDSL;
2222

2323
/**
24-
* Represents a function that can be used to create an "UpdateByExample" method in the style
24+
* Represents a function that can be used to set values in an "UpdateByExample" method in the style
2525
* of MyBatis Generator. When using this function, you can create a method that will map record fields to
2626
* tables columns to be updated in a common mapper, and then allow a user to set a where clause as needed.
2727
*

src/test/java/examples/simple/SimpleTableAnnotatedNewStyleMapperTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public void testSelectAll() {
8888
List<SimpleTableRecord> rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
8989

9090
assertThat(rows.size()).isEqualTo(6);
91+
assertThat(rows.get(0).getId()).isEqualTo(1);
92+
assertThat(rows.get(5).getId()).isEqualTo(6);
93+
}
94+
}
95+
96+
@Test
97+
public void testSelectAllOrdered() {
98+
try (SqlSession session = sqlSessionFactory.openSession()) {
99+
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
100+
101+
List<SimpleTableRecord> rows = mapper
102+
.selectByExample(MyBatis3SelectByExampleHelper.allRowsOrderdBy(lastName.descending(), firstName.descending()));
103+
104+
assertThat(rows.size()).isEqualTo(6);
105+
assertThat(rows.get(0).getId()).isEqualTo(5);
106+
assertThat(rows.get(5).getId()).isEqualTo(1);
91107
}
92108
}
93109

0 commit comments

Comments
 (0)