Steps needed to use MySQL database with Play frame

2020-01-25 03:54发布

I'm new to Play framework. I'm trying to configure MySQL database as a datasource to be used with Play Ebeans.

Could you some one please explain the steps that are needed to configure MySQL with Play 2.0 framework (like, downloading drivers, adding dependency etc).

10条回答
一纸荒年 Trace。
2楼-- · 2020-01-25 04:11

Got stuck with my MySQL configuration until I found this.

Most important things taken from @biesior answer:

  • Add MySQL connector/J in project's dependency (which is inside /project/Build.scala)
  • After adding dependency, run play dependencies to resolve newly added MySQL connector/J dependency
  • Uncomment default ebean configuration line ebean.default="models.*"
  • Configure MySQL database correctly with proper character encoding 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

It saved my day.

查看更多
The star\"
3楼-- · 2020-01-25 04:12

Play 2.4.3 & MYSQL 5.7.9

I was able to get this working by piecing together bits of info from all the previous answers. So here is another one, that is hopefully more up to date or useful to those with a similar environment.

Environment Details: (this is what I am using)

  • Play 2.4.3 this comes with activator-1.3.7-minimal
  • JDK8, you should already have this as I don't think this version of play works with JDK7
  • MYSQL 5.7.9

appication.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

Note:

  • testSchema in the URL is your database name, if you are using something like MYSQL workbench you will see this listed under the SCHEMAS section. I called mine testSchema. Others may call it something like "myDatabase"
  • The port should be the MYSQL port. Not your application port. I put 3306 in the example because that is usually the default for MYSQL.

build.sbt

Add this line below to your build.sbt file. This should go after the libraryDependencies ++= Seq() declaration.

libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.36"

Finally

  • run this command from your project root -> activator reload
  • restart your appplication
查看更多
可以哭但决不认输i
4楼-- · 2020-01-25 04:24

I had the same issue in latest play framework 2.4.x with activator 1.3.6.

Here are the steps. I Followed the steps described here https://www.playframework.com/documentation/2.4.x/JavaDatabase

Here is my 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.*"]

Here is 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")

Here is the important step.

After configuring above steps, go to command line, stop your activator and run the command activator run. In my situation, I was keep getting the error unable to find mysql drivers. After running the activator run, activator would actually download the MySQL drivers and would resolve the dependencies. That is the important step that resolved my issue.

查看更多
孤傲高冷的网名
5楼-- · 2020-01-25 04:29

I am using play 2.2.0 and I just had to add the following line to build.sbt in project's root folder.

  "mysql" % "mysql-connector-java" % "5.1.27"

And play automatically downloads the driver. It seems Build.scala is not needed for this anymore. Changes to application.conf should be applied as the above commentators have mentioned.

查看更多
登录 后发表回答