简体   繁体   English

Spring / Mybatis映射中的范围变量

[英]Scope variables in spring/mybatis maps

I have a conversationScope.myVar="myValue" variable; 我有一个对话范围.myVar =“ myValue”变量;

I would like to use it inside a mybatis map such as, 我想在mybatis地图中使用它,例如,

select col1, col1, conversationScope.myVar as ScopeVar
from table1;

desired result

col1  col2  ScopeVar

xxxx  xxxx   myValue

yyyy  yyyy   myValue

...

MyBatis does support dynamic SQL in the nature you need. MyBatis确实支持您所需的动态SQL。 The big trick, is to use ${variable} instead of #{variable}. 最大的技巧是使用$ {variable}而不是#{variable}。 Just be careful, it leaves you susceptible to SQL injection attacks. 请注意,它容易受到SQL注入攻击的影响。 When using # MyBatis uses PreparedStatements to avoid SQL injection, and then 使用#时,MyBatis使用PreparedStatements避免SQL注入,然后

So take for example example, you had a Mapper interface with method. 例如,您有一个带方法的Mapper接口。

ComplexObject selectSomeObject(@Param("columnName") String columnName);

Your SQL map could use the parameter in part of the actual select code. 您的SQL映射可以在实际选择代码的一部分中使用参数。

ie

<select id="selectSomeObject" resultType="someObject"> 
    select t1.column as ${columnName}    
    from table1 t1
 </select>

If you need a global variable, check out this question. 如果您需要全局变量,请查看此问题。 MyBatis - defining a global parameter MyBatis-定义全局参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM