一文解惑mybatis中的#{}和${}

简介: 一文解惑mybatis中的#{}和${}

基本概述

#{}:先编译sql语句,再给占位符传值,底层是PreparedStatement实现。可以防止sql注入,比较常用。

${}:先进行sql语句拼接,然后再编译sql语句,底层是Statement实现。存在sql注入现象。只有在需要进行sql语句关键字拼接的情况下才会用到。

#{}的基本使用

1. <select id="selectById" resultType="pojo.Car">
2.         select  * from t_car where id=#{id}
3. </select>

通过执行可以清楚的看到,sql语句中是带有 ? 的,这个 ? 就是大家在JDBC中所学的占位符,专门用来接收值的。

这就是 #{},它会先进行sql语句的预编译,然后再给占位符传值

${}的基本使用

1. <select id="selectByCarType" resultType="com.study.mybatis.pojo.Car">
2.         select
3.             id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.         from
5.             t_car
6.         where
7.             car_type = ${carType}
8. </select>

很显然,${} 是先进行sql语句的拼接,然后再编译,出现语法错误是正常的,因为 燃油车 是一个字符串,在sql语句中应该添加单引号

1. <select id="selectByCarType" resultType="com.study.mybatis.pojo.Car">
2.         select
3.             id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.         from
5.             t_car
6.         where
7. <!--car_type = #{carType}-->
8. <!--car_type = ${carType}-->
9.             car_type = '${carType}'
10. </select>

原则:能用 #{} 就不用 ${}

${}使用情况

当需要进行sql语句关键字拼接的时候。必须使用${}

sql排序asc|desc

需求:通过向sql语句中注入asc或desc关键字,来完成数据的升序或降序排列。

1. <select id="selectAll" resultType="com.study.mybatis.pojo.Car">
2.   select
3.   id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.   from
5.   t_car
6.   order by carNum ${key}
7. </select>

desc是一个关键字,不能带单引号的,所以在进行sql语句关键字拼接的时候,必须使用${}

拼接表名

业务背景:实际开发中,有的表数据量非常庞大,可能会采用分表方式进行存储,比如每天生成一张表,表的名字与日期挂钩,例如:2022年8月1日生成的表:t_user20220108。2000年1月1日生成的表:t_user20000101。此时前端在进行查询的时候会提交一个具体的日期,比如前端提交的日期为:2000年1月1日,那么后端就会根据这个日期动态拼接表名为:t_user20000101。有了这个表名之后,将表名拼接到sql语句当中,返回查询结果。那么大家思考一下,拼接表名到sql语句当中应该使用#{} 还是 ${} 呢?

使用#{}会是这样:select * from 't_car'

使用${}会是这样:select * from t_car

1. <select id="selectAllByTableName" resultType="car">
2.   select
3.   id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.   from
5.   ${tableName}
6. </select>

批量删除

业务背景:一次删除多条记录。

对应的sql语句:

  • delete from t_user where id = 1 or id = 2 or id = 3;
  • delete from t_user where id in(1, 2, 3);

假设现在使用in的方式处理,前端传过来的字符串:1, 2, 3

如果使用mybatis处理,应该使用#{} 还是 ${}

使用#{} :delete from t_user where id in('1,2,3')执行错误:1292 - Truncated incorrect DOUBLE value: '1,2,3'

使用${} :delete from t_user where id in(1, 2, 3)

1. <delete id="deleteBatch">
2.   delete from t_car where id in(${ids})
3. </delete>

模糊查询

需求:查询奔驰系列的汽车。【只要品牌brand中含有奔驰两个字的都查询出来。】

使用${}处理方法:

1. <select id="selectLikeByBrand" resultType="Car">
2.   select
3.   id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.   from
5.   t_car
6.   where
7.   brand like '%${brand}%'
8. </select>

使用#{}

第一种:concat函数

1. <select id="selectLikeByBrand" resultType="Car">
2.   select
3.   id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.   from
5.   t_car
6.   where
7.   brand like concat('%',#{brand},'%')
8. </select>

第二种:双引号方式

1. <select id="selectLikeByBrand" resultType="Car">
2.   select
3.   id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
4.   from
5.   t_car
6.   where
7.   brand like "%"#{brand}"%"
8. </select>


相关文章
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17488 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1374 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2432 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3938 22
|
20天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章