1、项目启动时报错,大概意思是@Autowired引用mapper时必须写(required = true)。
原因是:没有在启动类添加
@MapperScan("com.*.mapper")
2、项目启动报错找不到mapper
启动类添加了@MapperScan(“com.*.mapper”)也不生效:
报错信息如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mailConsumer': Unsatisfied dependency expressed through field 'msgLogService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'msgLogServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'msgLogMapper' defined in file [D:\****\target\classes\com\SpringBoot\*\mapper\MsgLogMapper.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException
①首先排除mapper-locations的问题
mybatis-plus.mapper-locations= classpath:mybatis/*Mapper.xml
#mybatis.mapper-locations= classpath:mybatis/*Mapper.xml
②排除第一种,还有第二种情况。原因是缺少以下jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
如果是mybatisplus 是:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
③一些情况记录
参考了这个博主的文章
https://blog.csdn.net/u013234928/article/details/94060733
我以为下面两个jar包只要引用一个就可以,没想到找了好几个小时原因,结果是这两个必须同时引用才不会报上面的错fo(╥﹏╥)o
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
3、项目可以正常启动,但是调找不到mybatisplus自带的方法。用自带方法的时候报错BindingException
org.apache.ibatis.binding.BindingException:
Invalid bound statement (not found): com.SpringBoot.
XX.mapper.XXXMapper.insert
原因依旧是缺jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
总结: 最终mybatisplus所依赖的jar包总结
<!-- mybatis plus【开始】-->
<!-- mybatis plus核心依赖【开始】-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<!-- mybatis plus核心依赖【结束】-->
<!-- 自动生成代码的依赖【开始】-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<!-- 自动生成代码的依赖【结束】-->
<!-- mybatis plus 【结束】-->