Skip to content

Commit fa87da3

Browse files
committed
Added Getting Started doc.
1 parent 488741f commit fa87da3

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,122 @@ MyBatis FreeMarker Support
77

88
MyBatis FreeMarker Scripting Support.
99

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:
50+
51+
```xml
52+
<configuration>
53+
...
54+
<typeAliases>
55+
<typeAlias alias="freemarker" type="org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver"/>
56+
</typeAliases>
57+
...
58+
</configuration>
59+
```
60+
61+
- (Optional) Set the freemarker as your default scripting language:
62+
63+
```xml
64+
<configuration>
65+
...
66+
<settings>
67+
<setting name="defaultScriptingLanguage" value="freemarker"/>
68+
</settings>
69+
...
70+
</configuration>
71+
```
72+
73+
## Usage in annotations-driven mappers
74+
75+
Just write your queries using FreeMarker syntax:
76+
77+
```java
78+
@Lang(FreeMarkerLanguageDriver.class)
79+
@Select("select * from names where id in (${ids?join(',')})")
80+
List<Name> findNamesByIds(@Param("ids") List<Integer> ids);
81+
```
82+
83+
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 -->
109+
<sql id="cols">id, ${r"firstName"}, lastName</sql>
110+
111+
<select id="findName" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
112+
findName.ftl
113+
</select>
114+
115+
<select id="findNamesByIds" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
116+
select <include refid="cols"/> from names where id in (${ids?join(',')})
117+
</select>
118+
119+
<!-- It is not very convenient - to use CDATA blocks. Better is to create external template
120+
or use more verbose syntax: ${r"#{id}"}. -->
121+
<select id="find" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
122+
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

Comments
 (0)