关联外键查询

QuickDAO提供了强大的外键关联查询,核心方法为joinTable方法.

实体类信息

  1. /**Person类,表示人*/
  2. public class Person{
  3. @Id(strategy = IdStrategy.AutoIncrement)
  4. private long id;
  5. //省略get/set方法
  6. }
  7. /**Order类,订单类*/
  8. public class Order{
  9. @Id(strategy = IdStrategy.AutoIncrement)
  10. private long id;
  11. @ForeignKey(table = Person.class)
  12. private long personId;
  13. private Person person;
  14. //省略get/set方法
  15. }
  16. /**地址类,每个订单有一个地址信息*/
  17. public class Address{
  18. @Id(strategy = IdStrategy.AutoIncrement)
  19. private long id;
  20. private String name;
  21. @ForeignKey(table = Order.class)
  22. private long orderId;
  23. private Order order;
  24. //省略get/set方法
  25. }

主表,子表,父表

  1. //from person
  2. dao.query(Person.class)
  3. //join order on person.id = order.person_id
  4. .joinTable(Order.class,"id","person_id")
  5. //join address on order.id = address.order_id
  6. .joinTable(Address.class,"id","order_id")
  7. .doneSubCondition()
  8. .done();

joinTable方法用于关联表,方法定义如下:

  1. <E> SubCondition<E> joinTable(Class<E> clazz, String primaryField, String joinTableField);

参数含义如下:

  • clazz: 要join的表
  • primaryField: 主表的关联字段
  • joinTableField: 子表的关联字段

其中主表,子表的定义如下:

  • 主表: query方法的参数即为主表
  • 子表: joinTable方法的第一个参数为子表

上述例子中,Person为主表,Order为子表.

此外,子表还可以再次关联表,此时原子表称为父表,父表和子表是相对关系

上述例子中,Order是Address的父表,Address是Order的子表

调用done方法返回主表,doneSubCondition方法返回父表

表别名

默认情况下,主表别名为t,依次关联的子表分别为t1,t2,t3……

关联查询

调用joinTable方法后一样可调用addQuery系列方法添加查询参数.此时的查询是针对join的表所添加的查询

  1. //from person
  2. dao.query(Person.class)
  3. //join order on person.id = order.person_id
  4. .joinTable(Order.class,"id","person_id")
  5. //order.person_id > 0
  6. .addQuery("person_id",">",0)
  7. //join address on order.id = address.order_id
  8. .joinTable(Address.class,"id","order_id")
  9. //address.name = 'quickdao'
  10. .addQuery("name","quickdao")
  11. .doneSubCondition()
  12. .done();

关联查询结果

若要返回关联查询实体,则必须手动调用compositField方法.

  1. //from person
  2. dao.query(Person.class)
  3. //join order on person.id = order.person_id
  4. .joinTable(Order.class,"id","person_id")
  5. //order.person_id > 0
  6. .addQuery("person_id",">",0)
  7. //join address on order.id = address.order_id
  8. .joinTable(Address.class,"id","order_id")
  9. //address.name = 'quickdao'
  10. .addQuery("name","quickdao")
  11. .doneSubCondition()
  12. .done()
  13. //调用以下方法后Order类的Person对象和Address类的Order对象会自动填充
  14. .compositField();

若未调用CompositField方法不会返回关联表属性信息.

默认情况下QuickDAO会根据实体类型自动寻找实体类成员变量中唯一匹配的成员变量.

多属性关联

若实体类中有多个需关联对象,则需要用户手动指定要关联的成员变量名.

  1. public class Person {
  2. private long id;
  3. private long motherId;
  4. private long fatherId;
  5. private Person mother;
  6. private Person father;
  7. }
  1. List<Person> userList = dao.query(Person.class)
  2. //person1.mother_id = person2.id,关联属性到mother变量
  3. .joinTable(Person.class,"mother_id","id","mother")
  4. .done()
  5. //person1.father_id = person2.id,关联属性到father变量
  6. .joinTable(Person.class,"father_id","id","father")
  7. .done()
  8. .compositField()
  9. .execute()
  10. .getList();

设置表别名

  • 设置主表别名
  1. dao.query(Person.class).tableAliasName("p");
  • 设置子表别名
  1. dao.query(Person.class)
  2. .joinTable(Person.class,"motherId","id","mother")
  3. .tableAliasName("motherTable")
  4. .done()
  5. .joinTable(Person.class,"fatherId","id","father")
  6. .tableAliasName("fatherTable")
  7. .done()

关联Condition

joinTable支持将Condition接口作为参数传入

  1. //select id,username,password,type from person as t join (select person_id,count(person_id) count from `order` group by person_id having count(person_id) > 0) t1 on t.id = t1.person_id where t.person_id = 1 order by t.id desc
  2. Condition joinCondition = dao.query(Order.class)
  3. .addColumn("person_id","count(person_id) count")
  4. .groupBy("personId")
  5. .having("count(person_id) > 0");
  6. Response response = dao.query(Person.class)
  7. .joinTable(joinCondition,
  8. "id","person_id")
  9. .done()
  10. .addQuery("person_id",1)
  11. .orderByDesc("id")
  12. .execute();

cross join

QuickDAO支持crossJoin方法

  1. //select id,username,password,type from person as t cross join order as t1 where t.id =t1.person_id and t1.order_no > 0
  2. List<Person> personList = dao.query(Person.class)
  3. .crossJoinTable(Order.class)
  4. .addQuery("orderNo",">",0)
  5. .done()
  6. .addRawQuery("t.id = t1.person_id")
  7. .execute()
  8. .getList();