Skip to content

Commit a6e9d06

Browse files
committed
Improved UpdateByExample support
1 parent 8a62ffb commit a6e9d06

8 files changed

+66
-41
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* <p>Or
5757
*
5858
* <pre>
59-
* long rows = mapper.countByExample(MyBatis3CountByExampleHelper.all());
59+
* long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows());
6060
* </pre>
6161
*
6262
* @author Jeff Butler
@@ -70,7 +70,7 @@ public interface MyBatis3CountByExampleHelper extends
7070
*
7171
* @return the helper that will count every row in a table
7272
*/
73-
static MyBatis3CountByExampleHelper all() {
73+
static MyBatis3CountByExampleHelper allRows() {
7474
return h -> h;
7575
}
7676
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* <p>Or
5656
*
5757
* <pre>
58-
* long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.all());
58+
* long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows());
5959
* </pre>
6060
6161
* @author Jeff Butler
@@ -69,7 +69,7 @@ public interface MyBatis3DeleteByExampleHelper extends
6969
*
7070
* @return the helper that will delete every row in a table
7171
*/
72-
static MyBatis3DeleteByExampleHelper all() {
72+
static MyBatis3DeleteByExampleHelper allRows() {
7373
return h -> h;
7474
}
7575
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* <p>Or
6767
*
6868
* <pre>
69-
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.all());
69+
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
7070
* </pre>
7171
*
7272
* @author Jeff Butler
@@ -79,9 +79,11 @@ public interface MyBatis3SelectByExampleHelper<T> extends
7979
/**
8080
* Returns a helper that can be used to select every row in a table.
8181
*
82+
* @param <T> the type of row returned
83+
*
8284
* @return the helper that will select every row in a table
8385
*/
84-
static <T> MyBatis3SelectByExampleHelper<T> all() {
86+
static <T> MyBatis3SelectByExampleHelper<T> allRows() {
8587
return h -> h;
8688
}
8789
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@
2222
import org.mybatis.dynamic.sql.update.UpdateDSL;
2323
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
2424

25-
public class MyBatis3UpdateByExampleRecordGatherer<T> {
25+
/**
26+
* This class is used to complete an update by example method in the style of MyBatis generator.
27+
*
28+
* @author Jeff Butler
29+
*
30+
* @see MyBatis3UpdateByExampleHelper
31+
*
32+
* @param <T> the type of record that will be updated
33+
*/
34+
public class MyBatis3UpdateByExampleCompleter<T> {
2635
private SqlTable table;
2736
private MyBatis3UpdateByExampleHelper helper;
2837
private ToIntFunction<UpdateStatementProvider> mapper;
2938
private MyBatis3UpdateByExampleValueSetter<T> valueSetter;
3039

31-
private MyBatis3UpdateByExampleRecordGatherer(MyBatis3UpdateByExampleRecordGatherer.Builder<T> builder) {
40+
private MyBatis3UpdateByExampleCompleter(MyBatis3UpdateByExampleCompleter.Builder<T> builder) {
3241
helper = Objects.requireNonNull(builder.helper);
3342
mapper = Objects.requireNonNull(builder.mapper);
3443
valueSetter = Objects.requireNonNull(builder.valueSetter);
@@ -48,30 +57,30 @@ public static class Builder<T> {
4857
private ToIntFunction<UpdateStatementProvider> mapper;
4958
private MyBatis3UpdateByExampleValueSetter<T> valueSetter;
5059

51-
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withTable(SqlTable table) {
60+
public MyBatis3UpdateByExampleCompleter.Builder<T> withTable(SqlTable table) {
5261
this.table = table;
5362
return this;
5463
}
5564

56-
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withHelper(MyBatis3UpdateByExampleHelper helper) {
65+
public MyBatis3UpdateByExampleCompleter.Builder<T> withHelper(MyBatis3UpdateByExampleHelper helper) {
5766
this.helper = helper;
5867
return this;
5968
}
6069

61-
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withMapper(
70+
public MyBatis3UpdateByExampleCompleter.Builder<T> withMapper(
6271
ToIntFunction<UpdateStatementProvider> mapper) {
6372
this.mapper = mapper;
6473
return this;
6574
}
6675

67-
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withValueSetter(
76+
public MyBatis3UpdateByExampleCompleter.Builder<T> withValueSetter(
6877
MyBatis3UpdateByExampleValueSetter<T> valueSetter) {
6978
this.valueSetter = valueSetter;
7079
return this;
7180
}
7281

73-
public MyBatis3UpdateByExampleRecordGatherer<T> build() {
74-
return new MyBatis3UpdateByExampleRecordGatherer<>(this);
82+
public MyBatis3UpdateByExampleCompleter<T> build() {
83+
return new MyBatis3UpdateByExampleCompleter<>(this);
7584
}
7685
}
7786
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,48 @@
3232
* &#64;UpdateProvider(type=SqlProviderAdapter.class, method="update")
3333
* int update(UpdateStatementProvider updateStatement);
3434
*
35-
* default int updateByExampleSelective(SimpleTableRecord record, MyBatis3UpdateByExampleHelper helper) {
36-
* return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable)
37-
* .set(id).equalToWhenPresent(record.getId())
38-
* .set(firstName).equalToWhenPresent(record::getFirstName)
39-
* .set(lastName).equalToWhenPresent(record::getLastName)
40-
* .set(birthDate).equalToWhenPresent(record::getBirthDate)
41-
* .set(employed).equalToWhenPresent(record::getEmployed)
42-
* .set(occupation).equalToWhenPresent(record::getOccupation))
43-
* .build()
44-
* .execute();
35+
* default MyBatis3UpdateByExampleCompleter updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
36+
* return new MyBatis3UpdateByExampleCompleter.Builder&lt;SimpleTableRecord&gt;()
37+
* .withHelper(helper)
38+
* .withMapper(this::update)
39+
* .withTable(simpleTable)
40+
* .withValueSetter((record, dsl) -&gt;
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();
4548
* }
4649
* </pre>
4750
*
4851
* <p>And then call the simplified default method like this:
4952
*
5053
* <pre>
51-
* int rows = mapper.updateByExampleSelective(record, q -&gt;
54+
* int rows = mapper.updateByExampleSelective(q -&gt;
5255
* q.where(id, isEqualTo(100))
53-
* .and(firstName, isEqualTo("Joe")));
56+
* .and(firstName, isEqualTo("Joe")))
57+
* .usingRecord(record);
5458
* </pre>
5559
*
5660
* <p>You can implement an "update all" with the following code:
5761
*
5862
* <pre>
59-
* int rows = mapper.updateByExampleSelective(record, q -&gt; q);
63+
* int rows = mapper.updateByExampleSelective(q -&gt; q)
64+
* .usingRecord(record);
6065
* </pre>
6166
*
6267
* <p>Or
6368
*
6469
* <pre>
65-
* int rows = mapper.updateByExampleSelective(record, MyBatis3UpdateByExampleHelper.all());
70+
* int rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows())
71+
* .usingRecord(record);
6672
* </pre>
6773
*
74+
* @see MyBatis3UpdateByExampleCompleter
75+
* @see MyBatis3UpdateByExampleValueSetter
76+
*
6877
* @author Jeff Butler
6978
*/
7079
@FunctionalInterface
@@ -76,7 +85,7 @@ public interface MyBatis3UpdateByExampleHelper extends
7685
*
7786
* @return the helper that will update every row in a table
7887
*/
79-
static MyBatis3UpdateByExampleHelper all() {
88+
static MyBatis3UpdateByExampleHelper allRows() {
8089
return h -> h;
8190
}
8291
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
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
25+
* of MyBatis Generator. When using this function, you can create a method that will map record fields to
26+
* tables columns to be updated in a common mapper, and then allow a user to set a where clause as needed.
2427
*
2528
* @author Jeff Butler
2629
*
27-
* @param <R> the type of record that will be updated
30+
* @see MyBatis3UpdateByExampleHelper
31+
*
32+
* @param <T> the type of record that will be updated
2833
*/
2934
@FunctionalInterface
30-
public interface MyBatis3UpdateByExampleValueSetter<R> extends
31-
BiFunction<R, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>> {
35+
public interface MyBatis3UpdateByExampleValueSetter<T> extends
36+
BiFunction<T, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>> {
3237
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper;
4545
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
4646
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
47+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleCompleter;
4748
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
48-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleRecordGatherer;
4949

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

174-
default MyBatis3UpdateByExampleRecordGatherer<SimpleTableRecord> updateByExample(MyBatis3UpdateByExampleHelper helper) {
175-
return new MyBatis3UpdateByExampleRecordGatherer.Builder<SimpleTableRecord>()
174+
default MyBatis3UpdateByExampleCompleter<SimpleTableRecord> updateByExample(MyBatis3UpdateByExampleHelper helper) {
175+
return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
176176
.withHelper(helper)
177177
.withMapper(this::update)
178178
.withTable(simpleTable)
@@ -186,8 +186,8 @@ default MyBatis3UpdateByExampleRecordGatherer<SimpleTableRecord> updateByExample
186186
.build();
187187
}
188188

189-
default MyBatis3UpdateByExampleRecordGatherer<SimpleTableRecord> updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
190-
return new MyBatis3UpdateByExampleRecordGatherer.Builder<SimpleTableRecord>()
189+
default MyBatis3UpdateByExampleCompleter<SimpleTableRecord> updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
190+
return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
191191
.withHelper(helper)
192192
.withMapper(this::update)
193193
.withTable(simpleTable)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void testSelectAll() {
8585
try (SqlSession session = sqlSessionFactory.openSession()) {
8686
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
8787

88-
List<SimpleTableRecord> rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.all());
88+
List<SimpleTableRecord> rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
8989

9090
assertThat(rows.size()).isEqualTo(6);
9191
}
@@ -151,7 +151,7 @@ public void testDeleteByExample() {
151151
public void testDeleteAll() {
152152
try (SqlSession session = sqlSessionFactory.openSession()) {
153153
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
154-
int rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.all());
154+
int rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows());
155155

156156
assertThat(rows).isEqualTo(6);
157157
}
@@ -327,7 +327,7 @@ public void testUpdateAll() {
327327

328328
record = new SimpleTableRecord();
329329
record.setOccupation("Programmer");
330-
rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.all()).usingRecord(record);
330+
rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows()).usingRecord(record);
331331

332332
assertThat(rows).isEqualTo(7);
333333

@@ -351,7 +351,7 @@ public void testCountByExample() {
351351
public void testCountAll() {
352352
try (SqlSession session = sqlSessionFactory.openSession()) {
353353
SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class);
354-
long rows = mapper.countByExample(MyBatis3CountByExampleHelper.all());
354+
long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows());
355355

356356
assertThat(rows).isEqualTo(6L);
357357
}

0 commit comments

Comments
 (0)