springboot整合mybatis时,报错提示:
Failed to load ApplicationContext
...
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commentMapper' defined in file [D:\code\springbootCode\chapter03\target\classes\com\nanding\mapper\CommentMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{content),#{author}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
期中关键的一条:Check syntax #{property|(expression), var1=value1, var2=value2, …}
通过该条提示:找到了参考博客,错误跟我的一模一样,链接是:https://www.cnblogs.com/dream-0916/p/5966429.html
错误代码如下:
@Mapper
public interface CommentMapper {
@Select("select * from t_comment where id=#{id}")
public Comment findById(Integer id);
@Insert("insert into t_comment(content,author,a_id) values(#{content),#{author},#{aId})")
public int insertComment(Comment comment);
@Update("update t_comment set content=#{content} where id=#{id}")
public int updateComment(Comment comment);
@Delete("delete from t_comment where id=#{id}")
public int deleteComment(Integer id);
}
错误的原因藏在@Insert注解中sql语句的编写,#{content) 的后面那个括号与#{}不匹配,从而会产生Check syntax #{property|(expression), var1=value1, var2=value2, …} 异常。
此刻心里奔腾着。。。又是花了一个小时,用更复杂的思路去排查了这个字符的拼写,浪费时间,效率低下。
小结:拼写的SQL字符串,编辑器不能提示出错误的地方,最好是通过SQL工具执行一次,然后再写进来。还有,绝大多数效率底的原因是排查bug的思路四处碰壁,应该按套路来,第一步应先仔细检查自己的代码,随后再按情况来分析。