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
Copy file name to clipboardExpand all lines: README.md
+119-1Lines changed: 119 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,4 +7,122 @@ MyBatis FreeMarker Support
7
7
8
8
MyBatis FreeMarker Scripting Support.
9
9
10
-
See tests to view examples.
10
+
Getting started
11
+
===============
12
+
13
+
## Introduction
14
+
15
+
mybatis-freemarker is a plugin that helps creating big dynamic SQL queries. You can use it selectively, to only queries that need if statmenets or foreach-loops, for example. But it is possible to use this syntax by default too.
16
+
17
+
If you are not familiar with FreeMarker syntax, you can view [Template Language Reference](http://freemarker.org/docs/ref.html)
18
+
19
+
## Install from sources
20
+
21
+
- Checkout the source code
22
+
- Run `mvn install` to build and to automatically install it to your local maven repo
23
+
- Add maven dependency to your project
24
+
25
+
```xml
26
+
<dependency>
27
+
<groupId>org.mybatis.scripting</groupId>
28
+
<artifactId>mybatis-freemarker</artifactId>
29
+
<version>1.0-SNAPSHOT</version>
30
+
</dependency>
31
+
```
32
+
33
+
## Configuring
34
+
35
+
### Common
36
+
37
+
- (Optional) Create `mybatis-freemarker.properties` file in your classpath:
38
+
39
+
```
40
+
basePackage=sql
41
+
```
42
+
43
+
This will define base package to search FreeMarker templates. By default it is empty string, so you will need to provide full path to template every time.
44
+
45
+
### XML-driven mappers
46
+
47
+
If your are using annotations-driven mappers, you don't need to do anything more. If you are using XML-driven mappers too, you may need to do next steps:
48
+
49
+
- Register the language driver alias in your mybatis configuration file:
If any whitespace found inside `@Select` text, it is interpreted as inline script, not template name. It is convenient to avoid creating templates when script is really small. If you have a large SQL script, you can place it in distinct template and write next code:
84
+
85
+
```java
86
+
@Lang(FreeMarkerLanguageDriver.class)
87
+
@Select("findName.ftl")
88
+
Name findName(@Param("n") String name);
89
+
```
90
+
91
+
Template will be searched in classpath using `basePackage` property that has already been described above.
92
+
93
+
`findName.ftl` content can be:
94
+
95
+
```
96
+
SELECT *
97
+
FROM names
98
+
where firstName = <@p name="n"/>
99
+
```
100
+
101
+
`<@p name="n"/>` is a custom directive to generate `#{n}` markup. This markup further will be passed into MyBatis engine, and it will replace this to `?`-parameter. You can't write `#{paramName}` directly, because FreeMarker supports this syntax natively (alghough it is deprecated). So, to get `?`-parameters to prepared statements works, you need to use `${r"#{paramName}"}` verbose syntax, or this directive. By the way, in XML files `${r"#{paramName}"}` is more preferrable because you don't need wrap it using `CDATA` statements. In annotations and in external templates `<@p/>` directive is more neat.
102
+
103
+
## Usage in XML-driven mappers
104
+
105
+
As in annotations, you can write inline scripts or template names.
106
+
107
+
```xml
108
+
<!-- This is handled by FreeMarker too, because it is included into select nodes AS IS -->
select * from names where id = <![CDATA[ <@p name='id'/> ]]> and id = ${id}
123
+
</select>
124
+
```
125
+
126
+
## Examples
127
+
128
+
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