mybatis xml 映射文件 sql include 的用法

烂漫一生 提交于 2020-11-24 08:49:29

mybatis xml 文件中对于重复出现的sql 片段可以使用标签提取出来,在使用的地方使用标签引用即可具体用法如下:

<sql id="someSQL">
        id,name
</sql>
<select id="selectSome" >
        select
       <include refid="someSQL"/>
        from t
 </select> 

 

在中可以使用${}传入参数,如下:

<sql id="someSQL">
        ${tableName}.id,${tableName}.name
</sql>
<select id="selectSome" >
        select
       <include refid="someSQL"><property name="tableName" value="t"/></include>
        from t
 </select> 

对于多个xml文件需要同时引用一段相同的 可以在某个xml 中定义这个 sql 代码片段,在需要引用的地方使用全称引用即可,例子如下:

ShareMapper.xml
<mapper namespace="com.company.ShareMapper">       
    <sql id="someSQL">
       id,name
    </sql>          
</mapper>

CustomMapper.xml
<mapper namespace="com.company.CustomMapper">       
    <select id="selectSome" >
        select
       <include refid="com.company.ShareMapper.someSQL"/>
        from t
    </select>          
</mapper>

转自:https://blog.csdn.net/jslcylcy/article/details/65628390

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!