Skip to content

Commit f7cba86

Browse files
committed
Improve UpdateByExample Code
1 parent 9b8c5eb commit f7cba86

File tree

4 files changed

+144
-26
lines changed

4 files changed

+144
-26
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.mybatis3;
17+
18+
import java.util.Objects;
19+
import java.util.function.ToIntFunction;
20+
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
import org.mybatis.dynamic.sql.update.UpdateDSL;
23+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
24+
25+
public class MyBatis3UpdateByExampleRecordGatherer<T> {
26+
private SqlTable table;
27+
private MyBatis3UpdateByExampleHelper helper;
28+
private ToIntFunction<UpdateStatementProvider> mapper;
29+
private MyBatis3UpdateByExampleValueSetter<T> valueSetter;
30+
31+
private MyBatis3UpdateByExampleRecordGatherer(MyBatis3UpdateByExampleRecordGatherer.Builder<T> builder) {
32+
helper = Objects.requireNonNull(builder.helper);
33+
mapper = Objects.requireNonNull(builder.mapper);
34+
valueSetter = Objects.requireNonNull(builder.valueSetter);
35+
table = Objects.requireNonNull(builder.table);
36+
}
37+
38+
public int usingRecord(T record) {
39+
return valueSetter.andThen(helper)
40+
.apply(record, UpdateDSL.updateWithMapper(p -> mapper.applyAsInt(p), table))
41+
.build()
42+
.execute();
43+
}
44+
45+
public static class Builder<T> {
46+
private SqlTable table;
47+
private MyBatis3UpdateByExampleHelper helper;
48+
private ToIntFunction<UpdateStatementProvider> mapper;
49+
private MyBatis3UpdateByExampleValueSetter<T> valueSetter;
50+
51+
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withTable(SqlTable table) {
52+
this.table = table;
53+
return this;
54+
}
55+
56+
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withHelper(MyBatis3UpdateByExampleHelper helper) {
57+
this.helper = helper;
58+
return this;
59+
}
60+
61+
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withMapper(
62+
ToIntFunction<UpdateStatementProvider> mapper) {
63+
this.mapper = mapper;
64+
return this;
65+
}
66+
67+
public MyBatis3UpdateByExampleRecordGatherer.Builder<T> withValueSetter(
68+
MyBatis3UpdateByExampleValueSetter<T> valueSetter) {
69+
this.valueSetter = valueSetter;
70+
return this;
71+
}
72+
73+
public MyBatis3UpdateByExampleRecordGatherer<T> build() {
74+
return new MyBatis3UpdateByExampleRecordGatherer<>(this);
75+
}
76+
}
77+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.mybatis3;
17+
18+
import java.util.function.BiFunction;
19+
20+
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
21+
import org.mybatis.dynamic.sql.update.UpdateDSL;
22+
23+
/**
24+
*
25+
* @author Jeff Butler
26+
*
27+
* @param <R> the type of record that will be updated
28+
*/
29+
@FunctionalInterface
30+
public interface MyBatis3UpdateByExampleValueSetter<R> extends
31+
BiFunction<R, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>> {
32+
}

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper;
4646
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
4747
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper;
48+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleRecordGatherer;
4849

4950
/**
5051
*
@@ -170,30 +171,36 @@ default SimpleTableRecord selectByPrimaryKey(Integer id_) {
170171
.execute();
171172
}
172173

173-
default int updateByExample(SimpleTableRecord record, MyBatis3UpdateByExampleHelper helper) {
174-
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable)
175-
.set(id).equalTo(record.getId())
176-
.set(firstName).equalTo(record::getFirstName)
177-
.set(lastName).equalTo(record::getLastName)
178-
.set(birthDate).equalTo(record::getBirthDate)
179-
.set(employed).equalTo(record::getEmployed)
180-
.set(occupation).equalTo(record::getOccupation))
181-
.build()
182-
.execute();
174+
default MyBatis3UpdateByExampleRecordGatherer<SimpleTableRecord> updateByExample(MyBatis3UpdateByExampleHelper helper) {
175+
return new MyBatis3UpdateByExampleRecordGatherer.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();
183187
}
184-
185-
default int updateByExampleSelective(SimpleTableRecord record, MyBatis3UpdateByExampleHelper helper) {
186-
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable)
187-
.set(id).equalToWhenPresent(record.getId())
188-
.set(firstName).equalToWhenPresent(record::getFirstName)
189-
.set(lastName).equalToWhenPresent(record::getLastName)
190-
.set(birthDate).equalToWhenPresent(record::getBirthDate)
191-
.set(employed).equalToWhenPresent(record::getEmployed)
192-
.set(occupation).equalToWhenPresent(record::getOccupation))
193-
.build()
194-
.execute();
188+
189+
default MyBatis3UpdateByExampleRecordGatherer<SimpleTableRecord> updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
190+
return new MyBatis3UpdateByExampleRecordGatherer.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();
195202
}
196-
203+
197204
default int updateByPrimaryKey(SimpleTableRecord record) {
198205
return UpdateDSL.updateWithMapper(this::update, simpleTable)
199206
.set(firstName).equalTo(record::getFirstName)

src/test/java/examples/simple/SimpleTableAnnotatedMapperTestNewStyle.java renamed to src/test/java/examples/simple/SimpleTableAnnotatedNewStyleMapperTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.junit.jupiter.api.BeforeEach;
4040
import org.junit.jupiter.api.Test;
4141

42-
public class SimpleTableAnnotatedMapperTestNewStyle {
42+
public class SimpleTableAnnotatedNewStyleMapperTest {
4343

4444
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
4545
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
@@ -293,9 +293,11 @@ public void testUpdateByExample() {
293293
assertThat(rows).isEqualTo(1);
294294

295295
record.setOccupation("Programmer");
296-
rows = mapper.updateByExample(record, q ->
296+
297+
rows = mapper.updateByExample(q ->
297298
q.where(id, isEqualTo(100))
298-
.and(firstName, isEqualTo("Joe")));
299+
.and(firstName, isEqualTo("Joe")))
300+
.usingRecord(record);
299301

300302
assertThat(rows).isEqualTo(1);
301303

@@ -321,7 +323,7 @@ public void testUpdateAll() {
321323

322324
record = new SimpleTableRecord();
323325
record.setOccupation("Programmer");
324-
rows = mapper.updateByExampleSelective(record, q -> q);
326+
rows = mapper.updateByExampleSelective(q -> q).usingRecord(record);
325327

326328
assertThat(rows).isEqualTo(7);
327329

0 commit comments

Comments
 (0)