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

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

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

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

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

ShareMapper.xml

<mapper namespace="com.lxw.ShareMapper">
        <sql id="Base_Column_List">
         id,name
        </sql>
    </mapper>

CustomMapper.xml

<mapper namespace="com.lxw.CustomMapper">
        <select id="selectSome" >
            select
            <include refid="com.lxw.ShareMapper.Base_Column_List"/>
            from t
        </select>
    </mapper>