簡體   English   中英

如何使用聯接查詢在greenDAO中顯示來自多個表的數據?

[英]How to use join query for displaying data from multiple tables in 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());

    }
}

我嘗試使用dao文檔中提到的greenDAO連接查詢,以從兩個表中獲取行,如下所示:

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關系文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM