Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 4: Play

Matthew Tyson | March 18, 2016
A closer look at Java's original, ultra-lightweight web development stack

Although Play is known as a microframework, really it's a total web development stack, including a build tool that closely integrates with application provisions like view and persistence support. You installed Play in Part 1, and got a quick tour of its programming basics. Now we'll extend that basic application to explore some exciting capabilities of Play.

As with the other deep dives, we'll begin by adding persistence to the basic app from Part 1. 

Connecting Play to a database

Listing 1 has the schema for the Play demo app. Note that we'll be using a MariaDB instance running on localhost.

Listing 1. DDL for the Play example app


create table groups (name varchar (200), id int not null auto_increment primary key);

Our first step is to tell Play how to connect to your database using MySQL. Open up application.conf, and insert the connection properties as I have done in Listing 2. You'll notice I left some comments in there. You'll also see the comments in your configuration file, because Play includes them as an example of connecting to the H2 database. I've used root and password for this example, but you shouldn't do that in a real app. You can use a different RDBMS if you prefer; just check the documentation for the specific connection parameters you'll need.

Listing 2. Database settings for application.conf


#db.default.driver=org.h2.Driver
#db.default.url="jdbc:h2:mem:play"
#db.default.username=sa
#db.default.password=""
db.default.driver=com.mysql.jdbc.Driver
db.default.url="mysql://root:password@localhost/play_app"

So far so good -- Play knows how to communicate with the MariaDB instance. Now let's consider our options for getting the data in and out from the app. Ninja had JPA/Hibernate bundled in, so we used that. For Spark we mixed a custom cocktail of DBUtils and Boon JSON. For Play we'll use EBean, a lightweight ORM that is similar in spirit to Hibernate but with lighter configuration. (Note that Play is capable of running a full JPA provider if you need that.)

It's your choice whether to start with configuration or code. Personally, I decide using some combination of celestial alignment and barometric pressure. For this project we'll start with the code, by creating a model class. In Part 1 we created a Group class in order to model a musical group. Now we'll extend the Group class into an Entity bean. Listing 3 shows the modified class.

Listing 3. An EBean-enabled model class


package models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import com.avaje.ebean.Model;

@Entity
@Table(name="groups")
public class Group extends Model {
	@Id
	@GeneratedValue
	String id;
	String name;
	
	public Group(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

The Group class in Listing 3 is in the models package, which is in the /app/models path. Note that this path lives at the same level as the /controllers and /views directories. Figure 1 shows the file's placement in Eclipse.

 

1  2  3  4  5  6  7  Next Page 

Sign up for CIO Asia eNewsletters.