Skip to content

Commit 58cab5e

Browse files
committed
2 parents ae15e1d + 3e1d96e commit 58cab5e

File tree

6 files changed

+243
-19
lines changed

6 files changed

+243
-19
lines changed

src/site/ja/xdoc/dynamic-sql.xml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,35 @@ AND title like ‘someTitle’]]></source>
191191
</subsection>
192192
<subsection name="ダイナミック SQL 記述言語">
193193
<p>バージョン 3.2 以降、ダイナミック SQL の記述言語が Pluggable になりました。言語ドライバーを記述することで、任意の言語でダイナミック SQL を記述することができます。</p>
194-
<p>標準では次の2つの言語が用意されています。</p>
195-
<ul>
196-
<li>xml</li>
197-
<li>raw</li>
198-
</ul>
199-
<p><code>xml</code> はデフォルトの SQL 記述言語で、これまでのセクションで説明した全ての機能を利用することができます。</p>
200-
<p><code>raw</code> というのは、実際には言語を使用しない場合に指定します。この場合、MyBatis がステートメントをデータベースドライバーに渡す前に行うのは引数の置換処理のみです。当然、 <code>xml</code> よりも <code>raw</code> を指定した場合の方が処理は高速です。
201-
</p>
202-
<p>言語の指定は、ステートメント要素に <code>lang</code> 属性を追加することで行います。
203-
</p>
204-
<source><![CDATA[<select id="selectBlog" lang="raw">
194+
<p>カスタムの言語ドライバーを使用する場合、まずは LanguageDriver インターフェイスを実装したクラスを作成します。</p>
195+
<source><![CDATA[public interface LanguageDriver {
196+
ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql);
197+
SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType);
198+
SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType);
199+
}]]></source>
200+
<p>作成した言語ドライバーをデフォルトとして使用する場合は、mybatis-config.xml に次のような設定を追加します(typeAlias の使用は必須ではありません)。</p>
201+
<source><![CDATA[<typeAliases>
202+
<typeAlias type="org.sample.MyLanguageDriver" alias="myLanguage"/>
203+
</typeAliases>
204+
<settings>
205+
<setting name="defaultScriptingLanguage" value="myLanguage"/>
206+
</settings>
207+
]]></source>
208+
<p><code>lang</code> 属性を指定することで、特定のステートメントの言語ドライバーのみを変更することもできます。</p>
209+
<source><![CDATA[<select id="selectBlog" lang="myLanguage">
205210
SELECT * FROM BLOG
206211
</select>]]></source>
207212
<p>Mapper インターフェイスを使っている場合は <code>@Lang</code> アノテーションを使います。</p>
208-
<source><![CDATA[public interface Mapper {
209-
@Lang(RawLanguageDriver.class)
213+
<source><![CDATA[public interface Mapper {
214+
@Lang(MyLanguageDriver.class)
210215
@Select("SELECT * FROM BLOG")
211216
List<Blog> selectBlog();
212217
}]]></source>
213-
<p>LanguageDriver インターフェイスを実装することで、カスタムの言語ドライバーを作成することもできます。</p>
214-
<source><![CDATA[public interface LanguageDriver {
215-
ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql);
216-
SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType);
217-
SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType);
218-
}]]></source>
218+
219+
<p><span class="label important">NOTE</span> Apache Velocity を使ってダイナミック SQL を記述することができます。MyBatis-Velocity プロジェクトを参照してください。</p>
220+
221+
<p>これまでのセクションで出てきた XML タグは、全てデフォルトの言語ドライバー <code>org.apache.ibatis.scripting.xmltags.XmlLanguageDriver</code> (エイリアスは <code>xml</code> )によって提供されているものです。</p>
222+
219223
</subsection>
220224
</section>
221225
</body>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2013 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (id, name) values(1, 'User1');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.raw_sql_source.Mapper">
22+
23+
<select id="getUser1" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
24+
select * from users where id = #{value}
25+
</select>
26+
27+
<select id="getUser2" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
28+
select * from users where id = ${value}
29+
</select>
30+
31+
<select id="getUser3" resultType="org.apache.ibatis.submitted.raw_sql_source.User">
32+
<if test="true">
33+
select * from users where id = #{value}
34+
</if>
35+
</select>
36+
37+
</mapper>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2009-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.raw_sql_source;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.jdbc.ScriptRunner;
23+
import org.apache.ibatis.mapping.SqlSource;
24+
import org.apache.ibatis.scripting.defaults.RawSqlSource;
25+
import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
26+
import org.apache.ibatis.session.SqlSession;
27+
import org.apache.ibatis.session.SqlSessionFactory;
28+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29+
import org.junit.Assert;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class RawSqlSourceTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeClass
38+
public static void setUp() throws Exception {
39+
// create an SqlSessionFactory
40+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/mybatis-config.xml");
41+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42+
reader.close();
43+
44+
// populate in-memory database
45+
SqlSession session = sqlSessionFactory.openSession();
46+
Connection conn = session.getConnection();
47+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/CreateDB.sql");
48+
ScriptRunner runner = new ScriptRunner(conn);
49+
runner.setLogWriter(null);
50+
runner.runScript(reader);
51+
reader.close();
52+
session.close();
53+
}
54+
55+
@Test
56+
public void shouldUseRawSqlSourceForAnStaticStatement() {
57+
test("getUser1", RawSqlSource.class);
58+
}
59+
60+
@Test
61+
public void shouldUseDynamicSqlSourceForAnStatementWithInlineArguments() {
62+
test("getUser2", DynamicSqlSource.class);
63+
}
64+
65+
@Test
66+
public void shouldUseDynamicSqlSourceForAnStatementWithXmlTags() {
67+
test("getUser3", DynamicSqlSource.class);
68+
}
69+
70+
private void test(String statement, Class<? extends SqlSource> sqlSource) {
71+
SqlSession sqlSession = sqlSessionFactory.openSession();
72+
try {
73+
Assert.assertEquals(sqlSource, sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getClass());
74+
User user = sqlSession.selectOne(statement, 1);
75+
Assert.assertEquals("User1", user.getName());
76+
} finally {
77+
sqlSession.close();
78+
}
79+
}
80+
81+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.raw_sql_source;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:raw_sql_source" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/raw_sql_source/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)