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>
相关推荐
鸿乃江边鸟3 天前
向量化和列式存储
大数据·sql·向量化
l1t3 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
懒虫虫~3 天前
通过内存去重替换SQL中distinct,优化SQL查询效率
java·sql·慢sql治理
逛逛GitHub3 天前
1 个神级智能问数工具,刚开源就 1500 Star 了。
sql·github
Huhbbjs4 天前
SQL 核心概念与实践总结
开发语言·数据库·sql
咋吃都不胖lyh4 天前
SQL-字符串函数、数值函数、日期函数
sql
sensenlin914 天前
Mybatis中SQL全大写或全小写影响执行性能吗
数据库·sql·mybatis
BXCQ_xuan4 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
wuyunhang1234564 天前
Redis----缓存策略和注意事项
redis·缓存·mybatis