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

本文详细介绍了如何使用SpringBoot框架整合MySQL数据库,包括创建数据库、用户授权、配置连接及实现基本的CRUD操作。通过具体实例演示了Controller、Repository的使用方法,以及常见配置问题的解决方案。

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

官方文档

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

mysql 8文档

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

如图:

1411662-20181206143209820-858503244.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

返回:
1411662-20181206160412057-1119285189.png

输入:

http://localhost:8080/demo/all

返回
1411662-20181206160530229-1694446620.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)

通常的解决:

  1. 确认配置文件位于正确的路径: src/main/resource
  2. 确认配置没有拼写错误

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

掌握要点

  • 在Controller中,@RequestMapping的参数path和value的功能是一样的
  • @Controller和@RestController的区别,似乎懂了又似乎没懂
  • 关于Controller中CrudRepository接口的知识,,不曾了解

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人生莫如饥荒

如果对您有帮助,也可以给点赏钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值