springboot启动后报错,显示如下:
Field discussPostMapper in com.example.niuke.service.DiscussPostService required a bean of type 'com.example.niuke.dao.DiscussPostMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.niuke.dao.DiscussPostMapper' in your configuration.
也就是说service层要dao层的一个mapper对象,但是找不到
找了网上有人说的一种方法:在启动类中增加MapperScan注解,扫描dao包
@SpringBootApplication
@MapperScan("com.example.niuke.dao")
public class NiukeApplication
{
public static void main(String[] args) {
SpringApplication.run(NiukeApplication.class, args);
}
}
结果出现了新的报错:
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
换另一种全新的方法:在pom.xml文件中查看依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
显然这里引入的是mybatis和mybatis-spring的依赖,将这两个依赖删除,替换成mybatis-springboot-starter依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
然后在maven里面,先clean,然后install,最后刷新