zoukankan      html  css  js  c++  java
  • ibatis中井号跟美元符号区别(#、$)

    1、#可以进行预编译,进行类型匹配,#变量名# 会转化为 jdbc 的 类型
    $不进行数据类型匹配,$变量名$就直接把 $name$替换为 name的内容
    例如:
    select * from tablename where id = #id# ,假设id的值为12,其中如果数据库字段id为字符型,那么#id#表示的就是'12',如果id为整型,那么#id#就是 12
    会转化为jdbc的 select * from tablename where id=?,把?参数设置为id的值
    select * from tablename where id = $id$ ,如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符型,
    那么Sql语句应该写成 select * from table where id = '$id$'

    3、#方式能够很大程度防止sql注入.
    4、$方式无法方式sql注入.
    5、$方式一般用于传入数据库对象.例如传入表名.
    6、所以ibatis用#比$好,一般能用#的就别用$.
    另外,使用##可以指定参数对应数据库的类型
    如:
    select * from tablename where id = #id:number#

    在做in,like 操作时候要特别注意


    总结以下:
    $号使用在具体pojo类也就是非基本类型的取值,而#号使用在具体有基本类型的取值

    <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>
    

      

  • 相关阅读:
    Java Output流写入包装问题
    SpringBoot项目单元测试不经过过滤器问题
    SpringSecurity集成启动报 In the composition of all global method configuration, no annotation support was actually activated 异常
    JWT jti和kid属性的说明
    Maven 排除依赖
    第五章 基因概念的发现
    第三章 孟德尔遗传的拓展
    第二章 孟德尔遗传
    第一章 引言
    GWAS全基因组关联分析
  • 原文地址:https://www.cnblogs.com/appinn/p/4815436.html
Copyright © 2011-2022 走看看