Skip to content

Commit 18c3493

Browse files
committed
Say goodbye to "by example"
1 parent 86a74cb commit 18c3493

File tree

6 files changed

+91
-71
lines changed

6 files changed

+91
-71
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.mybatis.dynamic.sql.util.Buildable;
2323

2424
/**
25-
* Represents a function that can be used to create a "CountByExample" method in the style
25+
* Represents a function that can be used to create a general count method in the style
2626
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
2727
* call the build().execute() methods - making client code look a bit cleaner.
2828
*
@@ -32,7 +32,7 @@
3232
* @SelectProvider(type=SqlProviderAdapter.class, method="select")
3333
* long count(SelectStatementProvider selectStatement);
3434
*
35-
* default long countByExample(MyBatis3CountByExampleHelper helper) {
35+
* default long count(MyBatis3CountHelper helper) {
3636
* return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
3737
* .from(simpleTable))
3838
* .build()
@@ -43,34 +43,34 @@
4343
* <p>And then call the simplified default method like this:
4444
*
4545
* <pre>
46-
* long rows = mapper.countByExample(q -&gt;
46+
* long rows = mapper.count(q -&gt;
4747
* q.where(occupation, isNull()));
4848
* </pre>
4949
*
5050
* <p>You can implement a "count all" with the following code:
5151
*
5252
* <pre>
53-
* long rows = mapper.countByExample(q -&gt; q);
53+
* long rows = mapper.count(q -&gt; q);
5454
* </pre>
5555
*
5656
* <p>Or
5757
*
5858
* <pre>
59-
* long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows());
59+
* long rows = mapper.count(MyBatis3CountHelper.allRows());
6060
* </pre>
6161
*
6262
* @author Jeff Butler
6363
*/
6464
@FunctionalInterface
65-
public interface MyBatis3CountByExampleHelper extends
65+
public interface MyBatis3CountHelper extends
6666
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>>, Buildable<MyBatis3SelectModelAdapter<Long>>> {
6767

6868
/**
6969
* Returns a helper that can be used to count every row in a table.
7070
*
7171
* @return the helper that will count every row in a table
7272
*/
73-
static MyBatis3CountByExampleHelper allRows() {
73+
static MyBatis3CountHelper allRows() {
7474
return h -> h;
7575
}
7676
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.mybatis.dynamic.sql.util.Buildable;
2323

2424
/**
25-
* Represents a function that can be used to create a "DeleteByExample" method in the style
25+
* Represents a function that can be used to create a general delete method in the style
2626
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
2727
* call the build().execute() methods - making client code look a bit cleaner.
2828
*
@@ -32,7 +32,7 @@
3232
* &#64;DeleteProvider(type=SqlProviderAdapter.class, method="delete")
3333
* int delete(DeleteStatementProvider deleteStatement);
3434
*
35-
* default int deleteByExample(MyBatis3DeleteByExampleHelper helper) {
35+
* default int delete(MyBatis3DeleteHelper helper) {
3636
* return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable))
3737
* .build()
3838
* .execute();
@@ -42,34 +42,34 @@
4242
* <p>And then call the simplified default method like this:
4343
*
4444
* <pre>
45-
* int rows = mapper.deleteByExample(q -&gt;
45+
* int rows = mapper.delete(q -&gt;
4646
* q.where(occupation, isNull()));
4747
* </pre>
4848
*
4949
* <p>You can implement a "delete all" with the following code:
5050
*
5151
* <pre>
52-
* int rows = mapper.deleteByExample(q -&gt; q);
52+
* int rows = mapper.delete(q -&gt; q);
5353
* </pre>
5454
*
5555
* <p>Or
5656
*
5757
* <pre>
58-
* long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows());
58+
* long rows = mapper.delete(MyBatis3DeleteHelper.allRows());
5959
* </pre>
6060
6161
* @author Jeff Butler
6262
*/
6363
@FunctionalInterface
64-
public interface MyBatis3DeleteByExampleHelper extends
64+
public interface MyBatis3DeleteHelper extends
6565
Function<DeleteDSL<MyBatis3DeleteModelAdapter<Integer>>, Buildable<MyBatis3DeleteModelAdapter<Integer>>> {
6666

6767
/**
6868
* Returns a helper that can be used to delete every row in a table.
6969
*
7070
* @return the helper that will delete every row in a table
7171
*/
72-
static MyBatis3DeleteByExampleHelper allRows() {
72+
static MyBatis3DeleteHelper allRows() {
7373
return h -> h;
7474
}
7575
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.mybatis.dynamic.sql.util.Buildable;
2525

2626
/**
27-
* Represents a function that can be used to create a "SelectByExample" method in the style
27+
* Represents a function that can be used to create a general select method in the style
2828
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
2929
* call the build().execute() methods - making client code look a bit cleaner.
3030
*
@@ -42,7 +42,7 @@
4242
* })
4343
* List&lt;SimpleRecord&gt; selectMany(SelectStatementProvider selectStatement);
4444
*
45-
* default List&lt;SimpleRecord&gt; selectByExample(MyBatis3SelectByExampleHelper&lt;SimpleRecord&gt; helper) {
45+
* default List&lt;SimpleRecord&gt; select(MyBatis3SelectHelper&lt;SimpleRecord&gt; helper) {
4646
* return helper.apply(SelectDSL.selectWithMapper(this::selectMany, simpleTable.allColumns())
4747
* .from(simpleTable))
4848
* .build()
@@ -53,27 +53,27 @@
5353
* <p>And then call the simplified default method like this:
5454
*
5555
* <pre>
56-
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(q -&gt;
56+
* List&lt;SimpleRecord&gt; rows = mapper.select(q -&gt;
5757
* q.where(id, isEqualTo(1))
5858
* .or(occupation, isNull()));
5959
* </pre>
6060
*
6161
* <p>You can implement a "select all" with the following code:
6262
*
6363
* <pre>
64-
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(q -&gt; q);
64+
* List&lt;SimpleRecord&gt; rows = mapper.select(q -&gt; q);
6565
* </pre>
6666
*
6767
* <p>Or
6868
*
6969
* <pre>
70-
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
70+
* List&lt;SimpleRecord&gt; rows = mapper.select(MyBatis3SelectHelper.allRows());
7171
* </pre>
7272
*
7373
* @author Jeff Butler
7474
*/
7575
@FunctionalInterface
76-
public interface MyBatis3SelectByExampleHelper<T> extends
76+
public interface MyBatis3SelectHelper<T> extends
7777
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<List<T>>>,
7878
Buildable<MyBatis3SelectModelAdapter<List<T>>>> {
7979

@@ -84,7 +84,7 @@ public interface MyBatis3SelectByExampleHelper<T> extends
8484
*
8585
* @return the helper that will select every row in a table
8686
*/
87-
static <T> MyBatis3SelectByExampleHelper<T> allRows() {
87+
static <T> MyBatis3SelectHelper<T> allRows() {
8888
return h -> h;
8989
}
9090

@@ -96,7 +96,7 @@ static <T> MyBatis3SelectByExampleHelper<T> allRows() {
9696
*
9797
* @return the helper that will select every row in a table in the specified order
9898
*/
99-
static <T> MyBatis3SelectByExampleHelper<T> allRowsOrderdBy(SortSpecification...columns) {
99+
static <T> MyBatis3SelectHelper<T> allRowsOrderdBy(SortSpecification...columns) {
100100
return h -> h.orderBy(columns);
101101
}
102102
}

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.mybatis.dynamic.sql.util.Buildable;
2323

2424
/**
25-
* Represents a function that can be used to create a multi-purpose update method in the style
25+
* Represents a function that can be used to create a general update method in the style
2626
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
2727
* call the build().execute() methods - making client code look a bit cleaner.
2828
*
@@ -32,7 +32,7 @@
3232
* &#64;UpdateProvider(type=SqlProviderAdapter.class, method="update")
3333
* int update(UpdateStatementProvider updateStatement);
3434
*
35-
* default int update(MyBatis3UpdateByExampleHelper helper) {
35+
* default int update(MyBatis3UpdateHelper helper) {
3636
* return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
3737
* .build()
3838
* .execute();
@@ -42,29 +42,49 @@
4242
* <p>And then call the simplified default method like this:
4343
*
4444
* <pre>
45-
* int rows = mapper.updateByExampleSelective(q -&gt;
46-
* q.where(id, isEqualTo(100))
47-
* .and(firstName, isEqualTo("Joe")))
48-
* .usingRecord(record);
45+
* int rows = mapper.update(q -&gt;
46+
* q.set(firstName).equalTo("Fred")
47+
* .where(id, isEqualTo(100))
48+
* );
4949
* </pre>
5050
*
51-
* <p>You can implement an "update all" with the following code:
51+
* <p>You can implement an "update all" simply by omitting a where clause:
5252
*
5353
* <pre>
54-
* int rows = mapper.updateByExampleSelective(q -&gt; q)
55-
* .usingRecord(record);
54+
* int rows = mapper.update(q -&gt;
55+
* q.set(firstName).equalTo("Fred")
56+
* );
5657
* </pre>
5758
*
58-
* <p>Or
59+
* <p>You could also implement a helper method that would set fields based on values of a record. For example,
60+
* the following method would set all fields of a record based on whether or not the values are null:
5961
*
6062
* <pre>
61-
* int rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows())
62-
* .usingRecord(record);
63+
* static UpdateDSL&lt;MyBatis3UpdateModelAdapter&lt;Integer&gt;&gt; setSelective(SimpleTableRecord record,
64+
* UpdateDSL&lt;MyBatis3UpdateModelAdapter&lt;Integer&gt;&gt; dsl) {
65+
* return dsl.set(id).equalToWhenPresent(record::getId)
66+
* .set(firstName).equalToWhenPresent(record::getFirstName)
67+
* .set(lastName).equalToWhenPresent(record::getLastName)
68+
* .set(birthDate).equalToWhenPresent(record::getBirthDate)
69+
* .set(employed).equalToWhenPresent(record::getEmployed)
70+
* .set(occupation).equalToWhenPresent(record::getOccupation);
71+
* }
72+
* </pre>
73+
*
74+
* <p>The helper method could be used like this:
75+
*
76+
* <pre>
77+
* rows = mapper.update(dsl -&gt;
78+
* SimpleTableAnnotatedMapperNewStyle.setSelective(record, dsl)
79+
* .where(id, isLessThan(100)));
6380
* </pre>
6481
*
82+
* <p>In this way, you could mimic the function of the old style "updateByExampleSelective" methods from
83+
* MyBatis Generator.
84+
*
6585
* @author Jeff Butler
6686
*/
6787
@FunctionalInterface
68-
public interface MyBatis3UpdateByExampleHelper extends
88+
public interface MyBatis3UpdateHelper extends
6989
Function<UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, Buildable<MyBatis3UpdateModelAdapter<Integer>>> {
7090
}

src/test/java/examples/simple/SimpleTableAnnotatedMapperNewStyle.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
import org.mybatis.dynamic.sql.update.UpdateDSL;
4343
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
4444
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
45-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper;
46-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
47-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
48-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
45+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper;
46+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper;
47+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectHelper;
48+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateHelper;
4949

5050
/**
5151
*
52-
* Note: this is the canonical mapper with the new style ByExample methods
52+
* Note: this is the canonical mapper with the new style methods
5353
* and represents the desired output for MyBatis Generator
5454
*
5555
*/
@@ -90,24 +90,23 @@ public interface SimpleTableAnnotatedMapperNewStyle {
9090
@SelectProvider(type=SqlProviderAdapter.class, method="select")
9191
long count(SelectStatementProvider selectStatement);
9292

93-
default long countByExample(MyBatis3CountByExampleHelper helper) {
93+
default long count(MyBatis3CountHelper helper) {
9494
return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
9595
.from(simpleTable))
9696
.build()
9797
.execute();
9898
}
9999

100-
default int deleteByExample(MyBatis3DeleteByExampleHelper helper) {
100+
default int delete(MyBatis3DeleteHelper helper) {
101101
return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable))
102102
.build()
103103
.execute();
104104
}
105105

106106
default int deleteByPrimaryKey(Integer id_) {
107-
return DeleteDSL.deleteFromWithMapper(this::delete, simpleTable)
108-
.where(id, isEqualTo(id_))
109-
.build()
110-
.execute();
107+
return delete(h ->
108+
h.where(id, isEqualTo(id_))
109+
);
111110
}
112111

113112
default int insert(SimpleTableRecord record) {
@@ -149,14 +148,14 @@ default int insertSelective(SimpleTableRecord record) {
149148
.render(RenderingStrategy.MYBATIS3));
150149
}
151150

152-
default List<SimpleTableRecord> selectByExample(MyBatis3SelectByExampleHelper<SimpleTableRecord> helper) {
151+
default List<SimpleTableRecord> select(MyBatis3SelectHelper<SimpleTableRecord> helper) {
153152
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
154153
.from(simpleTable))
155154
.build()
156155
.execute();
157156
}
158157

159-
default List<SimpleTableRecord> selectDistinctByExample(MyBatis3SelectByExampleHelper<SimpleTableRecord> helper) {
158+
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectHelper<SimpleTableRecord> helper) {
160159
return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
161160
.from(simpleTable))
162161
.build()
@@ -171,7 +170,7 @@ default SimpleTableRecord selectByPrimaryKey(Integer id_) {
171170
.execute();
172171
}
173172

174-
default int update(MyBatis3UpdateByExampleHelper helper) {
173+
default int update(MyBatis3UpdateHelper helper) {
175174
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
176175
.build()
177176
.execute();
@@ -186,7 +185,8 @@ static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setAll(SimpleTableRecord r
186185
.set(occupation).equalTo(record::getOccupation);
187186
}
188187

189-
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setSelective(SimpleTableRecord record, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
188+
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setSelective(SimpleTableRecord record,
189+
UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
190190
return dsl.set(id).equalToWhenPresent(record::getId)
191191
.set(firstName).equalToWhenPresent(record::getFirstName)
192192
.set(lastName).equalToWhenPresent(record::getLastName)

0 commit comments

Comments
 (0)