Skip to content

Commit f88bca2

Browse files
committed
Add non-boxing adapters and corresponding utility functions
1 parent f051575 commit f88bca2

File tree

7 files changed

+188
-13
lines changed

7 files changed

+188
-13
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.function.Function;
1919

2020
import org.mybatis.dynamic.sql.delete.DeleteDSL;
21-
import org.mybatis.dynamic.sql.delete.MyBatis3DeleteModelAdapter;
2221
import org.mybatis.dynamic.sql.util.Buildable;
2322

2423
/**
@@ -62,7 +61,7 @@
6261
*/
6362
@FunctionalInterface
6463
public interface MyBatis3DeleteHelper extends
65-
Function<DeleteDSL<MyBatis3DeleteModelAdapter<Integer>>, Buildable<MyBatis3DeleteModelAdapter<Integer>>> {
64+
Function<DeleteDSL<MyBatis3DeleteModelToIntAdapter>, Buildable<MyBatis3DeleteModelToIntAdapter>> {
6665

6766
/**
6867
* Returns a helper that can be used to delete every row in a table.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.delete.DeleteModel;
22+
import org.mybatis.dynamic.sql.delete.MyBatis3DeleteModelAdapter;
23+
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
24+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
25+
26+
/**
27+
* This adapter will render the underlying delete model for MyBatis3, and then call a MyBatis mapper method.
28+
* This version of the adapter is preferred over {@link MyBatis3DeleteModelAdapter} because it is not generic
29+
* and does not force a box/unbox for mappers that always return a primitive int.
30+
*
31+
* @see MyBatis3Utils
32+
*
33+
* @author Jeff Butler
34+
*
35+
*/
36+
public class MyBatis3DeleteModelToIntAdapter {
37+
38+
private DeleteModel deleteModel;
39+
private ToIntFunction<DeleteStatementProvider> mapperMethod;
40+
41+
private MyBatis3DeleteModelToIntAdapter(DeleteModel deleteModel,
42+
ToIntFunction<DeleteStatementProvider> mapperMethod) {
43+
this.deleteModel = Objects.requireNonNull(deleteModel);
44+
this.mapperMethod = Objects.requireNonNull(mapperMethod);
45+
}
46+
47+
public int execute() {
48+
return mapperMethod.applyAsInt(deleteStatement());
49+
}
50+
51+
private DeleteStatementProvider deleteStatement() {
52+
return deleteModel.render(RenderingStrategy.MYBATIS3);
53+
}
54+
55+
public static MyBatis3DeleteModelToIntAdapter of(DeleteModel deleteModel,
56+
ToIntFunction<DeleteStatementProvider> mapperMethod) {
57+
return new MyBatis3DeleteModelToIntAdapter(deleteModel, mapperMethod);
58+
}
59+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.function.Function;
1919

20-
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
2120
import org.mybatis.dynamic.sql.update.UpdateDSL;
2221
import org.mybatis.dynamic.sql.util.Buildable;
2322

@@ -86,5 +85,5 @@
8685
*/
8786
@FunctionalInterface
8887
public interface MyBatis3UpdateHelper extends
89-
Function<UpdateDSL<MyBatis3UpdateModelAdapter<Integer>>, Buildable<MyBatis3UpdateModelAdapter<Integer>>> {
88+
Function<UpdateDSL<MyBatis3UpdateModelToIntAdapter>, Buildable<MyBatis3UpdateModelToIntAdapter>> {
9089
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.render.RenderingStrategy;
22+
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
23+
import org.mybatis.dynamic.sql.update.UpdateModel;
24+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
25+
26+
/**
27+
* This adapter will render the underlying update model for MyBatis3, and then call a MyBatis mapper method.
28+
* This version of the adapter is preferred over {@link MyBatis3UpdateModelAdapter} because it is not generic
29+
* and does not force a box/unbox for mappers that always return a primitive int.
30+
*
31+
* @see MyBatis3Utils
32+
*
33+
* @author Jeff Butler
34+
*
35+
*/
36+
public class MyBatis3UpdateModelToIntAdapter {
37+
38+
private UpdateModel updateModel;
39+
private ToIntFunction<UpdateStatementProvider> mapperMethod;
40+
41+
private MyBatis3UpdateModelToIntAdapter(UpdateModel updateModel,
42+
ToIntFunction<UpdateStatementProvider> mapperMethod) {
43+
this.updateModel = Objects.requireNonNull(updateModel);
44+
this.mapperMethod = Objects.requireNonNull(mapperMethod);
45+
}
46+
47+
public int execute() {
48+
return mapperMethod.applyAsInt(updateStatement());
49+
}
50+
51+
private UpdateStatementProvider updateStatement() {
52+
return updateModel.render(RenderingStrategy.MYBATIS3);
53+
}
54+
55+
public static MyBatis3UpdateModelToIntAdapter of(UpdateModel updateModel,
56+
ToIntFunction<UpdateStatementProvider> mapperMethod) {
57+
return new MyBatis3UpdateModelToIntAdapter(updateModel, mapperMethod);
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.ToIntFunction;
19+
20+
import org.mybatis.dynamic.sql.SqlTable;
21+
import org.mybatis.dynamic.sql.delete.DeleteDSL;
22+
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
23+
import org.mybatis.dynamic.sql.update.UpdateDSL;
24+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
25+
26+
/**
27+
* Utility functions for building MyBatis3 mappers
28+
*
29+
* @author Jeff Butler
30+
*
31+
*/
32+
public class MyBatis3Utils {
33+
private MyBatis3Utils() {}
34+
35+
/**
36+
* Initiates a delete statement using the non-boxing adapter
37+
*
38+
* @param mapper a MyBatis3 mapper delete method
39+
* @param table the table to delete from
40+
* @return a partially completed DeleteDSL
41+
*/
42+
public static DeleteDSL<MyBatis3DeleteModelToIntAdapter> deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
43+
SqlTable table) {
44+
return DeleteDSL.deleteFrom(dm -> MyBatis3DeleteModelToIntAdapter.of(dm, mapper), table);
45+
}
46+
47+
/**
48+
* Initiates an update statement using the non-boxing adapter
49+
*
50+
* @param mapper a MyBatis3 mapper update method
51+
* @param table the table to update
52+
* @return a partially completed UpdateDSL
53+
*/
54+
public static UpdateDSL<MyBatis3UpdateModelToIntAdapter> update(ToIntFunction<UpdateStatementProvider> mapper,
55+
SqlTable table) {
56+
return UpdateDSL.update(um -> MyBatis3UpdateModelToIntAdapter.of(um, mapper), table);
57+
}
58+
}

src/site/markdown/docs/mybatis3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ This is a standard method for MyBatis Dynamic SQL that executes a delete and ret
5555

5656
```java
5757
default int delete(MyBatis3DeleteHelper helper) {
58-
return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable))
58+
return helper.apply(MyBatis3Utils.deleteFrom(this::delete, simpleTable))
5959
.build()
6060
.execute();
6161
}
@@ -172,7 +172,7 @@ This is a standard method for MyBatis Dynamic SQL that executes a query and retu
172172

173173
```java
174174
default int update(MyBatis3UpdateHelper helper) {
175-
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
175+
return helper.apply(MyBatis3Utils.update(this::update, simpleTable))
176176
.build()
177177
.execute();
178178
}

src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyle.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@
3131
import org.apache.ibatis.annotations.UpdateProvider;
3232
import org.apache.ibatis.type.JdbcType;
3333
import org.mybatis.dynamic.sql.SqlBuilder;
34-
import org.mybatis.dynamic.sql.delete.DeleteDSL;
3534
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
3635
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
3736
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
3837
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3938
import org.mybatis.dynamic.sql.select.SelectDSL;
4039
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
41-
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
4240
import org.mybatis.dynamic.sql.update.UpdateDSL;
4341
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
4442
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
4543
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper;
4644
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper;
4745
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectHelper;
4846
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateHelper;
47+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateModelToIntAdapter;
48+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
4949

5050
import examples.simple.LastNameTypeHandler;
5151
import examples.simple.SimpleTableRecord;
@@ -98,7 +98,7 @@ default long count(MyBatis3CountHelper helper) {
9898
}
9999

100100
default int delete(MyBatis3DeleteHelper helper) {
101-
return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable))
101+
return helper.apply(MyBatis3Utils.deleteFrom(this::delete, simpleTable))
102102
.build()
103103
.execute();
104104
}
@@ -171,12 +171,13 @@ default Optional<SimpleTableRecord> selectByPrimaryKey(Integer id_) {
171171
}
172172

173173
default int update(MyBatis3UpdateHelper helper) {
174-
return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
174+
return helper.apply(MyBatis3Utils.update(this::update, simpleTable))
175175
.build()
176176
.execute();
177177
}
178178

179-
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setAll(SimpleTableRecord record, UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
179+
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setAll(SimpleTableRecord record,
180+
UpdateDSL<MyBatis3UpdateModelToIntAdapter> dsl) {
180181
return dsl.set(id).equalTo(record::getId)
181182
.set(firstName).equalTo(record::getFirstName)
182183
.set(lastName).equalTo(record::getLastName)
@@ -185,8 +186,8 @@ static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setAll(SimpleTableRecord r
185186
.set(occupation).equalTo(record::getOccupation);
186187
}
187188

188-
static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setSelective(SimpleTableRecord record,
189-
UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
189+
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setSelective(SimpleTableRecord record,
190+
UpdateDSL<MyBatis3UpdateModelToIntAdapter> dsl) {
190191
return dsl.set(id).equalToWhenPresent(record::getId)
191192
.set(firstName).equalToWhenPresent(record::getFirstName)
192193
.set(lastName).equalToWhenPresent(record::getLastName)

0 commit comments

Comments
 (0)