Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using Ebean (Java) with MySQL


Play Framework 2.x includes an object-relational mapping tool called Ebean for Java-based Play applications. To be able to use Ebean, ensure that Ebean and a suitable MySQL driver are declared as project dependencies in foo_java/build.sbt.

For this recipe, we will be utilizing Ebean with database evolutions. Play Framework 2.x gives developers a way to manage database migrations. Database migrations are useful for tracking schema changes during the course of application development. Database evolutions are enabled by default but can be disabled in conf/application.conf with the following settings:

evolutionplugin=disabled

Evolution scripts are stored in the conf/evolutions/default/ directory. For more information regarding database evolutions, please refer to Play's online documentation at

https://www.playframework.com/documentation/2.3.x/Evolutions.

How to do it...

You need to perform the following steps to utilize Ebean:

  1. Add the Ebean dependency in build.sbt:

        libraryDependencies ++= Seq(
            javaJdbc,   javaEbean,
            "mysql" % "mysql-connector-java" % "5.1.28"
        )
  2. Ensure that Ebean and MySQL are configured properly in conf/application.conf:

        db.default.driver=com.mysql.jdbc.Driver
        db.default.url="jdbc:mysql://<YOUR_MYSQL_HOST>/<YOUR_DB>"
        db.default.user=<YOUR_USER>
        db.default.password=<YOUR_PASSWORD>
    
        ebean.default="models.*"
  3. For the next recipes, we need to create our product table in our MySQL database. Create our first database evolution file 1.sql in conf/evolutions/default and add the following SQL statements:

      # --- !Ups
        CREATE TABLE Products (
          id INT NOT NULL AUTO_INCREMENT,
          name VARCHAR(100) NOT NULL,
          PRIMARY KEY (id)
        );
    
      # --- !Downs
        DROP TABLE Products;    
  4. The next step is to create the Ebean model for our entity Product:

        package models;
    
        import java.util.*;
        import javax.persistence.*;
        import play.db.ebean.*;
        import play.data.format.*;
        import play.data.validation.*;
    
        @Entity
       @Table(name = "Products")
        public class Product extends Model {
          
        @Id
        public Long id;
      
        @Column
        @Constraints.Required
        public String name;
    
        public static Finder<Long, Product> find = new Finder<Long, Product>(
          Long.class, Product.class
        );
    
        public Long getId() {
          return id;
        }
        public void setId(Long id) {
          this.id = id;
        }
        public String getName() {
          return name;
        }
        public void setName(String name) {
          this.name = name;
        }
    }

The following displays various database-oriented operations using Ebean.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Creating a record

The following code snippet will create a new record:

Product product = new Product();
      product.name = "Apple iPhone";
      product.save();

Updating a record

The following code snippet will update a record:

   Product forUpdate = Product.find.ref(1L);
      forUpdate.name = "Apple iPhone 6";
      forUpdate.update();Deleting a record:
  Product.find.ref(1L).delete();

Querying a record

The following code snippet will query a record:

  Product p = Product.find.byId(1L);

Retrieving a record

The following code snippet will retrieve a record:

List<Product> products = Product.find.all();