接下来我们更新一下boot.scala.在其中添加以下代码
val sitemap=List(
Menu("Home")/"index",
Menu("Auctions")/"auctions"
)
LiftRules.setSiteMap(SiteMap(sitemap:_*))然后我们加入数据库连接所必要的代码
在Project.scala中,添加
val mapper = "net.liftweb" %% "lift-mapper" % liftVersion
val textile = "net.liftweb" %% "lift-textile" % liftVersion % "compile->default"
然后对项目执行
reload
update耐心等待mapper包下载安装
在boot中添加以下引用
import net.liftweb.util._
import net.liftweb.util.Helpers._导入mapper需要的模块import net.liftweb.mapper.{DB,Schemifier,DefaultConnectionIdentifier,StandardDBVendor,MapperRules}
引入数据库
// set the JNDI name that we'll be using
DefaultConnectionIdentifier.jndiName = "jdbc/liftinaction"
// handle JNDI not being avalible
if (!DB.jndiJdbcConnAvailable_?){
//logger.warn("No JNDI configured - making a direct application connection")
DB.defineConnectionManager(DefaultConnectionIdentifier, Database)
// make sure cyote unloads database connections before shutting down
LiftRules.unloadHooks.append(() => Database.closeAllConnections_!())
}
// automatically create the tables
Schemifier.schemify(true,Schemifier.infoF _,Supplier,Book,Author,AuthorBooks,Publisher)
// setup the load pattern
S.addAround(DB.buildLoanWrapper)其中Supplier,Book,Author,AuthorBooks,Publisher是我们接下来要建立的实体对象
增加一个数据库声明,如果本地不存在,则会建立一个临时的h2数据库
object Database extends StandardDBVendor(
Props.get("db.class").openOr("org.h2.Driver"),
Props.get("db.url").openOr("jdbc:h2:database/temp"),
Props.get("db.user"),
Props.get("db.pass"))使用>lift create mapper XXX来建立上述实体对象
class Book extends LongKeyedMapper[Book]
with IdPK
with CreatedUpdated
with ManyToMany {
def getSingleton = Book
object title extends MappedString(this,255)
object blurb extends MappedText(this)
object publishedOn extends MappedDate(this)
object publisher extends LongMappedMapper(this,Publisher)
object authors extends MappedManyToMany(
AuthorBooks,AuthorBooks.book,AuthorBooks.author,Author
)
}
object Book extends Book with LongKeyedMetaMapper[Book] {
override def dbTableName="books"
}class AuthorBooks extends LongKeyedMapper[AuthorBooks] with IdPK {
def getSingleton = AuthorBooks
object author extends LongMappedMapper(this,Author)
object book extends LongMappedMapper(this,Book)
}
object AuthorBooks extends AuthorBooks with LongKeyedMetaMapper[AuthorBooks]class Publisher extends LongKeyedMapper[Publisher]
with IdPK
with CreatedUpdated
with OneToMany[Long,Publisher]{
def getSingleton = Publisher
object name extends MappedString(this,255)
object description extends MappedText(this)
object books extends MappedOneToMany(Book,Book.publisher)
}
object Publisher extends Publisher with LongKeyedMetaMapper[Publisher] {
override def dbTableName = "publishers"
}class Author extends LongKeyedMapper[Author]
with IdPK
with CreatedUpdated
with ManyToMany {
def getSingleton = Author
object title extends MappedEnum(this,Titles)
object firstName extends MappedString(this,255)
object lastName extends MappedText(this)
object email extends MappedEmail(this,150)
object books extends MappedManyToMany(
AuthorBooks,AuthorBooks.author,
AuthorBooks.book,Book
)
}
object Author extends Author with LongKeyedMetaMapper[Author] {
override def dbTableName="authors"
}
object Titles extends Enumeration{
val Mr,Mrs,Miss,Dr = Value
}
本文详细介绍了如何在Lift Web框架中更新boot.scala文件,添加数据库连接代码,设置数据库映射和实体对象创建过程。通过一系列步骤确保项目的数据库连接正确配置,并实现数据库操作的自动化。
2309

被折叠的 条评论
为什么被折叠?



