假设xxx.xml中有类似下方的sql嵌套:
<?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">
<mapper namespace="com.xxx">
<!-- 片段1 -->
<sql id="sql1">
some sql
</sql>
<!-- 片段2 -->
<sql id="sql2">
some sql2
</sql>
<!-- 片段3 -->
<sql id="sql3">
<include refid="sql1" />
<include refid="sql2" />
</sql>
</mapper>
当你在yyy.xml中引入他的时候,可能会报错:
XML fragments parsed from previous mappers does not contain value for xxx
<?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">
<mapper namespace="com.yyy">
<select id="searchList">
some sql
<include refid="sql3" />
</select>
</mapper>
本人测试,把xxx.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">
<mapper namespace="com.xxx">
<!-- 片段1 -->
<sql id="sql1">
some sql
</sql>
<!-- 片段2 -->
<sql id="sql2">
some sql2
</sql>
<!-- 片段3 -->
<sql id="sql3">
<include refid="com.xxx.sql1" />
<include refid="com.xxx.sql2" />
</sql>
</mapper>
也就是在内部嵌套的时候,需要填写完整的引用id。
如果只是内部嵌套,那没问题。如果涉及到外部引用就会报错。