好的,@infused 的建议看起来很棒。在ActiveJDBC中也是如此
首先定义模型:
public class Customer extends Model {}
public class Merchant extends Model {}
public class Transaction extends Model {}
二、建表:
顾客:
id | first_name | last_name | etc
商家:
id | name | address1 | etc
交易:
id | customer_id | merchant_id | tag | etc
查找客户的所有交易:
customer.getAll(Transaction.class);
要查找商家的所有交易:
merchant.getAll(Transaction.class);
查找客户 #1 对商家 #25 的所有交易
customer = Customer.findById(1);
customer.get(Transaction.class, "merchant_id = ?", 25);
为客户 #8 查找商家 #1 的所有交易
merchant = Merchant.findById(1);
merchant.get(Transaction.class, "customer_id = ?", 8);
通过标签查找所有交易(假设标签是一个字符串字段):
transactions = Transaction.where("tag = ?", "best-seller");
查找所有交易标记为“畅销书”的商家:
transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
//iterate over transactions, and get a merchants:
transactions.get(i).getAll(Merchant.class)
这种方法将只运行 2 个 SQL 查询,而且速度非常快。
我希望这有帮助