0x04OGNL失效

问题描述:

mybatis动态where中条件如下:

            <if test="dto.allDsr == 0">
                and 1 = 1
            </if>
            <if test="dto.allDsr == '0'">
                and 2 = 2
            </if>
            <if test='dto.allDsr == "0"'>
                and 3 = 3
            </if>


参数dto中allDsr是String类型的”0”。why 最终结果中有1=1,3=3 却没有2=2?

just debug:

image-20250806160613564


image-20250806160712999


image-20250806160820904

image-20250806160912489

image-20250806161055214

dto.allDsr == 0 :

image-20250806161240865

equal中会将其转换为double进行比较

image-20250806161531012

故满足表达式。


dto.allDsr == ‘0’:

image-20250806162011056

注意v2是Character ‘0’


dto.allDsr == “0”:

image-20250806162313324

V1 v2都是字符串”0”


OGNL比较中,有一方是数字就会将结果转换为数字再比较,否则直接比较,这里的坑就在于java是强类型,char与string。