Skip to content

Instantly share code, notes, and snippets.

@yangbajing
Created July 22, 2012 03:52
Show Gist options
  • Save yangbajing/3158294 to your computer and use it in GitHub Desktop.
Save yangbajing/3158294 to your computer and use it in GitHub Desktop.
liftweb record-mongodb的多数据库连接
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"
}
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) // 最后更新时间
}
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