springdata和mysql_Spring Boot使用Spring Data JPA访问MySQL数据库

这篇博客介绍了如何使用SpringDataJPA访问MySQL数据库,包括创建数据库、用户授权以及配置application.properties文件。遇到的问题是无法确定内嵌数据库驱动类,解决办法是检查配置文件和拼写。通过访问特定URL可以验证应用是否成功运行。
摘要由CSDN通过智能技术生成

它使用Spring Data JPA来访问数据库,但这只是众多可能选择中的一种(例如,您可以使用普通的Spring JDBC)。

mysql建立数据库,添加用户,并且授权

mysql> create database db_example; -- Create the new database

mysql> create user 'springuser' identified by 'ThePassword'; -- Creates the user

mysql> grant all on db_example.* to 'springuser'; -- Gives all the privileges to the new user on the newly created database

如图:

708f0de49ec608c7ea91634fc0cef214.png

application.propertier配置文件:

spring.jpa.hibernate.ddl-auto=create

spring.datasource.url=jdbc:mysql://localhost:3306/db_springboot_mysql

spring.datasource.username=springboot_user

spring.datasource.password=pwd_springboot

还可以用server.port=8080来改变端口号。

验证

浏览器输入

http://localhost:8080/demo/add?name=First&email=233333@gmail.com

返回:

aee0f0eb6169c714c0e61b09b10ec0b9.png

输入:

http://localhost:8080/demo/all

返回

98047b3e5d214d009e9fd5f20289032e.png

即成功。

过程中碰到的问题

Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active)

通常的解决:

确认配置文件位于正确的路径: src/main/resource

确认配置没有拼写错误

关于这类问题还可以看前辈的这篇文章

掌握要点

在Controller中,@RequestMapping的参数path和value的功能是一样的

@Controller和@RestController的区别,似乎懂了又似乎没懂

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值