【发布时间】:2019-07-30 17:10:08
【问题描述】:
我有外部数据库,我想通过 Spring 中的请求映射查询数据。我已经像这样配置了我的 application.properties 文件:
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
# Connection url for the database "testdb"
# (I made up the hosting address for security reasons)
spring.datasource.url=jdbc:mysql://23.119.144.321:3306/testdb
# Username and password
spring.datasource.username=root
spring.datasource.password=root
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
问题:当我尝试从 requestmapping i UserController 请求用户列表时,我只得到一个空列表。我也尝试通过控制器中的gettingUser方法从数据库中获取一个用户,但Spring只返回一个空白页而不是json格式的数据。我不知道问题出在哪里。我在 db 中的 User 表中有 3 条记录(因此 db 不为空)。
解决方案:查看已检查的答案。
【问题讨论】:
-
你应该在控制台日志中有一些例外?
-
不,没有错误,也没有例外,只是关于 ASTQueryTranslatorFactory 和 DispatcherServlet 的 INFO 消息。
-
您确定连接到正确的架构吗?因为由于您拥有的 ddl 配置,Spring 可能会连接到另一个模式并从头开始创建表。
-
如何检查我是否连接到正确的架构?
-
首先,确保您的日志显示您在属性中定义的连接字符串,然后使用相同的连接详细信息通过命令行或 mysql 工作台登录数据库。列出并浏览在该实例上创建的所有数据库并浏览列出所有可用表。您应该只在 testdb 上找到您的实体的表。