mybatis xml 文件 sql include 的用法

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>
相关推荐
音视频工程实战5 小时前
PromptQL 新手入门与实战指南
数据库·sql·算法
清川渡水5 小时前
NL2SQL 的正确打开方式:从「AI 猜 SQL」到「置信度闭环」的工程化落地
数据库·人工智能·sql
IT界的老黄牛7 小时前
PostgreSQL 常用命令速查:MySQL 用户平滑上手,psql 元命令 + 库表管理 + 运维排查一篇通
sql·postgresql·psql·数据库运维·命令速查·mysql 对比
阿里云云原生8 小时前
AI Agent 上线容易稳定难?阿里云 AgentLoop 推出“经验自进化”闭环治理方案
人工智能·阿里云·mybatis·agentscope
祝威廉10 小时前
四条语句跑完机器学习:让模型成为一个 SQL 函数
人工智能·sql·机器学习
han_hanker11 小时前
SQL语法 , BETWEEN ... AND ...,比较运算符
前端·javascript·sql
爱喝水的鱼丶11 小时前
SAP-ABAP:SELECT大数据量查询性能调优——避免嵌套循环、减少数据库交互的核心方案
开发语言·数据库·sql·性能优化·sap·abap·erp
三84412 小时前
get方法/post方法/SQL注入文字型/数字型
数据库·sql·mysql
CodeStats1 天前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
Lucifer三思而后行1 天前
Veeam 备份 Oracle RAC 失败,是归档删除脚本的锅!
sql