Skip to content

Commit c215f61

Browse files
Allow mapper's XML to reference an XML fragment in another mapper when Configuration.addMapper() is used. See http://code.google.com/p/mybatis/issues/detail?id=133 .
1 parent cbf546a commit c215f61

File tree

10 files changed

+176
-9
lines changed

10 files changed

+176
-9
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.apache.ibatis.mapping.SqlCommandType;
5252
import org.apache.ibatis.mapping.SqlSource;
5353
import org.apache.ibatis.mapping.StatementType;
54-
import org.apache.ibatis.parsing.XNode;
5554
import org.apache.ibatis.session.Configuration;
5655
import org.apache.ibatis.session.RowBounds;
5756
import org.apache.ibatis.type.JdbcType;
@@ -65,14 +64,12 @@ public class MapperAnnotationBuilder {
6564
private Configuration configuration;
6665
private MapperBuilderAssistant assistant;
6766
private Class<?> type;
68-
private Map<String, XNode> sqlFragments;
6967

7068
public MapperAnnotationBuilder(Configuration configuration, Class<?> type) {
7169
String resource = type.getName().replace('.', '/') + ".java (best guess)";
7270
this.assistant = new MapperBuilderAssistant(configuration, resource);
7371
this.configuration = configuration;
7472
this.type = type;
75-
this.sqlFragments = new HashMap<String, XNode>();
7673

7774
sqlAnnotationTypes.add(Select.class);
7875
sqlAnnotationTypes.add(Insert.class);
@@ -110,7 +107,7 @@ private void loadXmlResource() {
110107
// ignore, resource is not required
111108
}
112109
if (xmlReader != null) {
113-
XMLMapperBuilder xmlParser = new XMLMapperBuilder(xmlReader, assistant.getConfiguration(), xmlResource, sqlFragments, type.getName());
110+
XMLMapperBuilder xmlParser = new XMLMapperBuilder(xmlReader, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
114111
xmlParser.parse();
115112
}
116113
}

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919
import org.apache.ibatis.type.TypeHandler;
2020

2121
import java.io.Reader;
22-
import java.util.HashMap;
23-
import java.util.Map;
2422
import java.util.Properties;
2523

2624
public class XMLConfigBuilder extends BaseBuilder {
2725

2826
private boolean parsed;
2927
private XPathParser parser;
3028
private String environment;
31-
private Map<String, XNode> sqlFragments = new HashMap<String, XNode>();
3229

3330
public XMLConfigBuilder(Reader reader) {
3431
this(reader, null, null);
@@ -243,12 +240,12 @@ private void mapperElement(XNode parent) throws Exception {
243240
if (resource != null && url == null) {
244241
ErrorContext.instance().resource(resource);
245242
reader = Resources.getResourceAsReader(resource);
246-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(reader, configuration, resource, sqlFragments);
243+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(reader, configuration, resource, configuration.getSqlFragments());
247244
mapperParser.parse();
248245
} else if (url != null && resource == null) {
249246
ErrorContext.instance().resource(url);
250247
reader = Resources.getUrlAsReader(url);
251-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(reader, configuration, url, sqlFragments);
248+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(reader, configuration, url, configuration.getSqlFragments());
252249
mapperParser.parse();
253250
} else {
254251
throw new BuilderException("A mapper element may only specify a url or resource, but not both.");

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.ibatis.executor.statement.StatementHandler;
2222
import org.apache.ibatis.io.ResolverUtil;
2323
import org.apache.ibatis.mapping.*;
24+
import org.apache.ibatis.parsing.XNode;
2425
import org.apache.ibatis.plugin.Interceptor;
2526
import org.apache.ibatis.plugin.InterceptorChain;
2627
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
@@ -65,6 +66,7 @@ public class Configuration {
6566
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<String, KeyGenerator>("Key Generators collection");
6667

6768
protected final Set<String> loadedResources = new HashSet<String>();
69+
protected final Map<String, XNode> sqlFragments = new StrictMap<String, XNode>("XML fragments parsed from previous mappers");
6870

6971
public Configuration(Environment environment) {
7072
this();
@@ -349,6 +351,10 @@ public MappedStatement getMappedStatement(String id) {
349351
return mappedStatements.get(id);
350352
}
351353

354+
public Map<String, XNode> getSqlFragments() {
355+
return sqlFragments;
356+
}
357+
352358
public void addInterceptor(Interceptor interceptor) {
353359
interceptorChain.addInterceptor(interceptor);
354360
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.apache.ibatis.submitted.xml_references;
2+
3+
import java.io.Reader;
4+
import java.util.Properties;
5+
6+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
7+
import org.apache.ibatis.mapping.Environment;
8+
import org.apache.ibatis.session.Configuration;
9+
import org.apache.ibatis.session.SqlSessionFactory;
10+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11+
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
12+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
13+
import org.junit.Test;
14+
15+
import com.ibatis.common.resources.Resources;
16+
17+
public class EnumWithOgnlTest {
18+
19+
@Test
20+
public void testConfiguration() {
21+
UnpooledDataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
22+
Properties dataSourceProperties = new Properties();
23+
dataSourceProperties.put("driver", "org.hsqldb.jdbcDriver");
24+
dataSourceProperties.put("url", "jdbc:hsqldb:mem:xml_references");
25+
dataSourceProperties.put("username", "sa");
26+
dataSourceFactory.setProperties(dataSourceProperties);
27+
Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSourceFactory.getDataSource());
28+
Configuration configuration = new Configuration();
29+
configuration.setEnvironment(environment);
30+
configuration.getTypeAliasRegistry().registerAlias(Person.class);
31+
configuration.addMapper(PersonMapper.class);
32+
configuration.addMapper(PersonMapper2.class);
33+
new DefaultSqlSessionFactory(configuration);
34+
}
35+
@Test
36+
public void testMixedConfiguration() throws Exception {
37+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_references/ibatisConfig.xml");
38+
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
sqlSessionFactory.getConfiguration().addMapper(PersonMapper2.class);
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.apache.ibatis.submitted.xml_references;
2+
3+
public class Person {
4+
public enum Type {
5+
EMPLOYEE,
6+
DIRECTOR
7+
}
8+
9+
private Long id;
10+
private String firstName;
11+
private String lastName;
12+
public String getFirstName() {
13+
return firstName;
14+
}
15+
public void setFirstName(String firstName) {
16+
this.firstName = firstName;
17+
}
18+
public String getLastName() {
19+
return lastName;
20+
}
21+
public void setLastName(String lastName) {
22+
this.lastName = lastName;
23+
}
24+
public Long getId() {
25+
return id;
26+
}
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.apache.ibatis.submitted.xml_references;
2+
3+
import java.util.List;
4+
5+
public interface PersonMapper {
6+
7+
public interface PersonType {
8+
public Person.Type getType();
9+
}
10+
11+
public List<Person> selectAllByType(Person.Type type);
12+
public List<Person> selectAllByTypeNameAttribute(Person.Type type);
13+
public List<Person> selectAllByTypeWithInterface(PersonType personType);
14+
public List<Person> selectAllByTypeNameAttributeWithInterface(PersonType personType);
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
6+
<mapper namespace="org.apache.ibatis.submitted.xml_references.PersonMapper">
7+
8+
<resultMap id="personMap" type="Person">
9+
<id property="id" column="id"/>
10+
<result property="firstName" column="firstName"/>
11+
<result property="lastName" column="lastName"/>
12+
</resultMap>
13+
14+
15+
<sql id="selectByIdScript">
16+
SELECT id, firstName, lastName, personType
17+
FROM person
18+
WHERE id = #{id}
19+
</sql>
20+
<select id="selectById" resultMap="personMap" parameterType="int">
21+
<include refid="selectByIdScript"/>
22+
</select>
23+
</mapper>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.apache.ibatis.submitted.xml_references;
2+
3+
import java.util.List;
4+
5+
public interface PersonMapper2 {
6+
7+
public interface PersonType {
8+
public Person.Type getType();
9+
}
10+
11+
public List<Person> selectAllByType(Person.Type type);
12+
public List<Person> selectAllByTypeNameAttribute(Person.Type type);
13+
public List<Person> selectAllByTypeWithInterface(PersonType personType);
14+
public List<Person> selectAllByTypeNameAttributeWithInterface(PersonType personType);
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
6+
<mapper namespace="org.apache.ibatis.submitted.xml_references.PersonMapper2">
7+
8+
<resultMap id="personMap" type="Person">
9+
<id property="id" column="id"/>
10+
<result property="firstName" column="firstName"/>
11+
<result property="lastName" column="lastName"/>
12+
</resultMap>
13+
14+
15+
<select id="selectById" resultMap="personMap" parameterType="int">
16+
<include refid="org.apache.ibatis.submitted.xml_references.PersonMapper.selectByIdScript"/>
17+
</select>
18+
</mapper>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
6+
<configuration>
7+
<typeAliases>
8+
<typeAlias alias="Person" type="org.apache.ibatis.submitted.xml_references.Person"/>
9+
</typeAliases>
10+
11+
<environments default="test">
12+
<environment id="test">
13+
<transactionManager type="JDBC"></transactionManager>
14+
<dataSource type="UNPOOLED">
15+
<property name="driver" value="org.hsqldb.jdbcDriver"/>
16+
<property name="url" value="jdbc:hsqldb:mem:xml_references"/>
17+
<property name="username" value="sa"/>
18+
</dataSource>
19+
</environment>
20+
</environments>
21+
22+
<mappers>
23+
<mapper resource="org/apache/ibatis/submitted/xml_references/PersonMapper.xml"/>
24+
</mappers>
25+
</configuration>

0 commit comments

Comments
 (0)