test条件中单引号双引号问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

test条件中单引号双引号问题

小生小生小小生   2022-05-23 我要评论

test条件中单引号双引号问题

在mybatis中test判断条件中使用单引号会报错 通常使用双引号

通常test后的判断条件写在双引号内,但是当条件中判断使用字符串时应该如下方式开发

<when  test=“channel ==null” >
<when  test='channel =="QT"' >

具体原因

为单引号会被mybatis默认为字符类型,若为单字符可以使用单引号。否则会报错。 

动态sql中test的一些问题

mybatis动态sql中OGNL中type=="1"和type='1'的区别

最近在mybatis中使用OGNL所遇到的坑: 一点鸡肋: type=="1"和type='1'的区别

//CountryDao.java
public interface CountryDao {
    CountryDo getByType(@Param("type") String type);
}
//CountryManager.java
@Component("countryManager")
public class CountryManager {
    @Autowired
    private CountryDao countryDao;
    public CountryDo getByType(String type){
        return countryDao.getByType(type);
    }
}

mapper.xml:

//.xml
<select id="getByType" resultType="com.github.**.domain.CountryDo">
    <choose>
      //注意写法`type=='0'`
        <when test="type == '0'">
            SELECT * FROM country WHERE country_id = 2
        </when>
        <otherwise>
            SELECT * FROM country WHERE country_id = 3;
        </otherwise>
    </choose>
</select>

测试案例:

@Test
public void getByTypeTest() throws JsonProcessingException {
    String type = "0";
    CountryDo countryDo = countryManager.getByType(type);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(countryDo);
    System.out.println(json);
}

理想情况下,当type=0时返回的是country_id=2的结果

测试结果:

{"countryId":3,"country":"American Samoa","lastUpdate":"2006-02-15 04:44:00.0"}

居然返回的是country_id=3的结果

百度了才知道

原来传值进去的是String类型,而mybatis的动态sql中的OGNL表达式不会将单引号包裹的char类型的内容转成String类型,并且String类型和char类型直接比较一定是不相等的。所以输出是country_id=3的结果

解决方案

将<when test="type == '0'">换成<when test='type == "0"'>即可,即将单引号换成双引号

0==’'问题

一个更有趣的问题,如果将mapper.xml文件内容换成:

<select id="getByType" resultType="com.github.sijing.domain.CountryDo">
    <choose>
        <when test="0 ==''">
            SELECT * FROM country WHERE country_id = 2
        </when>
        <otherwise>
            SELECT * FROM country WHERE country_id = 3;
        </otherwise>
    </choose>
</select>

测试案例不变:

@Test
public void getByTypeTest() throws JsonProcessingException {
    //此时传值已经没有影响了
    String type = "0";
    CountryDo countryDo = countryManager.getByType(type);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(countryDo);
    System.out.println(json);
}

测试结果为:

{"countryId":2,"country":"Algeria","lastUpdate":"2006-02-15 04:44:00.0"}

怎么还是country_id=2的结果?,难道0==''??

查看mybatis源码发现,比如对于动态sql<if>标签的解析:

// IfSqlNode.
@Override
 public boolean apply(DynamicContext context) {
   if (evaluator.evaluateBoolean(test, context.getBindings())) {
     contents.apply(context);
     return true;
   }
   return false;
 }

在evaluator.evaluateBoolean方法中:

//ExpressionEvaluator.java OGNL表达式处理
public boolean evaluateBoolean(String expression, Object parameterObject) {
  Object value = OgnlCache.getValue(expression, parameterObject);
  if (value instanceof Boolean) {
    return (Boolean) value;
  }
  if (value instanceof Number) {
    return new BigDecimal(String.valueOf(value)).compareTo(BigDecimal.ZERO) != 0;
  }
  return value != null;
}

当传入值为0时,因为0是Number类型,所以被转成了BigDecimal类型,并与0作比较,test的结果为true,所以返回的是country_id=2的结果,这个说法有误。。。

再次调试,首先char ''被转成了字符String"",大概在OgnlOps.java文件的isEqual方法中的(compareWithConversion(object1, object2) == 0),点进去发现:

case NONNUMERIC:
    if ((t1 == NONNUMERIC) && (t2 == NONNUMERIC)) {
        if ((v1 instanceof Comparable) && v1.getClass().isAssignableFrom(v2.getClass())) {
            result = ((Comparable) v1).compareTo(v2);
            break;
        } else {
            throw new IllegalArgumentException("invalid comparison: " + v1.getClass().getName() + " and "
                    + v2.getClass().getName());
        }
    }
    // else fall through
case FLOAT:
case DOUBLE:
    //v1=0, v2=""
    double dv1 = doubleValue(v1),
    dv2 = doubleValue(v2);
    return (dv1 == dv2) ? 0 : ((dv1 < dv2) ? -1 : 1);

在doubleValue方法中:

public static double doubleValue(Object value)
        throws NumberFormatException
    {
        if (value == null) return 0.0;
        Class c = value.getClass();
        if (c.getSuperclass() == Number.class) return ((Number) value).doubleValue();
        if (c == Boolean.class) return ((Boolean) value).booleanValue() ? 1 : 0;
        if (c == Character.class) return ((Character) value).charValue();
        String s = stringValue(value, true);
        return (s.length() == 0) ? 0.0 : Double.parseDouble(s);
    }

坑坑坑!!!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们