[MyBatis] The difference between $ and # in Mapper

  1. #It takes the incoming value as a string, eg:, select id,name,age from student where id =#{id}when the current end passes the id value of 1 to the background, it is equivalent to select id,name,age from student where id ='1'.

  2. $The incoming data is displayed directly generated sql statement, EG: select id,name,age from student where id =${id}, end the current id value of 1, passed back to the time equivalent select id,name,age from student where id = 1.

  3. Use #can be 很大程度on 防止sql注入. (Concatenation of sentences)

  4. But if it is used in order by, it needs to be used $.

  5. It is often used in most cases #, but must be used in different situations $.

I think that #with the {}incoming value, when sql parses, the parameter is not quoted.

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

eg:select id,name,age from student where name=#{name}  这个name是动态的,可变的。当你传入什么样的值,就会根据你传入的值执行sql语句。
  1. Two: use $and#
  • #{}: Resolves to a JDBC prepared statement (prepared statement) parameter markers, one #{ }is interpreted as a 参数占位符.
  • ${}: Just for one 纯碎的 string 替换, 动态 SQL 解析阶段will be carried out 变量替换.

eg: name–>cy

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

————————————————
Original link: https://blog.csdn.net/hao65103940/article/details/79099159

Guess you like

Origin blog.csdn.net/weixin_43438052/article/details/114189041