mybatis mapper.xml文件的sql书写技巧

本文介绍了在MyBatis的Mapper XML文件中书写SQL时的注意事项,包括语句末尾禁止使用分号,如何转义大于号`>`,以及在处理字符串拼接时对双引号和单引号的转义方法。同时,推荐使用`<![CDATA[ ... ]]>`包裹含有特殊字符的SQL片段,并展示了如何利用`StringEscapeUtils`进行字符串的转义和反转义操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.语句末尾不能有分号  ;

示例:

 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from hkbi_meta_datasource
    where id = #{id,jdbcType=INTEGER}
  </select>

2.对 sql中有比较大小的需要转义 < , > 。 这里省略转义,使用<![CDATA[   sql      ]]>。

示例:

<<=>>=&'"
&lt;&lt;=&gt;&gt;=&amp;&apos;&quot;

示例:

select name from tbl_user where age > #{age}

<![CDATA[  select name from tbl_user where age > #{age}   ]]>

age&gt;= #{age}

使用<![CDATA[   sql      ]]> 方法为xml中的语法。在CDATA内部的所有内容都会被解析器忽略. 但是因此会导致<if test=""></if> <where></where> <choose></choose> <trim></trim>等标签本身功能失效,所以使用时把带有特殊字符的语句或字符本身放在<![CDATA[ ]]>内即可,即尽量精确其作用范围.

3.sql语句存入的内容为拼接字符串, 对双引号,单引号等特殊符号做转义处理。

示例:使用org.apache.commons.lang3下的包

  sql存库 :  StringEscapeUtils.escapeJava(conent);    

sql查询取出:   StringEscapeUtils.unescapeJava(conent);  

类似的方法还有 escapeSql, html,javascript,xml 等方法。大家可尝试使用

pom导包

<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.8.1</version>
		</dependency>

4.插入一段sql,记录下常用标签的使用

<sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>


 <insert id="insertSelective" parameterType="org.triber.dghabi.model.ConfigDatasource">
    insert into hki_meta_datasource
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="dName != null">
        d_name,
      </if>
      <if test="dType != null">
        d_type,
      </if>
      <if test="dUrl != null">
        d_url,
      </if>
      <if test="dUsername != null">
        d_username,
      </if>
      <if test="dPassword != null">
        d_password,
      </if>
      <if test="dDriverClass != null">
        d_driver_class,
      </if>
      <if test="dQueryClass != null">
        d_query_class,
      </if>
      <if test="options != null">
        options,
      </if>
      <if test="dDesc != null">
        d_desc,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="dName != null">
        #{dName,jdbcType=VARCHAR},
      </if>
      <if test="dType != null">
        #{dType,jdbcType=VARCHAR},
      </if>
      <if test="dUrl != null">
        #{dUrl,jdbcType=VARCHAR},
      </if>
      <if test="dUsername != null">
        #{dUsername,jdbcType=VARCHAR},
      </if>
      <if test="dPassword != null">
        #{dPassword,jdbcType=VARCHAR},
      </if>
      <if test="dDriverClass != null">
        #{dDriverClass,jdbcType=VARCHAR},
      </if>
      <if test="dQueryClass != null">
        #{dQueryClass,jdbcType=VARCHAR},
      </if>
      <if test="options != null">
        #{options,jdbcType=VARCHAR},
      </if>
      <if test="dDesc != null">
        #{dDesc,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值