Mybatis <include> <trim>标签详解

本文介绍了MyBatis中用于构建动态SQL的<include>和<trim>标签。<include>标签允许将重复的SQL片段封装,便于管理和维护,通过refid引用SQL片段ID。<trim>标签则用于动态生成SQL,去除多余的条件语句前缀,如AND和OR,以优化生成的SQL语句。文章详细阐述了这两个标签的用法和作用域,并提供了示例说明其工作原理。

一、标签< include>

标签用于将某个SQL片段作为一个整体引入到当前SQL语句中,可以将重复使用的SQL语句片段进行封装,方便管理和维护。在使用标签时,需要注意以下几点:

语法格式

<include refid="SQL片段ID"/>

其中,refid属性指向要引入的SQL片段的ID,例如:

<select id="selectUsers" resultMap="userResultMap">
  SELECT * FROM users
  <include refid="whereClause" />
</select>

<sql id="whereClause">
  <where>
    <if test="name != null">
      AND name = #{name}
    </if>
    <if test="age != null">
      AND age = #{age}
    </if>
  </where>
</sql>

在上述示例中,标签定义了一个SQL片段,其中包含了一个标签和两个标签,用于动态生成查询条件。标签的refid属性引用了该SQL片段,并将其包含在了标签中。

SQL片段的作用域
在Mybatis的XML配置文件中,SQL片段的作用域有两种:

(1)全局作用域:定义在标签外部的SQL片段可以在整个XML文件中使用。

(2)局部作用域:定义在、、或标签内部的SQL片段仅在当前标签的SQL语句中使用。

因此,在定义SQL片段时,需要根据实际情况选择合适的作用域,避免命名冲突或不必要的内存占用。

二、< trim>标签

标签也用于动态生成SQL语句,常用于去除多余的SQL关键字或条件语句。在使用标签时,需要注意以下几点:

语法格式

<trim prefix="前缀" suffix="后缀" prefixOverrides="需要去除的前缀" suffixOverrides="需要去除的后缀">
  SQL语句
</trim>

其中,prefix属性表示在SQL语句前添加的前缀,suffix属性表示在SQL语句后添加的后缀,prefixOverrides属性表示需要去除的前缀,suffixOverrides属性表示需要去除的后缀,假设我们有一个查询用户的SQL语句,其中可能会出现一些多余的AND或OR条件,我们可以使用trim标签来去除这些多余的部分,如下所示:

<select id="getUserList" resultType="User">
  SELECT * FROM user
  <trim prefix="WHERE" prefixOverrides="AND | OR">
    <if test="id != null">
      AND id = #{id}
    </if>
    <if test="name != null">
      AND name = #{name}
    </if>
    <if test="age != null">
      AND age = #{age}
    </if>
  </trim>
</select>

在这个示例中,我们使用了trim标签来去除SQL语句中可能出现的多余的WHERE、AND和OR关键字。如果id、name和age都为空,那么最终生成的SQL语句就是“SELECT * FROM user”,如果只有id不为空,那么最终生成的SQL语句就是“SELECT * FROM user WHERE id = ?”,如果只有name和age不为空,那么最终生成的SQL语句就是“SELECT * FROM user WHERE name = ? AND age = ?”,如果id、name和age都不为空,那么最终生成的SQL语句就是“SELECT * FROM user WHERE id = ? AND name = ? AND age = ?”。

我有一个xml,这个里面的又是什么意思呢? <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zingsemi.generator.mapper.GenTableColumnMapper"> <resultMap type="GenTableColumn" id="GenTableColumnResult"> <id property="columnId" column="column_id" /> <result property="tableId" column="table_id" /> <result property="columnName" column="column_name" /> <result property="columnComment" column="column_comment" /> <result property="columnType" column="column_type" /> <result property="javaType" column="java_type" /> <result property="javaField" column="java_field" /> <result property="isPk" column="is_pk" /> <result property="isIncrement" column="is_increment" /> <result property="isRequired" column="is_required" /> <result property="isInsert" column="is_insert" /> <result property="isEdit" column="is_edit" /> <result property="isList" column="is_list" /> <result property="isQuery" column="is_query" /> <result property="queryType" column="query_type" /> <result property="htmlType" column="html_type" /> <result property="dictType" column="dict_type" /> <result property="sort" column="sort" /> <result property="createBy" column="create_by" /> <result property="createTime" column="create_time" /> <result property="updateBy" column="update_by" /> <result property="updateTime" column="update_time" /> </resultMap> <sql id="selectGenTableColumnVo"> select column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_query, query_type, html_type, dict_type, sort, create_by, create_time, update_by, update_time from gen_table_column </sql> <select id="selectGenTableColumnListByTableId" parameterType="Long" resultMap="GenTableColumnResult"> <include refid="selectGenTableColumnVo"/> where table_id = #{tableId} order by sort </select> <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult"> select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName}) order by ordinal_position </select> <insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="true" keyProperty="columnId"> insert into gen_table_column ( <if test="tableId != null and tableId != ''">table_id,</if> <if test="columnName != null and columnName != ''">column_name,</if> <if test="columnComment != null and columnComment != ''">column_comment,</if> <if test="columnType != null and columnType != ''">column_type,</if> <if test="javaType != null and javaType != ''">java_type,</if> <if test="javaField != null and javaField != ''">java_field,</if> <if test="isPk != null and isPk != ''">is_pk,</if> <if test="isIncrement != null and isIncrement != ''">is_increment,</if> <if test="isRequired != null and isRequired != ''">is_required,</if> <if test="isInsert != null and isInsert != ''">is_insert,</if> <if test="isEdit != null and isEdit != ''">is_edit,</if> <if test="isList != null and isList != ''">is_list,</if> <if test="isQuery != null and isQuery != ''">is_query,</if> <if test="queryType != null and queryType != ''">query_type,</if> <if test="htmlType != null and htmlType != ''">html_type,</if> <if test="dictType != null and dictType != ''">dict_type,</if> <if test="sort != null">sort,</if> <if test="createBy != null and createBy != ''">create_by,</if> create_time )values( <if test="tableId != null and tableId != ''">#{tableId},</if> <if test="columnName != null and columnName != ''">#{columnName},</if> <if test="columnComment != null and columnComment != ''">#{columnComment},</if> <if test="columnType != null and columnType != ''">#{columnType},</if> <if test="javaType != null and javaType != ''">#{javaType},</if> <if test="javaField != null and javaField != ''">#{javaField},</if> <if test="isPk != null and isPk != ''">#{isPk},</if> <if test="isIncrement != null and isIncrement != ''">#{isIncrement},</if> <if test="isRequired != null and isRequired != ''">#{isRequired},</if> <if test="isInsert != null and isInsert != ''">#{isInsert},</if> <if test="isEdit != null and isEdit != ''">#{isEdit},</if> <if test="isList != null and isList != ''">#{isList},</if> <if test="isQuery != null and isQuery != ''">#{isQuery},</if> <if test="queryType != null and queryType != ''">#{queryType},</if> <if test="htmlType != null and htmlType != ''">#{htmlType},</if> <if test="dictType != null and dictType != ''">#{dictType},</if> <if test="sort != null">#{sort},</if> <if test="createBy != null and createBy != ''">#{createBy},</if> sysdate() ) </insert> <update id="updateGenTableColumn" parameterType="GenTableColumn"> update gen_table_column <set> column_comment = #{columnComment}, java_type = #{javaType}, java_field = #{javaField}, is_insert = #{isInsert}, is_edit = #{isEdit}, is_list = #{isList}, is_query = #{isQuery}, is_required = #{isRequired}, query_type = #{queryType}, html_type = #{htmlType}, dict_type = #{dictType}, sort = #{sort}, update_by = #{updateBy}, update_time = sysdate() </set> where column_id = #{columnId} </update> <delete id="deleteGenTableColumnByIds" parameterType="Long"> delete from gen_table_column where table_id in <foreach collection="array" item="tableId" open="(" separator="," close=")"> #{tableId} </foreach> </delete> <delete id="deleteGenTableColumns"> delete from gen_table_column where column_id in <foreach collection="list" item="item" open="(" separator="," close=")"> #{item.columnId} </foreach> </delete> </mapper>
最新发布
09-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值