通过MyBatis拦截器添加当前用户会话数据作为全局参数

时间:2018-07-28 06:53:32

标签: java spring spring-security mybatis spring-mybatis

我想在执行myBatis查询时传递用户会话数据(用户ID,IP,用户名,电子邮件和/或权限等)。

我知道我能够添加全局变量。但是我得到了错误。它假定我的全局参数为模型变量。我该如何传递价值并获得和使用它。

    @Intercepts({@Signature(
        type= Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class})})
public class ConfigPropInterceptor implements Interceptor {

    private final Map<String, Object> properties = new HashMap<String, Object>();

    public Object intercept(Invocation invocation) throws Throwable {
        Object param = invocation.getArgs()[1];
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
        Configuration configuration = ms.getConfiguration();
        configuration.getVariables().put("param", "value");

    return invocation.proceed();

    }
} 

MyBatis插入呼叫

<insert id="insertCity" parameterType="City" useGeneratedKeys="true" keyProperty="id">
    insert into city(name, state, country) values("${param}",#{state},#{country})
</insert>

错误

There was an unexpected error (type=Internal Server Error, status=500).
nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.project.data.model.City'

0 个答案:

没有答案