idea开发springboot 的mysql数据库连接问题

今天在家用idea进行springboot开发,前面一些坑相对避免了,但是到数据库这块总是连接不上,报错主要是:

Access denied for user 'root'@'localhost' (using password: NO)

网页上显示错误

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 06 15:18:14 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) ### The error may exist in file [D:\test\target\classes\mapper\User.xml] ### The error may involve com.example.mapper.UserMapper.getUserById ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

我检查我的密码等都是正确的,网上各种查,最后,原来是被idea坑了,

根据idea的提示写出来的数据源信息是:

 
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.data-username=root
spring.datasource.data-password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这个data-username,data-password,driver-class-name不是jdbc数据源的属性字段,所以不识别,当改为如下设置,数据库就连接上了。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 IntelliJ IDEA 中创建一个 Spring Boot 项目并连接数据库通常包括以下几个步骤: ### 1. 创建新的Spring Boot项目 - 打开IntelliJ IDEA,并点击 "Create New Project"。 - 选择 "Spring Initializr" 模板,点击下一步。 - 设置项目的名称、位置以及组ID。你可以将 `group` 设置为你想要的命名空间,如 `com.example`。 - 选择依赖项。为了连接数据库,确保勾选了以下依赖: - `Spring Web` - `Lombok` (可选,用于简化代码) - 数据库相关的依赖,例如 `Spring Data JPA` 和对应的数据库驱动(如 MySQL 或 PostgreSQL)。 ### 2. 添加数据库连接配置 在 `application.properties` 文件中添加数据库连接信息: ```properties # 配置文件路径: src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update ``` 这里的 `your_database_name` 和 `your_password` 分别替换为你实际使用的数据库名和密码。 ### 3. 使用 Thymeleaf 模版引擎 如果你计划使用模板展示数据,可以开启 Thymeleaf 的支持: ```properties # 配置文件路径: src/main/resources/application.properties spring.thymeleaf.enabled=true ``` ### 4. 编写实体类和Repository接口 在 `src/main/java/com/example/projectname/repository` 目录下创建一个接口 `YourEntityNameRepository.java` 并继承自 `JpaRepository`: ```java package com.example.projectname.repository; import com.example.projectname.entity.YourEntityName; import org.springframework.data.jpa.repository.JpaRepository; public interface YourEntityNameRepository extends JpaRepository<YourEntityName, Long> { } ``` 同时,在 `src/main/java/com/example/projectname/entity` 目录下创建实体类 `YourEntityName.java`: ```java package com.example.projectname.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class YourEntityName { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // 其他属性... public YourEntityName() {} // 构造函数、getter、setter... } ``` ### 5. 配置 DataSource 和连接池 对于更复杂的数据库连接管理,可以考虑使用 Spring Cloud Config Server 或外部化配置存储(如:Eureka、Consul 等),以便更精细地控制和更新应用的配置信息。 ### 6. 测试数据库连接 编写单元测试以验证数据库连接是否正常工作: ```java package com.example.projectname.service; import com.example.projectname.entity.YourEntityName; import com.example.projectname.repository.YourEntityNameRepository; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest public class YourEntityServiceIT { @Autowired private YourEntityNameRepository repository; @Test void shouldFindAnEntityByItsId() { long entityId = 1L; // 示例 ID YourEntityName foundEntity = repository.findById(entityId).orElseThrow(); assertThat(foundEntity.getId()).isEqualTo(entityId); } } ``` ### 相关问题: 1. 怎样在Spring Boot中添加MySQL数据库依赖? 2. 如何在IDEA中运行Spring Boot项目并看到日志输出? 3. 在Spring Boot中如何处理SQL注入攻击?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值