Springboot 问题记录
父级起步依赖版本1.5.9.RELEASE
一、springboot整合mybatis报错。
错误一
**原因:application.properties中混淆了mybatis.config-location=classpath:com/taotao/springbootmybatis/mapper/*.xml
和mybatis.config-locations=classpath:com/taotao/springbootmybatis/mapper/*.xml
前者参数为String,且不支持通配符。后者参数为String[],支持。
日志如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘mybatisTestController’: Unsatisfied dependency expressed through field ‘service’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘mybatisTestServiceImpl’: Unsatisfied dependency expressed through field ‘tbItemMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘tbItemMapper’ defined in file [I:\TaoTao\gitee\code01\springboot-mybatis\target\classes\com\taotao\springbootmybatis\mapper\TbItemMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is java.io.FileNotFoundException: class path resource [com/taotao/springbootmybatis/mapper/*.xml] cannot be opened because it does not exist
错误二:
原因:其实也是混淆了mybatis.config-location以及mybatis.config-locations两个配置项。
错误的配置了mybatis.config-location=classpath:com/taotao/springbootmybatis/mapper/TbItemMapper.xml
正确配置为mybatis.config-locations=classpath:com/taotao/springbootmybatis/mapper/*.xml
日志:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘mybatisTestController’: Unsatisfied dependency expressed through field ‘service’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘mybatisTestServiceImpl’: Unsatisfied dependency expressed through field ‘tbItemMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘tbItemMapper’ defined in file [I:\TaoTao\gitee\code01\springboot-mybatis\target\classes\com\taotao\springbootmybatis\mapper\TbItemMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [com/taotao/springbootmybatis/mapper/TbItemMapper.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.NullPointerException
二、springboot中的406(Not Acceptable)错误
可能的原因:
1.未引入jackson的相关jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
这个一般在SSM框架中很可能出现,但是在springboot中,只要依赖了spring-boot-starter-web,就会引入,所以基本不会出现
2.某些pojo类,没有给默认的构造方法。
由于springboot中使用了大量发射机制,反射必须依赖默认的无参构造器。
所以pojo必须给出默认的无参构造器。
三、mybatis的Invalid bound statement (not found)
根据以下可能原因排查:
1.mapper.xml 属性对应的mapper.java 是否 拼写错误,包路径是否正确
2.mapper.xml的id即方法名称,和mapper.java 是否一致
3.mapper.xml定义的方法返回值,和mapper.java 是否一致
4.mapper.xml的SQL语句是否有问题
5.mapper.xml有没有被系统加载起来?