gpt4 book ai didi

java - 连接 "computer-database-jpa"Play 2.1 示例应用程序与 MySQL

转载 作者:可可西里 更新时间:2023-11-01 07:02:45 27 4
gpt4 key购买 nike

我正在玩 computer-database-jpa (Java) Play Framework 2.1 示例应用程序。当我在内存数据库中使用 H2 时一切正常,但当我想将应用程序与 MySQL 连接时出现问题。

有些人有同样的问题(Help wanted getting sample app connected to MySQL)但没有解决方案。

我添加了 mysql-connector (Build.scala):

val appDependencies = Seq(
....
"mysql" % "mysql-connector-java" % "5.1.18"
)

并编辑了 application.conf:

db.default.url="jdbc:mysql://password:user@localhost/my-database"
db.default.driver=com.mysql.jdbc.Driver

当我启动应用程序并应用 1.sql(演化脚本)时,出现错误:

You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'sequence company_seq
start with 1000' at line 1 [ERROR:1064, SQLSTATE:42000]

有没有人知道如何解决这个问题?

最佳答案

我找到了解决方案 - https://github.com/opensas/openshift-play2-computerdb .

进化脚本中使用的语法不符合MySQL:

List of changes needed to port computer-database sample app from H2 to mysql

conf/evolutions/default/1.sql

  • added engine=innodb, to enable referential integrity
  • replaced sequences with autoincrement for id fields
  • replaced 'SET REFERENTIAL_INTEGRITY' command with 'SET FOREIGN_KEY_CHECKS'
  • replaced timestamp fields with datetime

conf/evolutions/default/2.sql

  • splitted the computer data between 2.sql and 3.sql file (avoid bug in evolutions running on mysql)

models/Models.scala

  • removed 'nulls last' from Computer.list sql query
  • modified Computer.insert to skip id field (because is auto-assigned by mysql)

因为我玩的是 Java 而不是 Scala 版本,所以我要更改 Company.javaComputer.java 文件。我添加了 @GeneratedValue 注释:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;

在这里你可以找到修改过的进化脚本:https://github.com/opensas/openshift-play2-computerdb/tree/master/conf/evolutions/default

关于java - 连接 "computer-database-jpa"Play 2.1 示例应用程序与 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com