Skip to content

Commit 9a4340b

Browse files
committed
Docs for standalone where clauses
1 parent d3b332b commit 9a4340b

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/site/markdown/docs/whereClauses.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Most of the conditions also support a subquery. For example:
6969
```
7070

7171
## Stand Alone Where Clauses
72-
You can use the where clause support on its own if you would rather code your own SQL for the remainder of a statement. There may be several reasons to do this - mainly if the library doesn't support some SQL or MyBatisfeature you want to use. A good example would be paginated queries which are currently not support by the library. If you want to use a standalone where clause, you can code a mapper method that looks like this:
72+
You can use the where clause support on its own if you would rather code your own SQL for the remainder of a statement. There may be several reasons to do this - mainly if the library doesn't support some SQL or MyBatis feature you want to use. A good example would be paginated queries which are currently not support by the library. If you want to use a stand alone where clause, you can code a mapper method that looks like this:
7373

7474
```java
7575
@Select({
@@ -90,4 +90,56 @@ You can build a stand alone where clause and call your mapper like this:
9090

9191
List<AnimalData> animals = mapper.selectByExample(whereClause);
9292
```
93-
93+
This method works well when there are no other parameters needed for the statement and when there are no table aliases involved. If you have those other needs, then see the following.
94+
95+
### Table Aliases
96+
If you need to use a table alias in the generated where clause you can supply it on the render method using the `TableAliasCalculator` class. For example, if you have a mapper like this:
97+
98+
```java
99+
@Select({
100+
"select a.id, a.animal_name, a.brain_weight, a.body_weight",
101+
"from AnimalData a",
102+
"${whereClause}"
103+
})
104+
@ResultMap("AnimalDataResult")
105+
List<AnimalData> selectByExampleWithAlias(WhereClauseAndParameters whereClause);
106+
```
107+
Then you can specify the alias for the generated WHERE clause on the render method like this:
108+
109+
```java
110+
WhereClauseAndParameters whereClause = where(id, isEqualTo(1), or(bodyWeight, isGreaterThan(1.0)))
111+
.build()
112+
.render(RenderingStrategy.MYBATIS3, TableAliasCalculator.of(animalData, "a"));
113+
114+
List<AnimalData> animals = mapper.selectByExampleWithAlias(whereClause);
115+
```
116+
It is more likely that you will be using table aliases with hand coded joins where there is more than on table alias. In this case, you supply a `Map<SqlTable, String>` to the TableAliasCalculator that holds an alias for each table involved in the WHERE clause.
117+
118+
### Handling Multiple Parameters
119+
By default, the WHERE clause renderer assumes that the rendered WHERE clause will be the only parameter to the mapper method. This is not always the case. For example, suppose you have a paginated query like this (this is HSQLDB syntax):
120+
121+
```java
122+
@Select({
123+
"select id, animal_name, brain_weight, body_weight",
124+
"from AnimalData",
125+
"${whereSupport.whereClause}",
126+
"order by id",
127+
"OFFSET #{offset,jdbcType=INTEGER} LIMIT #{limit,jdbcType=INTEGER}"
128+
})
129+
@ResultMap("AnimalDataResult")
130+
List<AnimalData> selectByExampleWithLimitAndOffset(@Param("whereSupport") WhereClauseAndParameters whereClause,
131+
@Param("limit") int limit, @Param("offset") int offset);
132+
```
133+
134+
In this mapper method there are three parameters. So in this case it will be necessary to tell the WHERE rendered what parameter name to use the for rendered where clause. That code looks like this:
135+
136+
```java
137+
WhereClauseAndParameters whereClause = where(id, isLessThan(60))
138+
.build()
139+
.render(RenderingStrategy.MYBATIS3, "whereSupport");
140+
141+
List<AnimalData> animals = mapper.selectByExampleWithLimitAndOffset(whereClause, 5, 15);
142+
```
143+
Notice that the string `whereSupport` is used both as the parameter name in the mapper `@Param` annotation and the parameter name in the `render` method.
144+
145+
The render method also has an override that accepts a TableAliasCalculator and a parameter name.

0 commit comments

Comments
 (0)