Skip to content

Commit 8a62ffb

Browse files
committed
Add all() methods to the helpers
1 parent f7cba86 commit 8a62ffb

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,30 @@
4747
* q.where(occupation, isNull()));
4848
* </pre>
4949
*
50-
* <p>You can also do a "count all" with the following code:
50+
* <p>You can implement a "count all" with the following code:
5151
*
5252
* <pre>
5353
* long rows = mapper.countByExample(q -&gt; q);
5454
* </pre>
55+
*
56+
* <p>Or
57+
*
58+
* <pre>
59+
* long rows = mapper.countByExample(MyBatis3CountByExampleHelper.all());
60+
* </pre>
5561
*
5662
* @author Jeff Butler
5763
*/
5864
@FunctionalInterface
5965
public interface MyBatis3CountByExampleHelper extends
6066
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>>, Buildable<MyBatis3SelectModelAdapter<Long>>> {
67+
68+
/**
69+
* Returns a helper that can be used to count every row in a table.
70+
*
71+
* @return the helper that will count every row in a table
72+
*/
73+
static MyBatis3CountByExampleHelper all() {
74+
return h -> h;
75+
}
6176
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,30 @@
4646
* q.where(occupation, isNull()));
4747
* </pre>
4848
*
49-
* <p>You can also do a "delete all" with the following code:
49+
* <p>You can implement a "delete all" with the following code:
5050
*
5151
* <pre>
5252
* int rows = mapper.deleteByExample(q -&gt; q);
5353
* </pre>
5454
*
55+
* <p>Or
56+
*
57+
* <pre>
58+
* long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.all());
59+
* </pre>
60+
5561
* @author Jeff Butler
5662
*/
5763
@FunctionalInterface
5864
public interface MyBatis3DeleteByExampleHelper extends
5965
Function<DeleteDSL<MyBatis3DeleteModelAdapter<Integer>>, Buildable<MyBatis3DeleteModelAdapter<Integer>>> {
66+
67+
/**
68+
* Returns a helper that can be used to delete every row in a table.
69+
*
70+
* @return the helper that will delete every row in a table
71+
*/
72+
static MyBatis3DeleteByExampleHelper all() {
73+
return h -> h;
74+
}
6075
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,31 @@
5757
* .or(occupation, isNull()));
5858
* </pre>
5959
*
60-
* <p>You can also do a "select all" with the following code:
60+
* <p>You can implement a "select all" with the following code:
6161
*
6262
* <pre>
6363
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(q -&gt; q);
6464
* </pre>
6565
*
66+
* <p>Or
67+
*
68+
* <pre>
69+
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.all());
70+
* </pre>
71+
*
6672
* @author Jeff Butler
6773
*/
6874
@FunctionalInterface
6975
public interface MyBatis3SelectByExampleHelper<T> extends
7076
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<List<T>>>,
7177
Buildable<MyBatis3SelectModelAdapter<List<T>>>> {
78+
79+
/**
80+
* Returns a helper that can be used to select every row in a table.
81+
*
82+
* @return the helper that will select every row in a table
83+
*/
84+
static <T> MyBatis3SelectByExampleHelper<T> all() {
85+
return h -> h;
86+
}
7287
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,30 @@
5353
* .and(firstName, isEqualTo("Joe")));
5454
* </pre>
5555
*
56-
* <p>You can also do an "update all" with the following code:
56+
* <p>You can implement an "update all" with the following code:
5757
*
5858
* <pre>
5959
* int rows = mapper.updateByExampleSelective(record, q -&gt; q);
6060
* </pre>
6161
*
62+
* <p>Or
63+
*
64+
* <pre>
65+
* int rows = mapper.updateByExampleSelective(record, MyBatis3UpdateByExampleHelper.all());
66+
* </pre>
67+
*
6268
* @author Jeff Butler
6369
*/
6470
@FunctionalInterface
6571
public interface MyBatis3UpdateByExampleHelper extends
6672
Function<UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, Buildable<MyBatis3UpdateModelAdapter<Integer>>> {
73+
74+
/**
75+
* Returns a helper that can be used to update every row in a table.
76+
*
77+
* @return the helper that will update every row in a table
78+
*/
79+
static MyBatis3UpdateByExampleHelper all() {
80+
return h -> h;
81+
}
6782
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3939
import org.junit.jupiter.api.BeforeEach;
4040
import org.junit.jupiter.api.Test;
41+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper;
42+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
43+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
44+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
4145

4246
public class SimpleTableAnnotatedNewStyleMapperTest {
4347

@@ -81,7 +85,7 @@ public void testSelectAll() {
8185
try (SqlSession session = sqlSessionFactory.openSession()) {
8286
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
8387

84-
List<SimpleTableRecord> rows = mapper.selectByExample(q -> q);
88+
List<SimpleTableRecord> rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.all());
8589

8690
assertThat(rows.size()).isEqualTo(6);
8791
}
@@ -147,7 +151,7 @@ public void testDeleteByExample() {
147151
public void testDeleteAll() {
148152
try (SqlSession session = sqlSessionFactory.openSession()) {
149153
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
150-
int rows = mapper.deleteByExample(d -> d);
154+
int rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.all());
151155

152156
assertThat(rows).isEqualTo(6);
153157
}
@@ -323,7 +327,7 @@ public void testUpdateAll() {
323327

324328
record = new SimpleTableRecord();
325329
record.setOccupation("Programmer");
326-
rows = mapper.updateByExampleSelective(q -> q).usingRecord(record);
330+
rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.all()).usingRecord(record);
327331

328332
assertThat(rows).isEqualTo(7);
329333

@@ -347,7 +351,7 @@ public void testCountByExample() {
347351
public void testCountAll() {
348352
try (SqlSession session = sqlSessionFactory.openSession()) {
349353
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
350-
long rows = mapper.countByExample(q -> q);
354+
long rows = mapper.countByExample(MyBatis3CountByExampleHelper.all());
351355

352356
assertThat(rows).isEqualTo(6L);
353357
}

0 commit comments

Comments
 (0)