① 启动报错Failed to configure a DataSource: ‘url’ attribute is not specified and …
根据报错日志分析是在springboot项目启动的时候没有找到database 数据库连接地址,我们知道在spring boot 启动的时候会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。
- 检查pom.xml 项目数据库jar 是否引用;
- 查看**.properties或**.yml 配置文件是否配置数据库链接池;
- 查看spring - datasource - url 配置的地址格式错误需要转义等;
- yml或者properties文件可能没有被扫描到(情况比较少,如果按照标准命名都会被默认扫描);
方案、如果项目不需要数据库相关信息就排除此类的autoconfig
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application, args);
}
}
②Field userMapper in com.example.demo.controller.UserController required a bean of type ‘com.example.demo.mapper.UserMapper’ that could not be found.
检查@MapperScan和@Mapper是否使用
- 可以@Mapper
- 也可以@MapperScan+@Repository
③ Error creating bean with name ‘xxxxxController’: Unsatisfied dependency expressed through field…
可以看到黑体部分的信息,意思是创建helloController这个bean出错了,嵌套异常是userService不满足依赖,容器里面没有com.cloud.xp.manager.service.UserService 的 实例
既然是userService这个对象的问题,那么我们就首先来分析,userService对象是怎么添加到容器里的,首先我想到的就是注解,这个时候我们可以先检查一下UserService这个接口的实现类上又没加上Service注解
④ SpringBoot+MyBatis整合中的坑以及Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required错误
解决方案添加DruidDataSource配置文件
⑤springboot整合mybatis错误 Invalid bound statement (not found): com.yuan.mapper.UserMapper.getUserList
解决方案:
1.把映射文件 放到resources 目录下 结构目录一模一样
2.在pom.xml中导入以下代码
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>