练涛

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

如图:

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

返回:

输入:

http://localhost:8080/demo/all

返回

即成功。

过程中碰到的问题

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

posted @ 2018-12-06 16:16  练涛  阅读(322)  评论(0编辑  收藏  举报