Skip to content

Commit 1af9dab

Browse files
committed
Documentation
1 parent 3a67102 commit 1af9dab

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
1313
### Added
1414

1515
- Added a callback capability to the "In" conditions that will be called before rendering when the conditions are empty. Also, removed the option that forced the library to render invalid SQL in that case. ([#241](https://github.com/mybatis/mybatis-dynamic-sql/pull/241))
16+
- Added a utility mapper for MyBatis that allows you to run any select query without having to predefine a result mapping.
1617

1718
## Release 1.2.0 - August 19, 2020
1819

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@
3030
* This is a general purpose MyBatis mapper. It allows you to execute select statements without having to
3131
* write a custom {@link org.apache.ibatis.annotations.ResultMap} for each statement.
3232
*
33-
* <p>This mapper contains two types of methods:
33+
* <p>This mapper contains three types of methods:
3434
* <ul>
35-
* <li>selectOneMappedRow, selectOne, selectManyMappedRows, and selectMany can be used to process result sets with
36-
* any number of columns. The row values are processed by MyBatis and returned in a Map. A function can
37-
* be applied to the Map to create custom objects if desired</li>
38-
* <li>The other methods are for result sets with a single column. There are functions for many different
39-
* data types (Integer, Long, String, etc.) and functions that return a single value, and Optional value,
40-
* or a List of values</li>
35+
* <li>The selectOneMappedRow and selectManyMappedRows methods allow you to use select statements with
36+
* any number of columns. MyBatis will process the rows and return a Map of values, or a List of Maps.</li>
37+
* <li>The selectOne and selectMany methods also allow you to use select statements with any number of columns.
38+
* These methods also allow you to specify a function that will transform a Map of row values into a specific
39+
* object.</li>
40+
* <li>The other methods are for result sets with a single column. There are functions for many
41+
* data types (Integer, Long, String, etc.) There are also functions that return a single value, and Optional value,
42+
* or a List of values.</li>
4143
* </ul>
4244
*
43-
* <p>This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers.</p>
45+
* <p>This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers.
4446
*
4547
* @author Jeff Butler
4648
*/

src/site/markdown/docs/mybatis3.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,124 @@ The goal of this support is to reduce the amount of boilerplate code needed for
55

66
With version 1.1.3, specialized interfaces and utilities were added that can further simplify client code. This support enables the creation of methods that have similar functionality to some of the methods generated in previous versions of MyBatis generator like countByExample, deleteByExample, and selectByExample. We no longer use the "by example" terms for these methods as this library has eliminated the Example class that was generated by prior versions of MyBatis Generator.
77

8+
## Dynamic Result Mapping Support
9+
MyBatis is very good at mapping result sets to objects - this is one of its primary differentiators. MyBatis also requires
10+
that you predefine the mappings for every possibility. This presents a challenge if you want very dynamic column lists
11+
in a query. This library provides a generalized MyBatis mapper that can assist with that problem.
12+
13+
The general mapper is `org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper`. This mapper can be injected into a MyBatis configuration
14+
as is, or it can be extended by an existing mapper. If you are using MyBatis Spring support and auto scanning for mappers,
15+
you can create an extension in your application's mapper package as follows:
16+
17+
```java
18+
package foo.mapper;
19+
20+
import org.apache.ibatis.annotations.Mapper;
21+
import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper;
22+
23+
@Mapper
24+
public interface MyGeneralMapper extends GeneralMapper {
25+
}
26+
```
27+
28+
The mapper contains three types of methods:
29+
30+
1. The `selectOneMappedRow` and `selectManyMappedRows` methods allow you to use select statements with
31+
any number of columns. MyBatis will process the rows and return a Map of values, or a List of Maps.
32+
1. The `selectOne` and `selectMany` methods also allow you to use select statements with any number of columns. These methods
33+
also allow you to specify a function that will transform a Map of row values into a specific object.
34+
1. The other methods are for result sets with a single column. There are functions for many
35+
data types (Integer, Long, String, etc.) There are also functions that return a single value, and Optional value,
36+
or a List of values.
37+
38+
An example of using the mapped row methods follows:
39+
40+
```java
41+
package foo.service;
42+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
43+
44+
import java.util.List;
45+
import java.util.Map;
46+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
47+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
48+
import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper;
49+
50+
public class MyService {
51+
public List<Map<String,Object>> generalSearch() {
52+
GeneralMapper mapper = getGeneralMapper(); // not shown
53+
54+
SelectStatementProvider selectStatement = select(id, description)
55+
.from(foo)
56+
.where(description. isLike("%bar%"))
57+
.build()
58+
.render(RenderingStrategies.MYBATIS3);
59+
return mapper.selectManyMappedRows(selectStatement);
60+
}
61+
}
62+
```
63+
64+
As you can see, the method returns a List of Maps containing the row values. The Map key will be the column name as
65+
returned from the database (typically in upper case), and the column value as returned from the `ResultSet.getObject()`.
66+
See your JDBC driver's documentation for details about how SQL types are mapped to Java types to determine the data type
67+
for your specific database.
68+
69+
This method works well, but usually it is better to marshal the result set into actual objects. This can be accomplished
70+
as follows:
71+
72+
```java
73+
package foo.service;
74+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
75+
76+
import java.util.List;
77+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
78+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
79+
import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper;
80+
81+
public class MyService {
82+
public List<TableCode> generalSearch() {
83+
GeneralMapper mapper = getGeneralMapper(); // not shown
84+
85+
SelectStatementProvider selectStatement = select(id, description)
86+
.from(foo)
87+
.where(description. isLike("%bar%"))
88+
.build()
89+
.render(RenderingStrategies.MYBATIS3);
90+
return mapper.selectMany(selectStatement, m -> {
91+
TableCode tc = new TableCode();
92+
tc.setId((Integer) m.get("ID"));
93+
tc.setDescription((String) m.get("DESCRIPTION"));
94+
return tc;
95+
});
96+
}
97+
}
98+
```
99+
100+
With this method you can centralize all the database specific operations in a single method.
101+
102+
If you only have a single column in the result set, the general mapper provides methods to retrieve the value directly.
103+
For example:
104+
105+
```java
106+
package foo.service;
107+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
108+
109+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
110+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
111+
import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper;
112+
113+
public class MyService {
114+
public Long getCount() {
115+
GeneralMapper mapper = getGeneralMapper(); // not shown
116+
117+
SelectStatementProvider selectStatement = countFrom(foo)
118+
.where(description. isLike("%bar%"))
119+
.build()
120+
.render(RenderingStrategies.MYBATIS3);
121+
return mapper.selectOneLong(selectStatement);
122+
}
123+
}
124+
```
125+
8126
## Count Method Support
9127

10128
The goal of count method support is to enable the creation of methods that execute a count query allowing a user to specify a where clause at runtime, but abstracting away all other details.

0 commit comments

Comments
 (0)