Mybatis中不能使用非静态方法,不能使用非静态变量,OGNL表达式用法

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/86515664

1Mybatis中使用非静态方法会报错

package com.weather.weatherexpert.common.utils;


/**
 * @Desc
 * @Author liuyazhou
 * @CreateTime 2018/6/13 21:01
 **/
public class StringUtils {
    public boolean isNotBlank(String str) {
        return org.apache.commons.lang.StringUtils.isNotBlank(str);
    }
}

<if test="@com.weather.weatherexpert.common.utils.StringUtils@isNotBlank(cityName)">
	and city_name=#{cityName}
</if>

Cause: org.apache.ibatis.ognl.MethodFailedException: Method "isNotBlank" failed 
for object class com.weather.weatherexpert.common.utils.StringUtils 
[java.lang.NoSuchMethodException: isNotBlank(java.lang.String)]] with root cause
java.lang.NoSuchMethodException: isNotBlank(java.lang.String)

2 Mybatis中使用静态方法正常运行

public static boolean isNotBlank(String str) {
	return org.apache.commons.lang.StringUtils.isNotBlank(str);
}		
	
<if test="@com.weather.weatherexpert.common.utils.StringUtils@isNotBlank(cityName)">
and city_name=#{cityName}
</if>	
	
DEBUG: 2019-01-15 13:58:14 [com..queryByTableType:159] ==>  Preparing: select city_name from area_table where 1=1 and city_name=? 
DEBUG: 2019-01-15 13:58:14 [com..queryByTableType:159] ==> Parameters: 忻州(String)
DEBUG: 2019-01-15 13:58:14 [com..queryByTableType:159] <==      Total: 16	

3 类前面不带@报错

<if test="com.weather.weatherexpert.common.utils.StringUtils@isNotBlank(cityName)">
	and city_name=#{cityName}
</if>		

Caused by: org.apache.ibatis.ognl.ExpressionSyntaxException: 
Malformed OGNL expression: com.weather.weatherexpert.common.utils.StringUtils@isNotBlank(cityName)
at org.apache.ibatis.ognl.Ognl.parseExpression(Ognl.java:112) ~[mybatis-3.4.6.jar:3.4.6]	

4 Mybatis中使用静态变量正常运行,使用非静态变量会报错

public class CityClass {

//    public static String XINZHOU = "忻州";//正确
//    <if test="cityName == @com.weather.weatherexpert.common.utils.CityClass@XINZHOU">

    public  String XINZHOU = "忻州";//错误:
    // <if test="cityName == @com.weather.weatherexpert.common.utils.CityClass@XINZHOU">
    //Caused by: org.apache.ibatis.ognl.OgnlException: Field XINZHOU of class com.weather.weatherexpert.common.utils.CityClass is not static
    

OGNL 表达式

  1. 常量: 字符串:“ hello ” 字符:‘ h ’ 数字:除了像 java 的内置类型 int,long,float 和 double,Ognl 还有如例:10.01B,相当于 java.math.BigDecimal,使用’ b ’或者’ B ’后缀。 100000H,相当于 java.math.BigInteger,使用’ h ’ 或 ’ H ’ 后缀。
  2. 属性的引用 例如:user.name
  3. 变量的引用 例如:#name
  4. 静态变量的访问 使用 @class@field
  5. 静态方法的调用 使用 @class@method(args), 如果没有指定 class 那么默认就使用 java.lang.Math.
  6. 构造函数的调用 例如:new java.util.ArrayList();

其它的 Ognl 的表达式可以参考 Ognl 的语言手册。

参考:https://www.ibm.com/developerworks/cn/opensource/os-cn-ognl/index.html

OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。                

OGNL表达式介绍 https://www.cnblogs.com/renchunxiao/p/3423299.html

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/86515664