我有外部数据库,我想通过Spring中的requestmapping从中查询数据。我已经这样配置了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
问题::当我尝试从UserM iUserController的requestmapping请求用户列表时,我只得到一个空列表。我也尝试通过控制器中的gettingUser方法从数据库中获取一个用户,但是Spring只是返回一个空白的白页而不是json格式的数据。我不知道问题出在哪里。我在数据库的用户表中有3条记录(因此数据库不为空)。
解决方案::查看检查后的答案。
答案 0 :(得分:0)
在application.properties文件中添加以下内容:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
答案 1 :(得分:0)
用户为reserved keyword in mysql。可能会导致错误,您可以尝试更改实体表名称。然后使用您的应用程序保存一个新用户,并使用终结点获取结果数据。