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>
相关推荐
漂着的圆木1 天前
Agent 功能参与度:Copilot 怎么算
sql·数据分析·agent·githubcopilot·度量
旺仔不是程序员2 天前
LIMIT 1:PostgreSQL 只取一行的高效查询姿势
数据库·后端·sql
大漠孤烟s2 天前
4.Mybatis学习-动态sql、缓存、逆向工程
mybatis
知识的搬运工旺仔2 天前
唯一索引与 NULL 值:PostgreSQL 主键约束与 NULLS NOT DISTINCT
数据库·后端·sql·postgresql
想念是会呼吸的鱼2 天前
【ClickHouse 常用 SQL 语句整理】
sql·clickhouse
想念是会呼吸的鱼3 天前
MySQL 常用语法整理
sql·mysql
SelectDB技术团队3 天前
StarRocks 迁移至 Apache Doris 完整指南:三步完成结构、数据与业务平滑切换
数据库·人工智能·sql·clickhouse·apache doris·selectdb·湖仓架构升级
imDwAaY3 天前
MySQL MVCC 详解:原理、版本链、Read View 与可见性判断
数据库·sql·mysql