Skip to content

Commit f367494

Browse files
committed
Fixes #127. Global variables are applied also to annotations.
1 parent c5ac686 commit f367494

File tree

7 files changed

+239
-1
lines changed

7 files changed

+239
-1
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/XMLLanguageDriver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2013 the original author or authors.
2+
* Copyright 2009-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.apache.ibatis.mapping.BoundSql;
2020
import org.apache.ibatis.mapping.MappedStatement;
2121
import org.apache.ibatis.mapping.SqlSource;
22+
import org.apache.ibatis.parsing.PropertyParser;
2223
import org.apache.ibatis.parsing.XNode;
2324
import org.apache.ibatis.scripting.LanguageDriver;
2425
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
@@ -41,6 +42,7 @@ public SqlSource createSqlSource(Configuration configuration, String script, Cla
4142
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
4243
return builder.parseScriptNode();
4344
} else {
45+
script = PropertyParser.parse(script, configuration.getVariables()); // issue #127
4446
TextSqlNode textSqlNode = new TextSqlNode(script);
4547
if (textSqlNode.isDynamic()) {
4648
return new DynamicSqlSource(configuration, textSqlNode);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.global_variables;
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.session.SqlSession;
24+
import org.apache.ibatis.session.SqlSessionFactory;
25+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.junit.Assert;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class BaseTest {
31+
32+
private static SqlSessionFactory sqlSessionFactory;
33+
34+
@BeforeClass
35+
public static void setUp() throws Exception {
36+
// create an SqlSessionFactory
37+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables/mybatis-config.xml");
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
reader.close();
40+
41+
// populate in-memory database
42+
SqlSession session = sqlSessionFactory.openSession();
43+
Connection conn = session.getConnection();
44+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables/CreateDB.sql");
45+
ScriptRunner runner = new ScriptRunner(conn);
46+
runner.setLogWriter(null);
47+
runner.runScript(reader);
48+
reader.close();
49+
session.close();
50+
}
51+
52+
@Test
53+
public void shouldGetAUser() {
54+
SqlSession sqlSession = sqlSessionFactory.openSession();
55+
try {
56+
Mapper mapper = sqlSession.getMapper(Mapper.class);
57+
User user = mapper.getUser(1);
58+
Assert.assertEquals("User1", user.getName());
59+
} finally {
60+
sqlSession.close();
61+
}
62+
}
63+
64+
@Test
65+
public void shouldGetAUserFromAnnotation() {
66+
SqlSession sqlSession = sqlSessionFactory.openSession();
67+
try {
68+
Mapper mapper = sqlSession.getMapper(Mapper.class);
69+
User user = mapper.getUserFromAnnotation(1);
70+
Assert.assertEquals("User1", user.getName());
71+
} finally {
72+
sqlSession.close();
73+
}
74+
}
75+
76+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2012 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2009-2012 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.global_variables;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
20+
public interface Mapper {
21+
22+
User getUser(Integer id);
23+
24+
@Select("select * from ${table} where id = #{id}")
25+
User getUserFromAnnotation(Integer id);
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.global_variables.Mapper">
22+
23+
<select id="getUser" resultType="org.apache.ibatis.submitted.global_variables.User">
24+
select * from ${table} where id = #{id}
25+
</select>
26+
27+
</mapper>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2012 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.global_variables;
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
<properties>
24+
<property name="table" value="users"/>
25+
</properties>
26+
27+
<environments default="development">
28+
<environment id="development">
29+
<transactionManager type="JDBC">
30+
<property name="" value="" />
31+
</transactionManager>
32+
<dataSource type="UNPOOLED">
33+
<property name="driver" value="org.hsqldb.jdbcDriver" />
34+
<property name="url" value="jdbc:hsqldb:mem:global_variables" />
35+
<property name="username" value="sa" />
36+
</dataSource>
37+
</environment>
38+
</environments>
39+
40+
<mappers>
41+
<mapper class="org.apache.ibatis.submitted.global_variables.Mapper" />
42+
</mappers>
43+
44+
</configuration>

0 commit comments

Comments
 (0)