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>
相关推荐
海尔辛37 分钟前
SQL 基础入门
数据库·sql
异常君3 小时前
MyBatis 中 SqlSessionFactory 和 SqlSession 的线程安全性深度分析
java·面试·mybatis
噼里啪啦啦.4 小时前
Spring事务和事务传播机制
数据库·sql·spring
李少兄4 小时前
解决 idea提示`SQL dialect is not configured` 问题
java·sql·intellij-idea
珹洺5 小时前
数据库系统概论(十七)超详细讲解数据库规范化与五大范式(从函数依赖到多值依赖,再到五大范式,附带例题,表格,知识图谱对比带你一步步掌握)
java·数据库·sql·安全·oracle
我科绝伦(Huanhuan Zhou)13 小时前
深入解析Oracle SQL调优健康检查工具(SQLHC):从原理到实战优化
数据库·sql·oracle
bing_15817 小时前
跨多个微服务使用 Redis 共享数据时,如何管理数据一致性?
redis·微服务·mybatis
神奇侠202417 小时前
Hive SQL常见操作
hive·hadoop·sql
一只叫煤球的猫17 小时前
MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
后端·sql·mysql
多多*18 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap