Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


MyBatis ${} 和 #{} 区别

MyBatis 大约 437 字

区别

${}本质是字符串拼接

#{}本质是占位符赋值

应用

#{}可以防止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},'%')
阅读 668 · 发布于 2022-09-11

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

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

扫描二维码关注我
昵称:
随便看看 换一批