Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
The database is the data storeroom for the entire site. User-submitted data can be stored in the database for future use. Play can communicate with the database through JDBC. I'll tell you about the connection to the play and MySQL databases.
The default action database for the Play 2.* version is via Ebean. Play provides a finder this type of help to implement some simple database queries.
Database Preparation
Increase the database testing in MySQL. Increase the user "player" with the password "player". Add the appropriate permissions for user player.
CREATE DATABASETestingDEFAULT CHARACTER SETUTF8;CREATE USER 'player'@'localhost'Identified by 'player';GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER,CREATE TemporaryTABLES, LOCK TABLES onTesting.* to 'player'@'localhost';
In order to use the MySQL database in play, you need to add the settings in conf/application.conf :
# Database Configurationdb.default.driver=com.mysql.jdbc.driverdb.default.url= "jdbc:mysql://127.0.0.1:3306/ Testing "db.default.user=" player "db.default.password=" player "
# Ebean configurationebean.default= "models.*"
You also need to modify the BUILD.SBT to:
Name: = "Test"version:= "1.0-snapshot"++= 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.
Create a model
Below, I add an entity to the model, which is a person class. Put Models/person.java
Packagemodels;Importjava.util.List;Importjavax.persistence.Entity;Importjavax.persistence.Id;ImportPlay.db.ebean.Model;ImportPlay.db.ebean.Model.Finder; @Entity Public classPersonextendsModel {@Id PublicInteger ID; PublicString name; //Query Public Staticfinder<integer,person> find =NewFinder<integer,person> (Integer.class, person.class); Public StaticList<person>FindAll () {returnFind.all (); } Public StaticPerson findbyname (String name) {returnFind.where (). EQ ("name", name). Findunique (); }}
The person class inherits from the model class and has a @entity annotation that indicates that it is an entity in the model. The entity has two fields, an integer ID, and a string name that is used to hold the data.
@id annotations, the ID will not be empty, not duplicated, and automatically incremented.
The person also has a static field find. Find is the Finder type available on play for database queries. In the static method of FindAll () and Findbyname () in the person class, find is called and the entry is queried in the database.
Play has a evolution module that manages the tables of the database. After writing the Person.java, visit the project. Play then generates a script that creates a table in MySQL. Run the script.
Adding database Entries
Add an action. This action adds an entry to the database:
Public Static Result Addperson () { new person (); New Person (); = "Vamei"; = "Play"; P1.save (); P2.save (); return OK ("Saved");}
The above code is to import models. person.
Will/addperson this URL pair should act. After access, the database will add entries:
practice Adding a form that adds an entry to the database, based on the contents of a single table.
database Queries
I can invoke the query method I just defined in the action FindAll () and Findbyname (), such as adding the Allperson () Action:
Public Static Result allperson () { List<Person> persons = Person.findall (); return OK (views.html.personList.render (Persons));}
The table of the type of person to be queried above is passed to the template views/personlist.scala.html:
@ (personlist:list[models. Person])<!DOCTYPE HTML><HTML> <Body> <ul>@for ( person <- personlist) {<li >@person. Name</Li> } </ul> </Body></HTML>
Modify routes, add the corresponding URL to/allperson, the page is as follows:
In fact, I can also call Person.find directly in the action to make a query statement. This will allow greater query freedom within the action. For example, the above action can be changed to write:
Public Static Result allperson () { List<Person> persons = Person.find.all (); return OK (views.html.personList.render (Persons));}
Summary
Save ()
Finder