Skip to content

Commit c5b5e16

Browse files
committed
Add a selectOne helper
1 parent f88bca2 commit c5b5e16

File tree

6 files changed

+103
-29
lines changed

6 files changed

+103
-29
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* @author Jeff Butler
7474
*/
7575
@FunctionalInterface
76-
public interface MyBatis3SelectHelper<T> extends
76+
public interface MyBatis3SelectListHelper<T> extends
7777
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<List<T>>>,
7878
Buildable<MyBatis3SelectModelAdapter<List<T>>>> {
7979

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

@@ -96,7 +96,7 @@ static <T> MyBatis3SelectHelper<T> allRows() {
9696
*
9797
* @return the helper that will select every row in a table in the specified order
9898
*/
99-
static <T> MyBatis3SelectHelper<T> allRowsOrderdBy(SortSpecification...columns) {
99+
static <T> MyBatis3SelectListHelper<T> allRowsOrderdBy(SortSpecification...columns) {
100100
return h -> h.orderBy(columns);
101101
}
102102
}
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.Optional;
19+
import java.util.function.Function;
20+
21+
import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter;
22+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
23+
import org.mybatis.dynamic.sql.util.Buildable;
24+
25+
/**
26+
* Represents a function that can be used to create a general select one method in the style
27+
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
28+
* call the build().execute() methods - making client code look a bit cleaner.
29+
*
30+
* <p>For example, you can create mapper interface methods like this:
31+
*
32+
* <pre>
33+
* &#64;SelectProvider(type=SqlProviderAdapter.class, method="select")
34+
* &#64;ResultMap("SimpleTableResult")
35+
* Optional&lt;SimpleTableRecord&gt; selectOne(SelectStatementProvider selectStatement);
36+
*
37+
* default Optional&lt;SimpleTableRecord&gt; selectOne(MyBatis3SelectOneHelper&lt;SimpleTableRecord&gt; helper) {
38+
* return helper.apply(SelectDSL.selectWithMapper(this::selectOne, simpleTable.allColumns())
39+
* .from(simpleTable))
40+
* .build()
41+
* .execute();
42+
* }
43+
* </pre>
44+
*
45+
* <p>And then call the simplified default method like this:
46+
*
47+
* <pre>
48+
* Optional&lt;SimpleRecord&gt; record = mapper.selectOne(q -&gt;
49+
* q.where(id, isEqualTo(1)));
50+
* </pre>
51+
*
52+
* @author Jeff Butler
53+
*/
54+
@FunctionalInterface
55+
public interface MyBatis3SelectOneHelper<T> extends
56+
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Optional<T>>>,
57+
Buildable<MyBatis3SelectModelAdapter<Optional<T>>>> {
58+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
2525

2626
/**
27-
* Utility functions for building MyBatis3 mappers
27+
* Utility functions for building MyBatis3 mappers.
2828
*
2929
* @author Jeff Butler
3030
*
@@ -33,7 +33,7 @@ public class MyBatis3Utils {
3333
private MyBatis3Utils() {}
3434

3535
/**
36-
* Initiates a delete statement using the non-boxing adapter
36+
* Initiates a delete statement using the non-boxing adapter.
3737
*
3838
* @param mapper a MyBatis3 mapper delete method
3939
* @param table the table to delete from
@@ -45,7 +45,7 @@ public static DeleteDSL<MyBatis3DeleteModelToIntAdapter> deleteFrom(ToIntFunctio
4545
}
4646

4747
/**
48-
* Initiates an update statement using the non-boxing adapter
48+
* Initiates an update statement using the non-boxing adapter.
4949
*
5050
* @param mapper a MyBatis3 mapper update method
5151
* @param table the table to update

src/site/markdown/docs/mybatis3.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,41 @@ Optional<SimpleTableRecord> selectOne(SelectStatementProvider selectStatement);
9999

100100
These two methods are standard methods for MyBatis Dynamic SQL. They execute a select and return either a list of records, or a single record.
101101

102-
The `selectOne` method can be used to implement a `selectByPrimaryKey` method:
102+
The `selectOne` method can be used to implement a generalized select one method:
103+
104+
```java
105+
default Optional<SimpleTableRecord> selectOne(MyBatis3SelectOneHelper<SimpleTableRecord> helper) {
106+
return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
107+
.from(simpleTable))
108+
.build()
109+
.execute();
110+
}
111+
```
112+
113+
This method shows the use of `MyBatis3SelectOneHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause.
114+
115+
The general `selectOne` method can be used to implement a `selectByPrimaryKey` method:
103116

104117
```java
105118
default Optional<SimpleTableRecord> selectByPrimaryKey(Integer id_) {
106-
return SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate,
107-
employed, occupation)
108-
.from(simpleTable)
109-
.where(id, isEqualTo(id_))
110-
.build()
111-
.execute();
119+
return selectOne(h ->
120+
h.where(id, isEqualTo(id_))
121+
);
112122
}
113123
```
114124

115125
The `selectMany` method can be used to implement generalized select methods where a user can specify a where clause and/or an order by clause. Typically we recommend two of these methods - for select, and select distinct:
116126

117127
```java
118-
default List<SimpleTableRecord> select(MyBatis3SelectHelper<SimpleTableRecord> helper) {
128+
default List<SimpleTableRecord> select(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
119129
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate,
120130
employed, occupation)
121131
.from(simpleTable))
122132
.build()
123133
.execute();
124134
}
125135

126-
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectHelper<SimpleTableRecord> helper) {
136+
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
127137
return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName,
128138
birthDate, employed, occupation)
129139
.from(simpleTable))
@@ -133,7 +143,7 @@ default List<SimpleTableRecord> selectDistinct(MyBatis3SelectHelper<SimpleTableR
133143
```
134144

135145

136-
These methods show the use of `MyBatis3SelectHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause and/or an order by clause.
146+
These methods show the use of `MyBatis3SelectListHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause and/or an order by clause.
137147

138148
Clients can use the methods as follows:
139149

@@ -147,14 +157,14 @@ There are utility methods that will select all rows in a table:
147157

148158
```java
149159
List<SimpleTableRecord> rows =
150-
mapper.selectByExample(MyBatis3SelectHelper.allRows());
160+
mapper.selectByExample(MyBatis3SelectListHelper.allRows());
151161
```
152162

153163
The following query will select all rows in a specified order:
154164

155165
```java
156166
List<SimpleTableRecord> rows =
157-
mapper.selectByExample(MyBatis3SelectHelper.allRowsOrderedBy(lastName, firstName));
167+
mapper.selectByExample(MyBatis3SelectListHelper.allRowsOrderedBy(lastName, firstName));
158168
```
159169

160170
## Update Method Support

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
4343
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper;
4444
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper;
45-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectHelper;
45+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectListHelper;
46+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectOneHelper;
4647
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateHelper;
4748
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateModelToIntAdapter;
4849
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
@@ -148,26 +149,31 @@ default int insertSelective(SimpleTableRecord record) {
148149
.render(RenderingStrategy.MYBATIS3));
149150
}
150151

151-
default List<SimpleTableRecord> select(MyBatis3SelectHelper<SimpleTableRecord> helper) {
152+
default Optional<SimpleTableRecord> selectOne(MyBatis3SelectOneHelper<SimpleTableRecord> helper) {
153+
return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
154+
.from(simpleTable))
155+
.build()
156+
.execute();
157+
}
158+
159+
default List<SimpleTableRecord> select(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
152160
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
153161
.from(simpleTable))
154162
.build()
155163
.execute();
156164
}
157165

158-
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectHelper<SimpleTableRecord> helper) {
166+
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
159167
return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
160168
.from(simpleTable))
161169
.build()
162170
.execute();
163171
}
164172

165173
default Optional<SimpleTableRecord> selectByPrimaryKey(Integer id_) {
166-
return SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
167-
.from(simpleTable)
168-
.where(id, isEqualTo(id_))
169-
.build()
170-
.execute();
174+
return selectOne(h ->
175+
h.where(id, isEqualTo(id_))
176+
);
171177
}
172178

173179
default int update(MyBatis3UpdateHelper helper) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.junit.jupiter.api.Test;
4242
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper;
4343
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper;
44-
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectHelper;
44+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectListHelper;
4545

4646
import examples.simple.LastName;
4747
import examples.simple.SimpleTableRecord;
@@ -88,7 +88,7 @@ public void testSelectAll() {
8888
try (SqlSession session = sqlSessionFactory.openSession()) {
8989
SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class);
9090

91-
List<SimpleTableRecord> rows = mapper.select(MyBatis3SelectHelper.allRows());
91+
List<SimpleTableRecord> rows = mapper.select(MyBatis3SelectListHelper.allRows());
9292

9393
assertThat(rows.size()).isEqualTo(6);
9494
assertThat(rows.get(0).getId()).isEqualTo(1);
@@ -102,7 +102,7 @@ public void testSelectAllOrdered() {
102102
SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class);
103103

104104
List<SimpleTableRecord> rows = mapper
105-
.select(MyBatis3SelectHelper.allRowsOrderdBy(lastName.descending(), firstName.descending()));
105+
.select(MyBatis3SelectListHelper.allRowsOrderdBy(lastName.descending(), firstName.descending()));
106106

107107
assertThat(rows.size()).isEqualTo(6);
108108
assertThat(rows.get(0).getId()).isEqualTo(5);

0 commit comments

Comments
 (0)