Created
July 22, 2012 03:52
-
-
Save yangbajing/3158294 to your computer and use it in GitHub Desktop.
liftweb record-mongodb的多数据库连接
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package bootstrap.liftweb | |
| import net.liftweb.common.{ Full } | |
| import net.liftweb.http.InternalServerErrorResponse | |
| import net.liftweb.http.{ XHtmlInHtml5OutProperties, LiftRules, Req } | |
| import net.liftweb.mongodb.{ MongoDB, DefaultMongoIdentifier, MongoAddress, MongoHost, MongoIdentifier } | |
| import net.liftweb.sitemap.{ SiteMap, Menu, ** } | |
| import cst.lib.CstProps | |
| class Boot extends me.yangbajing.log.LoggableLazy { | |
| def boot { | |
| LiftRules.addToPackages("learn") | |
| LiftRules.htmlProperties.default.set((r: Req) => new XHtmlInHtml5OutProperties(r.userAgent)) | |
| MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), CstProps.defaultDbname)) | |
| MongoDB.defineDb(NNNNNMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "nnnnn")) | |
| } | |
| } | |
| // 定义的别一个数据库连接 | |
| object NNNNNMongoIdentifier extends MongoIdentifier { | |
| def jndiName = "nnnnn" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package learn.model | |
| import net.liftweb.record.field._ | |
| import net.liftweb.mongodb.record._ | |
| import net.liftweb.mongodb.record.field._ | |
| import me.yangbajing._ | |
| import cst.model._ | |
| import net.liftweb.mongodb._ | |
| import net.liftweb.http.StringField | |
| object MTestRecord { | |
| def apply() = new MTestRecord(DefaultMongoIdentifier) | |
| def apply(identifier: MongoIdentifier) = new MTestRecord(identifier) | |
| } | |
| class MTestRecord(identifier: MongoIdentifier) | |
| extends MongoRecord[MTestRecord] | |
| with MongoId[MTestRecord] | |
| with MongoMetaRecord[MTestRecord] { | |
| def meta = this | |
| override val collectionName = "m_test" | |
| override def mongoIdentifier = identifier | |
| object createdAt extends DateTimeField(this) // 创建时间 | |
| object updatedAt extends DateTimeField(this) // 最后更新时间 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package learn.model | |
| import bootstrap.liftweb.NNNNNMongoIdentifier | |
| import net.liftweb.common._ | |
| import org.scalatest.FlatSpec | |
| import org.scalatest.BeforeAndAfter | |
| import org.scalatest.matchers.ShouldMatchers | |
| import cst.model._ | |
| class RecordTest extends FlatSpec with ShouldMatchers { | |
| new bootstrap.liftweb.Boot().boot | |
| it should ("Test TestRecord") in { | |
| val defaultDoc = MTestRecord().createRecord | |
| println("default doc: " + defaultDoc) | |
| val nnnnnDoc = MTestRecord(NNNNNMongoIdentifier).createRecord | |
| println("nnnnn doc: " + nnnnnDoc) | |
| defaultDoc.save | |
| nnnnnDoc.save | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment