Mybatis中配置文件注释的一个小坑

记录一个xml中的小坑

在Mybatis的配置文件中Mapper.xml中关于注释的问题
如果在<select></select>标签里面添加了注释,注释里面包含#{value}取值符字样的话,Mybatis一样会解析该取值符,造成sql语句执行时报错

例子Example

<select id="findUser" parameterType="java.lang.String" resultType="sysUser">
        --#{id} #{phone}
        /* #{id} #{phone} */
        <!-- #{id} #{phone} -->
        select * from sys_user where name = #{name}
</select>

异常Exception

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [name, param1]

    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
    at com.sun.proxy.$Proxy112.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:167)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)

总结

mybatis的mapper文件里有三种注释,1.--#{id} #{phone},2./*#{id} #{phone}*/,3. <!-- #{id} #{phone} -- > 。如果用1,2类型就会报如下异常,用3类型注释就正常执行。
删除<select></select>标签的异常,或者将注释移出到标签外面,或者使用3类型注释,成功执行
就是这么尴尬!!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。