报错原因
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>
也就是条件应该用双引号引起来,外面一层使用单引号

博客主要讲述MyBatis报错原因,MyBatis用OGNL表达式解析,在OGNL里'1'会被解析成字符,而Java是强类型,char和string不等会使if标签中的sql无法解析,解决办法是将'1'改为'\1\'或加.toString()转换,还提及依据性别查询的错误与正确写法。
484

被折叠的 条评论
为什么被折叠?



