
mybatis动态sql报错征解
在使用mybatis进行动态sql操作时,遇到报错提示"badsql",可能的原因是sql语句存在语法错误。
针对提供的sql语句:
select * from table a
<where>
a.project_id=#{projectid}
and a.id != #{id}
and a.status=3
<choose>
<when test="type == idcard">
and a.id_card = #{code}
</when>
<when test="type == unitcode">and a.unit_code = #{code}</when>
<otherwise>
</otherwise>
</choose>
</where>修改后,正确的sql语句应该如下:
select * from table a
<where>
a.project_id=#{projectId}
and a.id != #{id}
and a.status=3
<choose>
<when test="type == 'idCard'">
and a.id_card = #{code}
</when>
<when test="type == 'unitCode'">
and a.unit_code = #{code}
</when>
<otherwise>
</otherwise>
</choose>
</where>修改后的部分包括:
- 将test='type == idcard'修改为test="type == 'idcard'",添加了引号。
- 将test='type == unitcode'修改为test="type == 'unitcode'",添加了引号。
- 添加了<otherwise></otherwise>语句块。
修改后,sql语句的语法正确,可以正常执行。










