MyBatis动态SQL报错“badSql”,如何修改SQL语句使其正确执行?
一分耕耘,一分收获!既然都打开这篇,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新相关的内容,希望对大家都有所帮助!
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语句的语法正确,可以正常执行。
到这里,我们也就讲完了《MyBatis动态SQL报错“badSql”,如何修改SQL语句使其正确执行?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注主机宝贝公众号,带你了解更多关于的知识点!