Playframework link MySQL database problem

Source: Internet
Author: User

Scala has been in full swing in America, and some people even say that Scala will be the number one for future languages. The simplicity of code and expression makes many people feel comfortable, and functional and object-oriented programming paradigms allow them to be used in a variety of situations. In addition, it is run on the JVM virtual machine, can perfect call Java, so that many existing technology can continue to retain, reduce maintenance costs. However, Scala's overly complex syntax also makes countless developers headache, know that some people say he is "the most difficult language in the universe", I want to say "hehe", there is nothing, we should have "Mulberry becomes satin" attitude, will not learn slowly, young should be so capricious!!

Okay, so much nonsense, let's get into the topic: How to use playframework to connect to a database. Playframework is a popular development framework for both Java and Scala, but Scala uses a bit more. I'm using Scala here.

Official start:

1, first create a project, the name is: HelloWorld;

It is not recommended to create with the IDE, because now IntelliJ idea and Eclipse do the Scala plugin is not good enough, there will be a lot of problems in development, we recommend the creation of Typesafe Magic Board.

2. Open the BULID.SBT document of the project; add this sentence:

3. Then add the MySQL driver


After adding a new dependency in SBT, be sure to refresh it so that SBT can add something new to it.

4. Next, we open the conf/application.conf file,


Add your own database link information.

Note that if you want to link multiple data sources, you only need to modify "Db.default.driver", and note that "default" in "Db.default.driver" is the name of the data source and can be modified. For example, if you want to create an "ABC" Data source, you can write "Db.abc.driver"

, you write Db.getdatasource ("abc") When you get the data source in your code, and if you want to get the default data source, you can write Db.getdatasource () directly, because:
This is the source code, it is obvious. And through the source, you can know, play with the database link pool is BONECP, this is a great database connection pool, much faster than DBCP and C3P0, but no HIKARICP fast, BONECP Official document is said: "It beats older Connection pools such as c3p0 and DBCP but should now being considered deprecated in favour of HIKARICP "don't know if play will ever use Hikaric P as the database connection pool. Also, play's database connection pool is written dead, unable to expand the database connection pool through application.conf (can be extended by other means, such as self-reliance on the database connection pool in SBT, and then not through the configuration file, using the database itself to obtain a data source implementation of the connection. , personal feeling should be improved, hehe, do not shoot bricks, has been bandaged.

6. Connect pool link Database with play

Create a DB object, write a function in object (or a member variable can also), get the data source, connect to the database.

Note, be sure to "import scala.slick.driver.mysqldriver.simple._" and "import Play.api.Play.current", especially the latter, very easy to forget, because " Db.getdatasource () "will require an implicit parameter (as can be seen in the fifth step of the source map), so even if the" Play.api.Play.current "is written in the IDE, it will not go wrong.

7, according to their own database tables, generate models (or generate database tables based on entity can also). How to generate a slick document that can be viewed

http://slick.typesafe.com/docs/. The use of models, to achieve data deletion and modification.

Of course there are other ways to make the database link, but personally feel that this is the best way. Some of the examples given in Typesafe are based on dbaction, and personal feelings are a bit of a disadvantage, because if you use Dbaction, the controller layer and the model layer are not tightly coupled, not in accordance with the play itself MVC design pattern.

Above.

Playframework link MySQL database problem

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.