You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The generated SQL statement providers are compatible with Spring's `NamedParameterJdbcTemplate` in all cases. The only challenge comes with presenting statement parameters to Spring in the correct manner. To make this easier, the library provides a utility class `org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions` that executes statements properly in all cases and hides the complexity of rendering statements and formatting parameters. All the examples below will show usage both with and without the utility class.
16
16
17
-
MyBatis3 is a higher level abstraction over JDBC than Spring JDBC templates. While most functions in the library will work with Spring, there are some functions that do not work:
17
+
## Type Converters for Spring
18
18
19
-
1. Spring JDBC templates do not have anything equivalent to a type handler in MyBatis3. Therefore it is best to use data types that can be automatically understood by Spring
19
+
Spring JDBC templates do not have the equivalent of a type handler in MyBatis3. This is generally not a problem in processing results because you can build type conversions into your row handler. If you were manually creating the parameter map that is used as input to a Spring template you could perform a type conversion there too. But when you use MyBatis Dynamic SQL, the parameters are generated by the library, so you do not have the opportunity to perform type conversions directly.
20
+
21
+
To address this issue, the library provides a parameter type converter that can be used to perform a type conversion before parameters are placed in a paramter map.
22
+
23
+
For example, suppose we want to use a `Boolean` in Java to represent the value of a flag, but in the database the corresponding field is a `CHAR` field that expects values "true" or "false". This can be accomplished by using a `ParameterTypeConverter`. First create the converter as follows:
MyBatis Dynamic SQL will now call the converter function before corresponding parameters are placed into the generated parameter map. The converter will be called in the following cases:
44
+
45
+
1. With a general insert statement when using the `set(...).toValue(...)` or `set(...).toValueWhenPresent(...)` mappings
46
+
1. With an update statement when using the `set(...).equalTo(...)` or `set(...).equalToWhenPresent(...)` mappings
47
+
1. With where clauses in any statement type that contain conditions referencing the field
20
48
21
49
## Executing Select Statements
22
-
The Spring Named Parameter JDBC template expects an SQL statement with parameter markers in the Spring format, and a set of matched parameters. MyBatis Dynamic SQL will generate both. The parameters returned from the generated SQL statement can be wrapped in a Spring `MapSqlParameterSource`. Spring also expects you to provide a row mapper for creating the returned objects. The following code shows a complete example:
50
+
The Spring Named Parameter JDBC template expects an SQL statement with parameter markers in the Spring format, and a set of matched parameters. MyBatis Dynamic SQL will generate both. The parameters returned from the generated SQL statement can be wrapped in a Spring `MapSqlParameterSource`. Spring also expects you to provide a row mapper for creating the returned objects.
51
+
52
+
The following code shows a complete example without the utility class:
23
53
24
54
```java
25
55
NamedParameterJdbcTemplate template = getTemplate(); // not shown
@@ -46,7 +76,62 @@ The Spring Named Parameter JDBC template expects an SQL statement with parameter
46
76
});
47
77
```
48
78
49
-
## Executing General Insert Statements
79
+
The following code shows a complete example with the utility class:
80
+
81
+
```java
82
+
NamedParameterJdbcTemplate template = getTemplate(); // not shown
GeneratedAlwaysRecord record =newGeneratedAlwaysRecord();
119
+
record.setId(rs.getInt(1));
120
+
record.setFirstName(rs.getString(2));
121
+
record.setLastName(rs.getString(3));
122
+
record.setFullName(rs.getString(4));
123
+
return record;
124
+
}
125
+
});
126
+
```
127
+
128
+
## Executing Insert Statements
129
+
130
+
The library generates several types of insert statements. See the [Insert Statements](insert.html) page for details.
131
+
132
+
Spring supports retrieval of generated keys for many types of inserts. This library has support for generated key retrieval where it is supported by Spring.
133
+
134
+
### Executing General Insert Statements
50
135
General insert statements do not require a POJO object matching a table row. Following is a complete example:
51
136
52
137
```java
@@ -81,7 +166,26 @@ If you want to retrieve generated keys for a general insert statement the steps
int rows = extensions.generalInsert(insertStatement);
182
+
183
+
// retrieve generated keys
184
+
KeyHolder keyHolder =newGeneratedKeyHolder();
185
+
int rows = extensions.generalInsert(insertStatement, keyHolder);
186
+
```
187
+
188
+
### Executing Single Record Insert Statements
85
189
Insert record statements are a bit different - MyBatis Dynamic SQL generates a properly formatted SQL string for Spring, but instead of a map of parameters, the parameter mappings are created for the inserted record itself. So the parameters for the Spring template are created by a `BeanPropertySqlParameterSource`. Generated keys in Spring are supported with a `GeneratedKeyHolder`. The following is a complete example:
86
190
87
191
```java
@@ -107,8 +211,101 @@ Insert record statements are a bit different - MyBatis Dynamic SQL generates a p
Batch insert support in Spring is a bit different than batch support in MyBatis3 and Spring does not support returning generated keys from a batch insert. The following is a complete example of a batch insert (note the use of `SqlParameterSourceUtils` to create an array of parameter sources from an array of input records):
214
+
This can be simplified by using the utility class as follows:
215
+
216
+
```java
217
+
NamedParameterJdbcTemplate template = getTemplate(); // not shown
int rows = extensions.insert(insertStatement, keyHolder);
237
+
```
238
+
239
+
### Multi-Row Inserts
240
+
A multi-row insert is a single insert statament with multiple VALUES clauses. This can be a convienint way in insert a small number of records into a table with a single statement. Note however that a multi-row insert is not suitable for large bulk inserts as it is possible to exceed the limit of prepared statement parameters with a large number of records. For that use case, use a batch insert (see below).
241
+
242
+
With multi-row insert statements MyBatis Dynamic SQL generates a properly formatted SQL string for Spring. Instead of a map of parameters, the multiple records are stored in the generated provider object and the parameter mappings are created for the generated provider itself. The parameters for the Spring template are created by a `BeanPropertySqlParameterSource`. Generated keys in Spring are supported with a `GeneratedKeyHolder`. The following is a complete example:
243
+
244
+
```java
245
+
NamedParameterJdbcTemplate template = getTemplate(); // not shown
246
+
247
+
List<GeneratedAlwaysRecord> records =newArrayList<>();
248
+
GeneratedAlwaysRecord record =newGeneratedAlwaysRecord();
int rows = extensions.insertMultiple(insertStatement);
301
+
302
+
// retrieve generated keys
303
+
KeyHolder keyHolder =newGeneratedKeyHolder();
304
+
int rows = extensions.insertMultiple(insertStatement, keyHolder);
305
+
```
306
+
307
+
### Executing Batch Inserts
308
+
A JDBC batch insert is an efficient way to perform a bulk insert. It does not have the limitations of a multi-row insert and may perform better too. Spring does not support returning generated keys from a batch insert. The following is a complete example of a batch insert (note the use of `SqlParameterSourceUtils` to create an array of parameter sources from an array of input records):
112
309
113
310
```java
114
311
NamedParameterJdbcTemplate template = getTemplate(); // not shown
@@ -139,8 +336,64 @@ Batch insert support in Spring is a bit different than batch support in MyBatis3
0 commit comments