[MyBatis]マッパーの$と#の違い

  1. #着信値を文字列として受け取ります。たとえば、select id,name,age from student where id =#{id}現在の端がID値1をバックグラウンドに渡す場合、これはと同等select id,name,age from student where id ='1'です。

  2. $着信データを直接表示してSQLステートメントを生成します。たとえば、select id,name,age from student where id =${id}現在の端がID値1をバックグラウンドに渡す場合、これはと同等select id,name,age from student where id = 1です。

  3. 使用が#可能很大程度防止sql注入(文の連結)

  4. ただし、で使用する場合は、を使用order byする必要があります$

  5. ほとんどの場合#に使用されますが、さまざまな状況で使用する必要があります$

私はそれを考える#{}、SQL構文解析は、パラメータが引用されていない、着信値、。

  1. A:理解mybatis$#
在mybatis中的$与#都是在sql中动态的传入参数。

eg:select id,name,age from student where name=#{name}  这个name是动态的,可变的。当你传入什么样的值,就会根据你传入的值执行sql语句。
  1. 2:使用$して#
  • #{}:JDBCプリペアドステートメント(プリペアドステートメント)パラメーターマーカーに解決されます。1つ#{ }参数占位符として解釈されます
  • ${}:ちょうど1のために纯碎的 string 替换动态 SQL 解析阶段行われます变量替换

例:name–> cy

-- name='cy'
select id,name,age from student where name=#{name}
-- name=cy
select id,name,age from student where name=${name}

————————————————
元のリンク:https://blog.csdn.net/hao65103940/article/details/79099159

おすすめ

転載: blog.csdn.net/weixin_43438052/article/details/114189041