Mybatis框架:动态sql中出现For input string: "要判断的值"的问题(一般出现在判断语句中)

报错原因


mybatis是用OGNL表达式来解析的,在OGNL的表达式中,'1'会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析,需要将'1'改为"1",或者加 .toString() 来转换。

 

报错点:


我这里像依据性别进行查询

错误的写法:

<select id="getStudentByCondition" parameterType="string" resultType="com.ctbu.stusys.domain.Student">
    select * from `student`
    <where>
        <if test="'男' == studentCondition || '女' == studentCondition">
            stu_sex=#{studentCondition}
        </if>
    </where>
</select>

正确的写法

<select id="getStudentByCondition" parameterType="string" resultType="com.ctbu.stusys.domain.Student">
    select * from `student`
    <where>
        <if test=' "男"== studentCondition || "女" == studentCondition'>
            stu_sex=#{studentCondition}
        </if>
    </where>
</select>

 

注意:test里面的 双引号 和 单引号

posted @ 2021-08-27 12:51  浅笑19  阅读(241)  评论(0编辑  收藏  举报