Play frame (v) database

Source: Internet
Author: User
Tags character set integer mysql create database mysql database

The database is the data storeroom for the entire site. The data submitted by the user can be stored in the database for future use. Play can communicate through JDBC and database. I'll talk about the connection between play and MySQL database.

Play 2.* version of the default operation of the database by means of the Ebean. Play provides a finder for this type of help, and can implement some simple database queries.

Database Preparation

Add database testing to MySQL. Increase the user "player" and the password is "player". Increase the appropriate permissions for user player.

CREATE DATABASE testing DEFAULT CHARACTER SET UTF8;   
CREATE USER ' player ' @ ' localhost ' identified by ' Player ';   
GRANT SELECT, INSERT, UPDATE, DELETE, create, DROP, INDEX, ALTER, create temporary tables, LOCK tables on testing.* to ' pl Ayer ' @ ' localhost ';

In order to use the MySQL database in play, you need to add the setting in conf/application.conf:

# Database Configuration   
       
db.default.driver=com.mysql.jdbc.driver   
db.default.url= "jdbc:mysql:// 127.0.0.1:3306/testing "
db.default.user=" player "
db.default.password=" Player "
       
       
# Ebean Configuration   
ebean.default= "models.*"

You also need to modify BUILD.SBT to:

Name: = "Test"
       
version: = "1.0-snapshot"
       
librarydependencies ++= Seq (   
  javajdbc,   
  Javaebean,   
  cache ,   
  "MySQL"% "Mysql-connector-java"% "5.1.18"
) play   
       
. Project.playjavasettings

When the above changes are complete, use play run to run the server.

Creating a Model

Next, I add an entity (entity) to the model, that is, a person class. Put Models/person.java

Package models;   
       
Import java.util.List;   
       
Import javax.persistence.Entity;   
Import Javax.persistence.Id;   
       
Import Play.db.ebean.Model;   
Import Play.db.ebean.Model.Finder;   
       
@Entity public
class Person extends Model {   
    @Id the public
    Integer Id;   
    public String name;   
       
    Query public   
    static finder<integer,person> find =    
            new Finder<integer,person> (Integer.class, Person.class);   
           
    public static list<person> FindAll () {return   
        find.all ();   
    }   
           
    public static person Findbyname (String name) {return   
        find.where (). EQ ("name", name). Findunique ();   
    }   

The person class inherits from the model class and has a @entity annotation to indicate that it is an entity in the model. The entity has two fields, the ID of the integer and the name of the string, which is used to save the data.

@id Note, the ID will not be empty, not duplicated, and automatically incremented.

The person also has a static field find. Find is the Finder type provided by play for database queries. In the static method of FindAll () and Findbyname () in the person class, find is called to query the entry in the database.

Play has the Evolution module, manages the database the table. After writing the Person.java, visit the project. Play then generates a script that creates a table in MySQL. Run the script.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.