Mybatis关于动态排序 #{} ${}问题
作者:LitongZero 发布时间:2023-09-01 17:34:57
Mybatis动态排序 #{} ${}问题
在写Mybatis动态排序是遇到一个问题,开始,我是这样写的
<if test="orderField !=null and orderField != '' ">
order by t.#{orderField} #{orderType}
</if>
发现报错,后来经过查阅资料发现,用#{}会多个' '导致SQL语句失效。
就是说,向上面这样的,连续使用#{}进行注入的,会导致SQL语句失效。
所以,改成${}注入就可以了
<if test="orderField !=null and orderField != '' ">
order by t.${orderField} ${orderType}
</if>
通过动态排序理解#{}和${}的区别
在日常开发中,尤其是在数据列表展示中,排序是最基本的功能。一般根据创建时间倒叙,但有可能碰到动态排序的需求。
接下来,我们将围绕由后台动态排序进行探讨
例如
现在,我们要查询一张店长表tb_director,我们在原有的父类中,新定义两个字段
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Entity基类
*
* @author 进击的Java君
*/
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
/** 排序列*/
private String orderField;
/** 排序规则,升降序*/
private String orderType;
/** 搜索值 */
private String searchValue;
/** 创建者 */
private String createBy;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
private String updateBy;
/** 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
private String remark;
/** 开始时间 */
@JsonIgnore
private String beginTime;
/** 结束时间 */
@JsonIgnore
private String endTime;
/** 请求参数 */
private Map<String, Object> params;
}
/**
* 店长表
* @author 进击的Java君
* @date 2021-03-18
*/
@Entity
@Getter
@Setter
@Table(name = "tb_director")
public class Director extends BaseEntity {
/** 主键id */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/** 店铺名称 */
@Column(name = "director_name", unique = true)
private String directorName;
/** 店铺地址 */
@Column(name = "director_adress", unique = true)
private String directorAdress;
}
现在,我们只需要在mapper.xml中加上sql过滤条件即可
<!-- 查询店长信息 -->
<sql id="selectDirectorVo">
select id, director_name,director_adress,director_num,director_create_time,director_up_time,openId
from tb_director
</sql>
<!-- 查询条件 -->
<sql id="sqlwhereSearch">
<where>
<if test="directorName !=null and directorName !=''">
AND director_name like concat('%', #{directorName}, '%')
</if>
<if test="openId !=null and openId !=''">
AND openId=#{openId}
</if>
<if test="id !=null and id !=''">
AND id=#{id}
</if>
<if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
AND date_format(directorCreateTime,'%y%m%d') >= date_format(#{beginTime},'%y%m%d')
</if>
<if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
AND date_format(directorCreateTime,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
</if>
</where>
<!-- 根据传入字段动态过滤 -->
<if test="orderField !=null and orderField != '' ">
order by ${orderField} ${orderType}
</if>
</sql>
<!-- 根据条件查询店长 -->
<select id="sel" parameterType="Director" resultMap="DirectorResult">
<include refid="selectDirectorVo"/>
<include refid="sqlwhereSearch"/>
</select>
持久层代码编完后,我们只需要在调用时,传入我们想进行排序的字段即可。
如下所示:
127.0.0.1:8080/api/director/sel?orderField=director_create_time&orderType=desc
但是这样的话,就需要我们对表中的字段非常清楚,如果觉得这样不舒服的话,我们可以对sql进行修改
<if test="orderField !=null and orderField != '' ">
order by
<choose>
<when test="orderField == 'directorName'">
director_name ${orderType}
</when>
<when test="orderField == 'openId'">
openId ${orderType}
</when>
<otherwise>
create_time ${orderType}
</otherwise>
</choose>
</if>
注意事项
使用这样连续拼接两个注入参数时,只能用${},不能用#{}。
如果使用#{orderField},则会被解析成ORDER BY “orderField”,这显然是一种错误的写法。
$ 符号一般用来当作占位符
#{}是sql的参数占位符,Mybatis会将sql中的#{}替换为?号,在sql执行前会使用PreparedStatement的参数设置方法,按序给sql的?号占位符设置参数值。
预编译的机制。预编译是提前对SQL语句进行预编译,而其后注入的参数将不会再进行SQL编译。我们知道,SQL注入是发生在编译的过程中,因为恶意注入了某些特殊字符,最后被编译成了恶意的执行操作。而预编译机制则可以很好的防止SQL注入。
来源:https://blog.csdn.net/LitongZero/article/details/83753327


猜你喜欢
- 1.ACSII码加密//ACSII码加密 private static string
- 前言最近我跟自定义View杠上了,甚至说有点上瘾到走火入魔了。身为菜鸟的我自然要查阅大量的资料,学习大神们的代码,这不,前两天正好在郭神在微
- 本文实例讲述了Android实现的仿淘宝购物车。分享给大家供大家参考,具体如下:夏的热情渐渐退去,秋如期而至,丰收的季节,小编继续着实习之路
- Java从json串中获取某个值java对象是不能直接传输,只有json对象 转成字符串 可以进行传输 故 传输中都是json进行的 接收到
- 在实际开发中经常需要了解具体对象的类型,所以经常会使用GetType()和typeof()、尽管可以得到相应的类型、但两者之间也存在一些差别
- @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时
- 序言springboot框架价值,可以简单快速的构建独立的spring生产级别应用。springboot主要有以下的特性:1.创建独立的Sp
- 1.实现阴影或模糊边效果方式:2.通过shape来实现,具体是通过layer-list 多层叠放的方式实现的<?xml version
- 引言使用SpringMVC作为Controller层进行Web开发时,经常会需要对Controller中的方法进行参数检查。本来Spring
- 本文研究的主要是利用spring的 * 自定义缓存的实现,具体实现代码如下所示。Memcached 是一个高性能的分布式内存对象缓存系统,用
- import java.util.ArrayList;import java.util.HashMap;import java.util.I
- 一般查询手机归属地内容应该很好用json格式保存,在网上找到了淘宝的归属地API,并下了处理json相关的jar包,做了这个手机归属地查询功
- 效果 使用compile 'site.gemus:openingstartanimation:1.0.0' //在gra
- 目录I. 环境配置1. 项目配置2. 数据库表II. 传参类型确定1. 参数类型为整形2. 指定jdbcType3. 传参类型为String
- 在使用官方的showModalBottomSheet这个组件时到目前为止遇到了三个比较坑的地方1. 无法直接设置圆角;2. 组件最多只能撑满
- 新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端两个工程的读写操作线程类基本上完全相同服务端:import java.io.B
- 在实际业务中,当后台数据发生变化,客户端能够实时的收到通知,而不是由用户主动的进行页面刷新才能查看,这将是一个非常人性化的设计。比如数字化大
- 1. System.Char 字符char 是 System.Char 的别名。System.Char 占两个字节,16个二进制位。Syst
- Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据。使用Reflections可以很轻松的获取以下
- 前言开发系统时,有时候在实现功能时,删除操作需要实现逻辑删除就是将数据标记为删除,而并非真的物理删除(非DELETE操作),查询时需要携带状