springboot-starter-parent的版本需要和Mybatis对应,否则会出现bean注册不成功Mapper找不到的情况,成功使用的版本如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
</parent>
<dependencies>
<!-- web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- mysql依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- validation依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
当项目启动时报错,那大概率是maven依赖的对应版本出了问题,需要去官方查看对应版本。
当然,注册为bean的Mapper找不到本质是注解出了问题,Mapper没有被成功装入容器,也可以尝试在springboot启动类加上@MapperScan(value = "类名")
/**
* Hello world!
*
*/
@SpringBootApplication
//@ComponentScan(value = "com.atguigu.controller")
//@ComponentScan(value = "com.atguigu.mapper")
//@ComponentScan(value = "com.atguigu.service")
@MapperScan(value = "com.atguigu.mapper")
public class BigEventApplication {
public static void main( String[] args ) {
SpringApplication.run(BigEventApplication.class,args);
}
}