创建新的spring boot项目后,直接运行就出现了几个问题,希望大家能解答一下

新建一个spring boot项目后,直接运行就报错,其中有两个错误,一是下图这个:

img

二是下图这个

img

下面是我创建时候所用的环境

img

希望大家能帮忙解答一下

参考下这个看看呢 http://t.csdn.cn/MXXnG ,第二个错误没找到 mapper?

第一个是因为你电脑安装的jdk是13的,然后你项目配置的是17的

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7508959
  • 这篇博客也不错, 你可以看下spring boot 项目 更改项目包名后启动报:错误: 找不到或无法加载主类
  • 除此之外, 这篇博客: 在spring boot项目中修改包名要注意的一些问题中的 在spring boot项目中修改包名要注意的一些问题 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

  • 在修改包名时要连带修改以下几个配置,保证下面的地方引用的包名一致

    • 修改包名

    在这里插入图片描述
    如果在这里修改了包名,idea会自动将大部分引用了该包名的地方进行修改,但还是有一些地方没有被修改。比如下面两个地方。

    • application.yml
      在这里插入图片描述

    修改type-aliases-package对应的包名,否则会出现找不到类的异常。如:Caused by: java.lang.ClassNotFoundException: Cannot find class: xxxx

    • DemoApplication启动类中
      在这里插入图片描述

    要修改扫描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.

  • 您还可以看一下 刘忠旭老师的Spring Boot秒杀系统架构实战课程中的 秒杀架构揭秘--如何设计一个高性能秒杀系统?小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    答案:

    根据错误提示可以看出: 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是驱动类。

    1. Cannot determine embedded database driver class for database type NONE.的错误提示,是指在Spring Boot项目中使用了JPA框架,但是没有配置好嵌入式数据库,从而导致无法运行。解决方法是在pom.xml文件中加入对嵌入式数据库的依赖,比如:
    <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文件中加入数据源和嵌入式数据库的配置信息,然后重新运行项目即可。如果还是有问题,可以通过调试的方式逐步查找解决。