【xml sql片段效率提升】-sql 和 标签的使用

佐手、 提交于 2020-03-10 13:48:31

在 mybatis 的定义中, 节点是用来定义可重用的 SQL 代码段。
它可以被包含在其他语句里面, 使用 节点来包含。
而且, 它里面是可以使用 ${} 占位符参数化的(注意, 此处的参数不是调用时传进来的), 不同的属性值通过包含的实例而变化。

sql标签中id属性对应include标签中的refid属性。通过include标签将sql片段和原sql片段进行拼接成一个完整的sql语句进行执行。

<sql id="sqlid">
    res_type_id,res_type
</sql>

<select id="selectbyId" resultType="com.property.vo.PubResTypeVO">
    select
    <include refid="sqlid"/>
    from pub_res_type
</select>
#引用同一个xml中的sql片段
<include refid="sqlid"/>
#引用公用的sql片段
<include refid="namespace.sqlid"/>
#include标签中也可以用property标签,用以指定自定义属性。在sql标签中通过${}取出对应的属性值。

<select id="queryPubResType" parameterType="com.property.vo.PubResTypeVO" resultMap="PubResTypeList">
    select  a.res_type_id,
    <include refid="com.common.dao.FunctionDao.SF_GET_LNG_RES_TYPE">
        <property name="AI_RES_TYPE_ID" value="a.res_type_id"/>
        <property name="lng" value="#{lngId}"/>
        <property name="female" value="'女'"/>
    </include> as res_type
    from    pub_res_type a
</select>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!