首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >一文解惑mybatis中的#{}和${}

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

作者头像
一个风轻云淡
发布2023-10-15 10:51:21
发布2023-10-15 10:51:21
1.2K0
举报
文章被收录于专栏:java学习javajava学习java
基本概述

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

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

 #{}的基本使用
代码语言:javascript
复制
    <select id="selectById" resultType="pojo.Car">
        select  * from t_car where id=#{id}
    </select>

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

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

${}的基本使用 
代码语言:javascript
复制
    <select id="selectByCarType" resultType="com.study.mybatis.pojo.Car">
        select
            id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
        from
            t_car
        where
            car_type = ${carType}
    </select>

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

代码语言:javascript
复制
    <select id="selectByCarType" resultType="com.study.mybatis.pojo.Car">
        select
            id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
        from
            t_car
        where
            <!--car_type = #{carType}-->
            <!--car_type = ${carType}-->
            car_type = '${carType}'
    </select>

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

 ${}使用情况

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

sql排序asc|desc

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

代码语言:javascript
复制
<select id="selectAll" resultType="com.study.mybatis.pojo.Car">
  select
  id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
  from
  t_car
  order by carNum ${key}
</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

代码语言:javascript
复制
<select id="selectAllByTableName" resultType="car">
  select
  id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
  from
  ${tableName}
</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)

代码语言:javascript
复制
<delete id="deleteBatch">
  delete from t_car where id in(${ids})
</delete>
模糊查询

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

使用${}处理方法: 

代码语言:javascript
复制
<select id="selectLikeByBrand" resultType="Car">
  select
  id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
  from
  t_car
  where
  brand like '%${brand}%'
</select>

使用#{}

第一种:concat函数

代码语言:javascript
复制
<select id="selectLikeByBrand" resultType="Car">
  select
  id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
  from
  t_car
  where
  brand like concat('%',#{brand},'%')
</select>

第二种:双引号方式

代码语言:javascript
复制
<select id="selectLikeByBrand" resultType="Car">
  select
  id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
  from
  t_car
  where
  brand like "%"#{brand}"%"
</select>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本概述
  •  #{}的基本使用
  • ${}的基本使用 
  •  ${}使用情况
    • sql排序asc|desc
    • 拼接表名
    • 批量删除 
    • 模糊查询
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档