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

它使用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

转载于:https://www.cnblogs.com/famine/p/10077333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值