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>
相关推荐
考虑考虑19 小时前
Mybatis实现批量插入
java·后端·mybatis
唐青枫3 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
掉头发的王富贵5 天前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
zzzzzz31010 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
云技纵横12 天前
唯一索引 INSERT 死锁实战:5 秒复现交叉插入的 S 锁循环等待
sql·mysql
BD_Marathon14 天前
SQL学习指南——视图
数据库·sql
2601_9620725514 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
敲个大西瓜14 天前
mybatis拦截器插件实现数据库字段加解密
mybatis
HackTwoHub14 天前
Sqli-Scanner SQL注入SKILL自动化挖掘SQL注入,零依赖自动化SQL注入挖掘,赏金猎人
数据库·人工智能·sql·web安全·网络安全·自动化·系统安全