zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SRM 551 div2
    HDU_4390 Number Sequence (容斥原理)
    HDU 多校联合第五场
    HDU 多校联合第六场
    POJ 2057 The Lost House (经典树形dp)
    Lucas定理
    HDU 4385 Moving Bricks (状态dp+贪心)
    HDU 多校联合第三场
    当最短路变成二维 _!
    POJ 1848 (一道不错的树形dp)
  • 原文地址:https://www.cnblogs.com/famine/p/10077333.html
Copyright © 2011-2022 走看看