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
@@ -156,6 +156,37 @@ As in annotations, you can write inline scripts or template names.
156
156
</select>
157
157
```
158
158
159
+
## Prepared statements parameters
160
+
161
+
`<@p/>` directive can be used in two scenarios:
162
+
163
+
- To pass parameters to prepared statements AS IS:
164
+
165
+
`<@p name='id'/>` (will be translated to `#{id}`, and value already presents in parameter object)
166
+
167
+
- To pass any value as prepared statements parameter
168
+
169
+
`<@p value=someValue/>` will be converted to `#{_p0}`, and `_p0` parameter will be automatically added to parameters map. It is convenient to use in loops like this:
170
+
171
+
```ftl
172
+
select * from names where firstName in (
173
+
<#list ids as id>
174
+
<@p value=id/>
175
+
<#if id_has_next>,</#if>
176
+
</#list>
177
+
)
178
+
```
179
+
180
+
This markup will be translated to
181
+
182
+
```sql
183
+
select*from names where firstName in (#{_p0}, #{_p1}, #{_p2})
184
+
```
185
+
186
+
and there are no need to care about escaping. All this stuff will be done automatically by JDBC driver.
187
+
188
+
Unfortunately, you can't use this syntax if passing one object as parameter and without `@Param` annotation. The `UnsupportedOperationException` will be thrown. It is because appending additional parameters to some object in general is very hard. When you are using `@Param` annotated args, MyBatis will use `Map` to store parameters, and it is easy to add some generated params. So, if you want to use auto-generated prepared parameters, please don't forget about `@Param` annotation.
189
+
159
190
## Examples
160
191
161
192
You can view full-featured example of configuring and of both XML-mapper and annotations-driven mapper usage in [test suite](https://github.com/elw00d/mybatis-freemarker/tree/master/src/test)
0 commit comments