如何使用联接查询在 greenDAO 中显示来自多个表的数据



以下是我的DAOgenerator类:

public class MyDaoGenerator {
    public static void main(String args[]) throws Exception {
        Schema schema = new Schema(3, "Dao");
        Entity employee = schema.addEntity("Employee");
        employee.addIdProperty().autoincrement();
        employee.addStringProperty("name");
        employee.addStringProperty("mobile");
        employee.addStringProperty("address");
        employee.addStringProperty("company_id");
        Entity company = schema.addEntity("Company");
        company.addIdProperty().autoincrement();
        company.addStringProperty("comp_name");
        company.addStringProperty("comp_location");
        company.addStringProperty("comp_address");

        new DaoGenerator().generateAll(schema, args[0]);
    }
}

以下是我的 DAO 查询类。这个类有所有的函数,如insertdeleteclear table

public class DaoQueries {
    private static CompanyDao getMyCompanyDao(Context context){
        return ((AppMain)context.getApplicationContext()).getDaoSession().getCompanyDao();
    }

    public static void AddCompany(Context context,Company company){
        getMyCompanyDao(context).insertOrReplace(company);
    }
    public static int companySize(Context context){
        return  getMyCompanyDao(context).loadAll().size();
    }    
    public static List<Company> getCompanies(Context context){
        return getMyCompanyDao(context).loadAll();
    }
    private static EmployeeDao getEmployeeDao(Context context){
        return ((AppMain)context.getApplicationContext()).getDaoSession().getEmployeeDao();
    }
    public static void AddEmployee(Context context,Employee employee){
        getEmployeeDao(context).insertOrReplace(employee);
    }    

    public static List<Employee> getEmployees(Context context){
        return getEmployeeDao(context).loadAll();
    }
    public static void joinedUser(Context context){
        QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
        queryBuilder.join(Company.class,CompanyDao.Properties.Id)
            .where(CompanyDao.Properties.Id.eq(1));
        List<Employee> employees = queryBuilder.list();
        Log.e("DAO","EMPLOYE JOIN "+employees.size());
    }
}

我尝试使用 greenDAO 连接查询,如 dao 文档中所述,用于从两个表中获取行,如下所示:

public static void joinedUser(Context context){
    QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
    queryBuilder.join(Company.class,CompanyDao.Properties.Id)
            .where(CompanyDao.Properties.Id.eq(1));
    List<Employee> employees = queryBuilder.list();
    Log.e("DAO","EMPLOYE JOIN "+employees.size());
}

如果要显示两个表中的数据,可以使用 greenDAO 关系。在您的情况下,您将需要EmployeeCompany之间的 1:1 关系。因此,greenDAO 将创建一个具有 Company 类型成员的 Employee 类,当您加载Employee时,greenDAO 将执行联接查询并加载相关公司。

新的生成器代码:

public class MyDaoGenerator {
    public static void main(String args[]) throws Exception {
        Schema schema = new Schema(3, "Dao");
        Entity company = schema.addEntity("Company");
        company.addIdProperty().autoincrement().notNull();
        company.addStringProperty("comp_name");
        company.addStringProperty("comp_location");
        company.addStringProperty("comp_address");

        Entity employee = schema.addEntity("Employee");
        employee.addIdProperty().autoincrement();
        employee.addStringProperty("name");
        employee.addStringProperty("mobile");
        employee.addStringProperty("address");
        Property companyId = employee.addLongProperty("company_id").getProperty();
        employee.addToOne(company, companyId);
        new DaoGenerator().generateAll(schema, args[0]);
    }
}

让公司员工获得company_id = 1

QueryBuilder<Employee> queryBuilder = getEmployeeDao(context).queryBuilder();
List<Employee> employees = qb.where(EmployeeDao.Properties.Company_id.eq(1)).list();

遍历员工:

foreach(Employee employee : employees){
    Company company = employee.getCompany();
    String companyAddress = company.getComp_address(); 
    //...
}

有关更多信息,请查看 greeenDAO 关系文档。

相关内容

  • 没有找到相关文章

最新更新