MyBatis ${} 和 #{} 区别

MyBatis About 437 words

区别

${}本质是字符串拼接

#{}本质是占位符赋值

应用

#{}可以防止SQL注入,但有些场景还是需要${},比如如下查询语句,${}就不会加上单引号(需注意MySQLPostgreSQL对关键字加引号的处理不一致)。

select * from ${tableName}

模糊查询

MySQL

select * from user where username like "%"'keyword'"%"

MyBatis使用#{}占位

select * from user where username like "%"#{keyword}"%"

PostgreSQL

select * from "user" where username like CONCAT('%','keyword','%')

MyBatis使用#{}占位

select * from "user" where username like CONCAT('%',#{keyword},'%')
Views: 902 · Posted: 2022-09-10

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh