联表查询
quick框架通过Join、Find注解实现联表查询,下面我们来看一下他们的用法 ###Find Find注解用于联表查询某一张表里的某一个字段时使用,比如说查询一个用户所在部门的部门名称 ```java @Table(tableName = "T_USER") public class User extends Model<User>{ private String userId; private String userName; private String departmentId; @Find(joinTable="t_department",joinPk="dptId",foreignKey="departmentId",field="dptName") private String departmentName; } ``` ####Find注解包含四个属性:joinTable 、foreignKey 、joinPk、field #####joinTable:关联的表名,上述案例就是关联部门表t_department #####joinPk:关联表的主键,上述案例dptId就是t_department表的主键 #####foreignKey:主表对应关联表的外键,上述案例departmentId就是对应t_department表主键的外键 #####field:查询的关联表字段,上述案例就是查询t_departmen表的dptName字段 整个配置转换成的sql如下: ```sql select user.userId as user_userId, user.userName as user_userName, user.departmentId as user_departmentId, department.dptId as department_dptId, t_department_.dptName as user_departmentName from T_USER as user left join t_department as department on user.departmentId = department.dptId ``` ###Join Join注解用于联表查询某一张表里的数据,且封装成对象 ```java @Table(tableName = "T_USER") public class User extends Model<User>{ private String userId; private String userName; private String departmentId; @Join(foreignKey="departmentId",joinPk="dptId",findList={"dptId","ms"}) private Department department; } ``` ####Join注解包含三个属性:foreignKey 、joinPk 、findList #####foreignKey:主表对应关联表的外键,上述案例departmentId就是对应t_department表主键的外键 #####joinPk:关联表的主键,默认为关联对象的主键,上述案例中若不填写则默认为Department对象的主键 #####fieldList:查询的关联表字段,如若不填写则默认查询所有字段 整个配置转换成的sql如下: ```sql select user.userId as user_userId, user.userName as user2_userName, user.departmentId as user2_departmentId, department.dptId as department_dptId, department.ms as department_ms from T_USER as user left join t_department as department on user.departmentId = department.dptId ```