Skip to content

Commit 86a74cb

Browse files
committed
Remove ridiculous UpdateByExample helpers - favor a general update
1 parent b962a19 commit 86a74cb

File tree

6 files changed

+69
-201
lines changed

6 files changed

+69
-201
lines changed

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

Lines changed: 0 additions & 86 deletions
This file was deleted.

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

Lines changed: 5 additions & 26 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 an "UpdateByExample" method in the style
25+
* Represents a function that can be used to create a multi-purpose 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,19 +32,10 @@
3232
* @UpdateProvider(type=SqlProviderAdapter.class, method="update")
3333
* int update(UpdateStatementProvider updateStatement);
3434
*
35-
* default MyBatis3UpdateByExampleCompleter updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
36-
* return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
37-
* .withHelper(helper)
38-
* .withMapper(this::update)
39-
* .withTable(simpleTable)
40-
* .withValueSetter((record, dsl) ->
41-
* dsl.set(id).equalToWhenPresent(record::getId)
42-
* .set(firstName).equalToWhenPresent(record::getFirstName)
43-
* .set(lastName).equalToWhenPresent(record::getLastName)
44-
* .set(birthDate).equalToWhenPresent(record::getBirthDate)
45-
* .set(employed).equalToWhenPresent(record::getEmployed)
46-
* .set(occupation).equalToWhenPresent(record::getOccupation))
47-
* .build();
35+
* default int update(MyBatis3UpdateByExampleHelper helper) {
36+
* return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
37+
* .build()
38+
* .execute();
4839
* }
4940
* </pre>
5041
*
@@ -71,21 +62,9 @@
7162
* .usingRecord(record);
7263
* </pre>
7364
*
74-
* @see MyBatis3UpdateByExampleCompleter
75-
* @see MyBatis3UpdateByExampleValueSetter
76-
*
7765
* @author Jeff Butler
7866
*/
7967
@FunctionalInterface
8068
public interface MyBatis3UpdateByExampleHelper extends
8169
Function<UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, Buildable<MyBatis3UpdateModelAdapter<Integer>>> {
82-
83-
/**
84-
* Returns a helper that can be used to update every row in a table.
85-
*
86-
* @return the helper that will update every row in a table
87-
*/
88-
static MyBatis3UpdateByExampleHelper allRows() {
89-
return h -> h;
90-
}
9170
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/site/markdown/docs/mybatis3.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ This support is added to the DELETE, SELECT, and UPDATE statement generators and
55

66
With version 1.1.3, specialized interfaces were added that can further simplify client code.
77

8-
For example, it is possible to write a mapper interface like this:
8+
## CountByExample Support
9+
10+
## DeleteByExample Support
11+
12+
## SelectByExample Support
13+
14+
Support for SelectByExample allows creating reusable methods where the user only need specify a where clause.
915

1016
```java
1117
import static examples.simple.SimpleTableDynamicSqlSupport.*;
@@ -31,7 +37,7 @@ public interface SimpleTableMapper {
3137
@Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR),
3238
@Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR),
3339
@Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE),
34-
@Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class),
40+
@Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR),
3541
@Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR)
3642
})
3743
List<SimpleTableRecord> selectMany(SelectStatementProvider selectStatement);
@@ -55,6 +61,20 @@ The code is used like this:
5561
.or(occupation, isNull()));
5662
```
5763

64+
The following query will select all rows:
65+
66+
```java
67+
List<SimpleTableRecord> rows =
68+
mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
69+
```
70+
71+
The following query will select all rows in a specified order:
72+
73+
```java
74+
List<SimpleTableRecord> rows =
75+
mapper.selectByExample(MyBatis3SelectByExampleHelper.allRowsOrderedBy(lastName, firstName));
76+
```
77+
5878

5979
It is expected that MyBatis Generator will generate code that looks like this.
6080

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

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3939
import org.mybatis.dynamic.sql.select.SelectDSL;
4040
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
41+
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
4142
import org.mybatis.dynamic.sql.update.UpdateDSL;
4243
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
4344
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
4445
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper;
4546
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
4647
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
47-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleCompleter;
4848
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
4949

5050
/**
@@ -171,57 +171,49 @@ default SimpleTableRecord selectByPrimaryKey(Integer id_) {
171171
.execute();
172172
}
173173

174-
default MyBatis3UpdateByExampleCompleter<SimpleTableRecord> updateByExample(MyBatis3UpdateByExampleHelper helper) {
175-
return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
176-
.withHelper(helper)
177-
.withMapper(this::update)
178-
.withTable(simpleTable)
179-
.withValueSetter((record, dsl) ->
180-
dsl.set(id).equalTo(record::getId)
181-
.set(firstName).equalTo(record::getFirstName)
182-
.set(lastName).equalTo(record::getLastName)
183-
.set(birthDate).equalTo(record::getBirthDate)
184-
.set(employed).equalTo(record::getEmployed)
185-
.set(occupation).equalTo(record::getOccupation))
186-
.build();
187-
}
188-
189-
default MyBatis3UpdateByExampleCompleter<SimpleTableRecord> updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
190-
return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
191-
.withHelper(helper)
192-
.withMapper(this::update)
193-
.withTable(simpleTable)
194-
.withValueSetter((record, dsl) ->
195-
dsl.set(id).equalToWhenPresent(record::getId)
196-
.set(firstName).equalToWhenPresent(record::getFirstName)
197-
.set(lastName).equalToWhenPresent(record::getLastName)
198-
.set(birthDate).equalToWhenPresent(record::getBirthDate)
199-
.set(employed).equalToWhenPresent(record::getEmployed)
200-
.set(occupation).equalToWhenPresent(record::getOccupation))
201-
.build();
174+
default int update(MyBatis3UpdateByExampleHelper helper) {
175+
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
176+
.build()
177+
.execute();
202178
}
203179

204-
default int updateByPrimaryKey(SimpleTableRecord record) {
205-
return UpdateDSL.updateWithMapper(this::update, simpleTable)
180+
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setAll(SimpleTableRecord record, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
181+
return dsl.set(id).equalTo(record::getId)
206182
.set(firstName).equalTo(record::getFirstName)
207183
.set(lastName).equalTo(record::getLastName)
208184
.set(birthDate).equalTo(record::getBirthDate)
209185
.set(employed).equalTo(record::getEmployed)
210-
.set(occupation).equalTo(record::getOccupation)
211-
.where(id, isEqualTo(record::getId))
212-
.build()
213-
.execute();
186+
.set(occupation).equalTo(record::getOccupation);
214187
}
215-
216-
default int updateByPrimaryKeySelective(SimpleTableRecord record) {
217-
return UpdateDSL.updateWithMapper(this::update, simpleTable)
188+
189+
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setSelective(SimpleTableRecord record, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
190+
return dsl.set(id).equalToWhenPresent(record::getId)
218191
.set(firstName).equalToWhenPresent(record::getFirstName)
219192
.set(lastName).equalToWhenPresent(record::getLastName)
220193
.set(birthDate).equalToWhenPresent(record::getBirthDate)
221194
.set(employed).equalToWhenPresent(record::getEmployed)
222-
.set(occupation).equalToWhenPresent(record::getOccupation)
223-
.where(id, isEqualTo(record::getId))
224-
.build()
225-
.execute();
195+
.set(occupation).equalToWhenPresent(record::getOccupation);
196+
}
197+
198+
default int updateByPrimaryKey(SimpleTableRecord record) {
199+
return update(h ->
200+
h.set(firstName).equalTo(record::getFirstName)
201+
.set(lastName).equalTo(record::getLastName)
202+
.set(birthDate).equalTo(record::getBirthDate)
203+
.set(employed).equalTo(record::getEmployed)
204+
.set(occupation).equalTo(record::getOccupation)
205+
.where(id, isEqualTo(record::getId))
206+
);
207+
}
208+
209+
default int updateByPrimaryKeySelective(SimpleTableRecord record) {
210+
return update(h ->
211+
h.set(firstName).equalToWhenPresent(record::getFirstName)
212+
.set(lastName).equalToWhenPresent(record::getLastName)
213+
.set(birthDate).equalToWhenPresent(record::getBirthDate)
214+
.set(employed).equalToWhenPresent(record::getEmployed)
215+
.set(occupation).equalToWhenPresent(record::getOccupation)
216+
.where(id, isEqualTo(record::getId))
217+
);
226218
}
227219
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper;
4242
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
4343
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
44-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
4544

4645
public class SimpleTableAnnotatedNewStyleMapperTest {
4746

@@ -314,10 +313,10 @@ public void testUpdateByExample() {
314313

315314
record.setOccupation("Programmer");
316315

317-
rows = mapper.updateByExample(q ->
318-
q.where(id, isEqualTo(100))
319-
.and(firstName, isEqualTo("Joe")))
320-
.usingRecord(record);
316+
rows = mapper.update(dsl ->
317+
SimpleTableAnnotatedMapperNewStyle.setAll(record, dsl)
318+
.where(id, isEqualTo(100))
319+
.and(firstName, isEqualTo("Joe")));
321320

322321
assertThat(rows).isEqualTo(1);
323322

@@ -341,9 +340,10 @@ public void testUpdateAll() {
341340
int rows = mapper.insert(record);
342341
assertThat(rows).isEqualTo(1);
343342

344-
record = new SimpleTableRecord();
345-
record.setOccupation("Programmer");
346-
rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows()).usingRecord(record);
343+
SimpleTableRecord updateRecord = new SimpleTableRecord();
344+
updateRecord.setOccupation("Programmer");
345+
rows = mapper.update(dsl ->
346+
SimpleTableAnnotatedMapperNewStyle.setSelective(updateRecord, dsl));
347347

348348
assertThat(rows).isEqualTo(7);
349349

0 commit comments

Comments
 (0)