新建一个spring boot项目后,直接运行就报错,其中有两个错误,一是下图这个:
二是下图这个
下面是我创建时候所用的环境
希望大家能帮忙解答一下
参考下这个看看呢 http://t.csdn.cn/MXXnG ,第二个错误没找到 mapper?
第一个是因为你电脑安装的jdk是13的,然后你项目配置的是17的
如果在这里修改了包名,idea会自动将大部分引用了该包名的地方进行修改,但还是有一些地方没有被修改。比如下面两个地方。
修改type-aliases-package对应的包名,否则会出现找不到类的异常。如:Caused by: java.lang.ClassNotFoundException: Cannot find class: xxxx
要修改扫描mapper的路径,否则会出现找不到mapper的异常。如:
Description:Field leavesMapper in cn.zcbigdata.mybitsdemo.service.Impl.LeavesServiceImpl required a bean of type ‘cn.zcbigdata.mybitsdemo.mapper.LeavesMapper’ 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 ‘cn.zcbigdata.mybitsdemo.mapper.LeavesMapper’ in your configuration.
答案:
根据错误提示可以看出: 1. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.的错误提示,是指在Spring Boot项目中使用了JPA框架,但是没有配置好数据源,从而导致无法连接到数据库。解决方法是在application.properties或application.yml文件中加入数据源连接信息,比如:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
其中,url是数据库连接串,username是数据库用户名,password是数据库密码,driver-class-name是驱动类。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
然后在application.properties或application.yml文件中加入嵌入式数据库的配置信息,比如:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
针对文中提到的问题,可以在application.properties或application.yml文件中加入数据源和嵌入式数据库的配置信息,然后重新运行项目即可。如果还是有问题,可以通过调试的方式逐步查找解决。