开发者

Getting Started With Lift, Using Databases to Build Dynamic Sites

开发者 https://www.devze.com 2023-02-13 08:13 出处:网络
So I have been looking around the internet for a good explanation of how lift works concerning databases. I have not found anything very helpful yet. What I am looking for is a simple explanation or c

So I have been looking around the internet for a good explanation of how lift works concerning databases. I have not found anything very helpful yet. What I am looking for is a simple explanation or code example that can show how lift connects to its databases to perform transactions and how to use this to create new tables, models or update and edit existing tables.

For example: with django i fairly easily figured out how it generated database tables from model classes and executed updates on them through methods it inherited from the framework.

I am trying to开发者_JAVA技巧 create a simple app at the moment that would have users, information about them, posts on a website, etc.

I am currently reading through the available Lift books and would greatly appreciate more help in learning how to use lift.


Lift configures it's data source in Boot.scala.

if (!DB.jndiJdbcConnAvailable_?) {
  val vendor =
    new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
      Props.get("db.url") openOr
        "jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE",
      Props.get("db.user"), Props.get("db.password"))


  LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)

  DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)
}

It can generate table schemas for you using Schemifier:

 Schemifier.schemify(true, Schemifier.infoF _, User,Post,Tag,PostTags)

For general Lift project, you can just use Lift Mapper as an ORM tool, it's not complete but works for most of the cases.

You can refer to Lift WIKI and Simply Lift(Written by the Author) or Explore Lift. From my perspective, the documents available so far are rather disappointing. It's said the Lift in Action is very well written, but won't come out till this summer, you can read it from MEAP.


In the Exploring Lift book, the PocketChange example contains code showing how to define a User using MetaProtoUser and other features. I would start there for a better understanding of Lift, model and the built-in CRUD and User prototype objects.

http://exploring.liftweb.net/master/index-2.html#toc-Chapter-2

Keep in mind that the 'new' approach to DB integration will be via the Record. This is very much a work in progress, so I wouldn't rush to start learning it.

You can also look at the source for Lift in Action to get some ideas. Here's a link to the travel app built in the first couple chapters https://github.com/timperrett/lift-travel

And to the source code for the entire book. Chapter 10 is the Mapper chapter. https://github.com/timperrett/lift-in-action


The default ORM in Lift is Mapper which gives you among other things a quick path to CRUD functionality for your DB entities. However if you would like a more traditional JPA persistence approach (or rather SPA since entities would in that case be written in scala), i usually find very useful the JPA-like sample application that is part of the Lift distribution. To try it out, assuming maven is installed, just type:

   mvn archetype:generate -DarchetypeRepository=http://scala-tools.org/repo-snapshots -DarchetypeGroupId=net.liftweb -DarchetypeArtifactId=lift-archetype-jpa-basic_2.8.1 -DarchetypeVersion=2.3-SNAPSHOT -DgroupId=org.mycompany.myproject -DartifactId=MyProject -Dversion=1.0

This will create a MyProject Lift project, containing a simple library application with 2 entities (Author and Book) having a one-to-many relationship as well as CRUD snippets showing how you can create and edit such entities in a jdbc compliant database.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号