我对Play框架还不熟悉。 我正在尝试将MySQL数据库配置为Play Ebeans可用的数据源。
请问是否有人能解释一下如何在Play 2.0框架中配置MySQL(例如,下载驱动程序,添加依赖项等)的步骤。
我对Play框架还不熟悉。 我正在尝试将MySQL数据库配置为Play Ebeans可用的数据源。
请问是否有人能解释一下如何在Play 2.0框架中配置MySQL(例如,下载驱动程序,添加依赖项等)的步骤。
请看来自Play文档的此页面。 它说:
除用于开发模式的h2内存数据库之外,Play 2.0不提供任何数据库驱动程序。 因此,为了在生产中部署应用,您必须将您的数据库驱动程序添加为应用程序依赖项。
例如,如果使用MySQL5,则需要添加连接器的依赖项:
val appDependencies = Seq(
// Add your project dependencies here,
...
"mysql" % "mysql-connector-java" % "5.1.18"
...
)
SBT会为你下载驱动程序。你还应该查看管理依赖关系的部分。
要连接到MySQL,您还需要在application.conf
中更改一些设置:
db.default.driver=com.mysql.jdbc.Driver
db.default.url="mysql://root:secret@localhost/myDatabase"
正如Carsten所述,可以从文档中获取,以下是摘要:
确保在/project/Build.scala
中配置了相关依赖项。
val appDependencies = Seq(
// Add your project dependencies here,
"mysql" % "mysql-connector-java" % "5.1.18"
)
在/conf/application.conf
中添加适当的数据库配置(替换默认的H2配置):
(不要从URL中删除编码):
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/your_db_name?characterEncoding=UTF-8"
db.default.user=your_login
db.default.password=your_pass
在同一个文件中查找并确保此行没有被注释:
ebean.default="models.*"
好了,重新启动你的应用程序(或以开发模式运行),然后它将创建一个DDL并要求你应用它。
localhost
替换为127.0.0.1
。具体来说,使用来自MacPorts的MariaDB(一个无Oracle的MySQL替代品)时,我不得不在my.cnf
中注释掉skip-networking,并使用IP地址而不是localhost
以使Play成功连接。 - ivvi我正在使用Play 2.2.0,我只需要在项目根目录下的build.sbt中添加以下行。
"mysql" % "mysql-connector-java" % "5.1.27"
自动播放程序将下载驱动程序。似乎不再需要 Build.scala。如上评论者所述,应将更改应用于 application.conf。
libraryDependencies ++= Seq(jdbc,anorm,cache,"mysql" % "mysql-connector-java" % "5.1.27")
。这行代码将帮助您添加所需的依赖项。 - Dao LamFor Play 2.3.3, in the build.sbt file add the mysql specific line in the libraryDependencies:
libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.27"
)
In the /conf/application.conf file add this:
db.myotherdb.driver = com.mysql.jdbc.Driver
db.myotherdb.url = "jdbc:mysql://xxx.xxx.xxx.xxx/NameOfOtherDB?characterEncoding=UTF-8"
db.myotherdb.user = MyOtherDbUSername
db.myotherdb.password = MyOtherDbPass
You can replace "myotherdb" by "default" in case you want to use the default database or with any other name that you want to use. Replace "xxx.xxx.xxx.xxx" with the IP address of the server where your database is located (in case of an external database) or localhost (or 127.0.0.1) for local database. Replace "NameOfOtherDB" with the name of the database that you want to use, the "MyOtherDbUSername" with your database username and "MyOtherDbPass" with your database password.
Inside your Model (/app/models/MyModel.scala) add this:
val connection = DB.getConnection("myotherdb")
Create the statement, the query and execute it:
val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val query = "SELECT * FROM myTableName"
val resultset = statement.executeQuery(query)
Then you can continue with whatever you want to do with the retrieved data. For example:
while (resultset.next()) {
resultset.getString("columnName")
}
Where "columnName" is the name of the DB table column/field that you want to retrieve.
在我找到这个之前,我被MySQL的配置问题困扰了。
以下是@biesior答案中最重要的内容:
/project/Build.scala
内)play dependencies
以解决新添加的MySQL connector/J依赖项。ebean.default="models.*"
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://www.sample.com:3306/test?characterEncoding=UTF-8"
db.default.user=playuser
db.default.pass=playuser
这使我的一天得救了。
我从之前的答案中汲取信息并终于使这一切成功了。现在,我分享一下我的经验,希望对那些拥有类似环境的人有所帮助。
环境详情: (我使用的环境)
application.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/testSchema?characterEncoding=UTF-8"
db.default.user=yourDBUserName
db.default.password=yourDBUserPass
注意:
3306
,因为这通常是MYSQL的默认端口。build.sbt
请将以下行添加到您的build.sbt文件中。这应该在libraryDependencies ++= Seq()
声明之后。
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.36"
最后
activator reload
针对 play 2.3.1,请按照以下步骤操作。
1)将MySQL连接器/J添加到项目依赖中(位于 /project/build.sbt 内部)。
libraryDependencies ++= Seq( javaJdbc, javaEbean, "mysql" % "mysql-connector-java" % "5.1.29"
2) 取消注释默认的ebean配置行 ebean.default="models.*"
3) 正确配置MySQL数据库并使用适当的字符编码
db.default.driver=com.mysql.jdbc.Driver //this is com. and not org.
db.default.url="jdbc:mysql://127.0.0.1/test?characterEncoding=UTF-8"
db.default.user=playuser
db.default.pass=playuser
4) 最重要的是在控制台中运行reload命令。
sudo apt-get install mysql-client; sudo apt-get install libmysql-java
进行了安装,设置了export CLASSPATH=/usr/share/java/mysql-connector-java.jar
,并将其添加到了/etc/environment
(如https://help.ubuntu.com/community/JDBCAndMySQL所述)。但它仍然无法工作。 - Nick针对使用SBT玩Java项目
在"build.sbt"中将libraryDependency更改为以下形式
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
javaWs,
"mysql" % "mysql-connector-java" % "5.1.27"
)
使用 "activator run" 命令来运行您的项目。
Play 将会下载所需的 jdbc 连接器。
application.conf
。
# MySQL DB Configuration
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://{hostname or ipaddres}/{db name}?characterEncoding=UTF-8"
db.default.username=username // Note that user id deprecated, instead use username. Though that is not a major issue
db.default.password="password"
# JPA Configurations
jpa.default=defaultPersistenceUnit
PlayKeys.externalizeResources = false
# JavaEbean configuration
ebean.default = ["models.*"]
build.sbt
。
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
evolutions,
"mysql" % "mysql-connector-java" % "5.1.27"
)
plugins.sbt
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3")
// Web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
// Play enhancer - this automatically generates getters/setters for public fields
// and rewrites accessors of these fields to use the getters/setters. Remove this
// plugin if you prefer not to have this feature, or disable on a per project
// basis using disablePlugins(PlayEnhancer) in your build.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")
// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using
// enablePlugins(SbtEbean). Note, uncommenting this line will automatically bring in
// Play enhancer, regardless of whether the line above is commented out or not.
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
这是重要的步骤。
配置完以上步骤后,进入命令行,停止您的activator并运行命令
activator run
。在我的情况下,我一直收到错误信息无法找到mysql驱动程序
。运行activator run
后,activator会下载MySQL驱动程序并解决依赖关系。这是解决我的问题的重要步骤。
如果您需要此功能,请将以下行添加到您的依赖项中:
"mysql" % "mysql-connector-java" % "5.1.36"
import java.sql.Connection
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://localhost/world"
val username = "root"
val password = "root"
var connection: Connection = null
try {
// make the connection
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
// create the statement, and run the select query
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT id , name FROM bar")
val sql: SqlQuery = SQL("select * from products order by name asc")
while (resultSet.next()) {
val id = resultSet.getString("id")
val name = resultSet.getString("name")
println(id, name)
}
} catch {
case e: Exception => println("exception caught: " + e);
}
connection.close()