具有Join in Liftweb的复杂SQL查询

Chr*_*che 13 sql scala lift

我想知道是否有办法在Liftweb中使用Mapper进行一些复杂的SQL查询.

实际上,我想要做的是使用它们通过1对多关系链接的事实从数据库Employe和Departments执行Join查询.另一个例子也是受欢迎的.

提前致谢.


以下是一些更多细节:假设我有2个表:

Employee : birthday, department ID, salary
Department : department ID, budget, address
Run Code Online (Sandbox Code Playgroud)

现在我想获得一个对象Employee(用Mapper创建)的列表,它有一个salary > 10$和一个department budget < 100$.

当然,我的原始代码比这复杂得多,但我的目标是能够Employee在其自己的表或链接表中具有与标准对应的映射对象列表(即).

小智 4

我查过这个。看起来好像连接是在对象层中完成的。

\n\n

http://exploring.liftweb.net/master/index-8.html推断到您的情况:

\n\n
// Accessing foreign objects  \nclass Employee extends LongKeyedMapper[Employee] with IdPK {  \n ...  \n  object department extends MappedLongForeignKey(this, Department)  \n  def departmentName =  \n    Text("My department is " + (department.obj.map(_.name.is) openOr "Unknown"))  \n}  \n\nclass Department ... {  \n  ...  \n  def entries = Employee.findAll(By(Employee.department, this.id))  \n}  \n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您想要进行多对多映射,您\xe2\x80\x99将需要提供您自己的
\n\xe2\x80\x9cjoin\xe2\x80\x9d类,其中包含两个映射实体的外键。

\n\n
// DepartmentId Entity  \nclass DepartmentId extends LongKeyedMapper[DepartmentId] with IdPK {  \n  def getSingleton = DepartmentId  \n  object name extends MappedString(this,100)  \n}  \nobject DepartmentId extends DepartmentId with LongKeyedMetaMapper[DepartmentId] {  \n  override def fieldOrder = List(name)  \n}  \n
Run Code Online (Sandbox Code Playgroud)\n\n

接下来,我们定义连接实体,如下所示。
\n它\xe2\x80\x99s 是一个 LongKeyedMapper,就像其他实体一样,
\n但它只包含其他实体的外键字段。

\n\n
// Join Entity  \nclass DepartmentIdTag extends LongKeyedMapper[DepartmentIdTag] with IdPK {  \n  def getSingleton = DepartmentIdTag  \n  object departmentid extends MappedLongForeignKey(this,DepartmentId)  \n  object Employee extends MappedLongForeignKey(this,Employee)  \n}  \nobject DepartmentIdTag extends DepartmentIdTag with LongKeyedMetaMapper[DepartmentIdTag] {  \n  def join (departmentid : DepartmentId, tx : Employee) =  \n    this.create.departmentid(departmentid).Employee(tx).save  \n}  \n
Run Code Online (Sandbox Code Playgroud)\n\n

要使用连接实体,您\xe2\x80\x99 需要创建一个新实例并设置
\n适当的外键以指向关联的实例。正如您所看到的,
\nwe\xe2\x80\x99 在我们的 Expense 元对象上定义了一个方便的方法来做到这一点。
\n为了使多对多可作为实体上的字段进行访问,我们可以使用
\nHasManyThrough 特征,如下所示

\n\n
// HasManyThrough for Many-to-Many Relationships  \nclass Employee ... {  \n  object departmentids extends HasManyThrough(this, DepartmentId,   \n    DepartmentIdTag, DepartmentIdTag.departmentid, DepartmentIdTag.Employee)  \n}  \n
Run Code Online (Sandbox Code Playgroud)\n