复杂SQL查询通过加入Liftweb
我想知道是否有办法使用Liftweb中的Mapper进行一些复杂的SQL查询.
事实上,我想做的是从数据库“雇员”和“部门”中使用由1对多关系链接的事实执行连接查询. 提前致谢. 这里有一些更多的细节:假设我有2个表: Employee : birthday,department ID,salary Department : department ID,budget,address 现在我想获得一个对象Employee(使用Mapper创建)的列表,其中有一个工资> 10 $和部门预算< 100 $. 当然,我的原始代码比这更复杂,但我的目标是能够拥有一个对应于自己的表格或链接表中的标准的映射对象(即Employee)列表. 解决方法我已经看了这个.它看起来好像在对象层中完成了连接.从http://exploring.liftweb.net/master/index-8.html推出到你的情况: // Accessing foreign objects class Employee extends LongKeyedMapper[Employee] with IdPK { ... object department extends MappedLongForeignKey(this,Department) def departmentName = Text("My department is " + (department.obj.map(_.name.is) openOr "Unknown")) } class Department ... { ... def entries = Employee.findAll(By(Employee.department,this.id)) } 如果你想做多对多的映射,你需要提供你自己的 // DepartmentId Entity class DepartmentId extends LongKeyedMapper[DepartmentId] with IdPK { def getSingleton = DepartmentId object name extends MappedString(this,100) } object DepartmentId extends DepartmentId with LongKeyedMetaMapper[DepartmentId] { override def fieldOrder = List(name) } 接下来,我们定义我们的连接实体,如下所示. // Join Entity class DepartmentIdTag extends LongKeyedMapper[DepartmentIdTag] with IdPK { def getSingleton = DepartmentIdTag object departmentid extends MappedLongForeignKey(this,DepartmentId) object Employee extends MappedLongForeignKey(this,Employee) } object DepartmentIdTag extends DepartmentIdTag with LongKeyedMetaMapper[DepartmentIdTag] { def join (departmentid : DepartmentId,tx : Employee) = this.create.departmentid(departmentid).Employee(tx).save } 要使用连接实体,您需要创建一个新的实例并设置 // HasManyThrough for Many-to-Many Relationships class Employee ... { object departmentids extends HasManyThrough(this,DepartmentId,DepartmentIdTag,DepartmentIdTag.departmentid,DepartmentIdTag.Employee) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |