Alex Klibisz
Machine learning research intern at ORNL.

Setting Up Play Framework 2.3.6 (2.3.x) with MyBatis

Alex Klibisz, 12/19/2014

The "final product" is in this repo: https://github.com/alexklibisz/play-2.3.6-mybatis-sample

I'm generally a fan of the MyBatis sql mapping setup for projects that are too complex for the ORMs that have become so popular lately. But I was having recently having trouble setting up the newest version of Play Framework - 2.3.6 - to work with MyBatis due to a lack of documentation for MyBatis with this most recent version.

I came across this great tutorial from inoio.de, which goes through the configuration process for Play 2.1. Their final setup can be found here: https://github.com/inoio/play-mybatis-sample.

I built off of this tutorial as well as some searching on stackoverflow to get 2.3.6 up and running with MyBatis. It's my assumption that this process will differ very little for all of the iterations of 2.3, so as to say it should work for 2.3.x.

The main differences between the Inoio tutorial for 2.1 and 2.3.6 configuration:

  1. The MyBatis and Guice Dependencies should be added to build.sbt instead of build.scala.
  2. The mapper XML classpath should be added to build.sbt in a different format - more below.
  3. The Global.java file should be created in the /app directory.

The 2.3.6 Configuration

Here is the configuration in more detail, building from the Inoio tutorial. Again, I give credit to Inoio.de for 99% of this, I simply adjusted a few things for the newer version of Play.

  1. Create new play project: same as the Inoio tutorial.
  2. Add MyBatis Guice Dependency: slight difference.
    The dependency should be added in /build.sbt to the existing libraryDependencies values:
libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "org.mybatis" % "mybatis" % "3.1.1",
  "org.mybatis" % "mybatis-guice" % "3.3",
  javaCore, javaJdbc
)

3) Add mapper xml files to classpath: slight difference.
The two internal lines should be added to build.sbt, the comma should be removed, and there should be a blank line between the two lines:

// Add app folder as resource directory so that mapper xml files are in the classpath
unmanagedResourceDirectories in Compile <+= baseDirectory( _ / "app" )

// but filter out java and html files that would then also be copied to the classpath
excludeFilter in Compile in unmanagedResources := "*.java" || "*.html"

4) Setup Guice + MyBatis in Global.java: same as the Inoio tutorial. The Global.java file can be created in the /app directory. Don't forget to uncomment the db.default.* lines in conf/application.conf.
5) Inject MyBatis class into managed controller: same as the Inoio tutorial.

That's about all there is to it. It's not terribly much, but it was enough to trip me up for several hours until I made the slight changes described here. I hope that helps!

The "final product" is in this repo: https://github.com/alexklibisz/play-2.3.6-mybatis-sample

Related Posts

Comments