3

I am using Spring and MyBatis. I have tried to import another MyBatis file(which is auto-generated) by refering Can we import XML file into another XML file?.

1) MyTest.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" [
        <!ENTITY BaseTest.xml SYSTEM "BaseTest.xml" >
        ]>
<mapper namespace="com.test.abc">
    &BaseTest.xml;

    <select id="CUSTOMIZED_SELECT">
    ...
    </select>
    <insert id="CUSTOMIZED_INSERT">
       ...
    </insert>
</mapper>

2) BaseTest.xml. Actually it's just a XML snippet; it has no xml header nor outer mapper element:

<sql id="GENERAL_WHERE">
    ...
</sql>
<select id="GENERAL_SELECT">
    ...
</select>
<insert id="GENERAL_INSERT">
    ...
</insert>

These two files are located at the same directory. But Spring always complains that it can't find the BaseTest.xml.

Is there any special that I need to adjust ?

BTW, the reason behind BaseTest.xml and MyTest.xml is that BaseTest.xml is auto-generated and MyTest.xml is manual-written.

Thanks!

Community
  • 1
  • 1
user1040933
  • 277
  • 5
  • 14

1 Answers1

1

No you cannot import Mybatis XML mapper into another. If you could: what would happen to <mapper> namespace attribute?

<sql> fragments, <resultMap> defined in a mapper may be referenced from another mapper. just make sure to use full qualified name: namespace.id

e.g: <include refid="com.test.abc.TestMapper.sqlFragementId" />

Ans don't forget that if included fragment also includes a fragment, references will be made from the root mapper, then using full qualified name may be required even when referencing fragment in the same mapper. Anyway, keeping with simple id reference allows overriding (a kind of inheritance). But this is to handle with care.

blackwizard
  • 2,034
  • 1
  • 9
  • 21
  • Thanks for the answer. I know this can works but that's not what I want. In essence, MyBatis is a XML file, so it should be able to include another XML file unchagedly according to http://stackoverflow.com/questions/5121052/can-we-import-xml-file-into-another-xml-file. Of course, we should not add the element in the BaseTest.xml. – user1040933 Apr 05 '17 at 00:53
  • I have re-edit my question to include an example of BaseTest.xml so that it's more clear. – user1040933 Apr 05 '17 at 00:59