Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用param{}在Mybatis中定义别名字段_Mysql_Mybatis - Fatal编程技术网

Mysql 如何使用param{}在Mybatis中定义别名字段

Mysql 如何使用param{}在Mybatis中定义别名字段,mysql,mybatis,Mysql,Mybatis,我使用springboot、mybatis 3.4.4和Mysql 5.7,在mybatis mapper中,我需要将sql参数${}更改为{}, 我的sql是这样的: select * from t1 as record <if test="orderBy != null and sort != null"> order by vr.${orderBy} ${sort} </if> 我打印sql,它是正确的,编译后的sql如下: select *

我使用springboot、mybatis 3.4.4和Mysql 5.7,在mybatis mapper中,我需要将sql参数${}更改为{}, 我的sql是这样的:

select * from t1 as record
<if test="orderBy != null and sort != null">
order by
vr.${orderBy} ${sort}
</if>
我打印sql,它是正确的,编译后的sql如下:

select * from t1 as record
order by
concat('vr.', ?) desc
但是,如果不按sortBy字段排序,则不起作用,然后我在DataGrip中的一个简单测试表上进行测试,如果我使用quote输入param,则不起作用,我怀疑mybatis中的param输入是一个带quote的字符串,而不是值本身

问题:如何解决这个问题以让排序工作。

JDBC驱动程序可以在SQL语句中的有限点应用参数,并且每个数据库/驱动程序允许为它们设置不同的位置。参数所在的位置不是参数的典型位置,因此对它的支持可能很粗略

为了安全起见,我通常会:

select * 
from t1 as record
<if test="orderBy != null and sort != null">
  order by
  <choose>
    <when test="orderBy = 'id'">id</when>
    <when test="orderBy = 'name'">name</when>
    <when test="orderBy = 'address'">address</when>
    <otherwise>status</otherwise>
  </choose>
  <if test="'desc'.equalsIgnoreCase(sort)">desc</if>
</if>

但是,它不起作用-您有错误吗?@打印机不是错误,但结果不按目标字段排序
select * 
from t1 as record
<if test="orderBy != null and sort != null">
  order by
  <choose>
    <when test="orderBy = 'id'">id</when>
    <when test="orderBy = 'name'">name</when>
    <when test="orderBy = 'address'">address</when>
    <otherwise>status</otherwise>
  </choose>
  <if test="'desc'.equalsIgnoreCase(sort)">desc</if>
</if>