晚上有空就加了這個功能 -- orm模塊加入外鍵支持:外鍵自動保存,自動加載對象,支持延遲加載。 java
同時作了不少測試,修復了一些BUG. git
測試代碼以下: github
測試用的實體類: sql
public class Child { private int id; public String name; private String email; @Foreign(column = "parentId", foreign = "id") public Parent parent; //@Foreign(column = "parentId", foreign = "id") //public SQLiteLazyLoader<Parent> parent; //@Foreign(column = "parentId", foreign = "isVIP") //public List<Parent> parent; ....... }
public class Parent { private int id; public String name; private String email; private boolean isAdmin; public boolean isVIP; private Date time; private java.sql.Date time2; ......... }
private void testDb() { String temp = ""; Parent parent = new Parent(); parent.name = "測試"; parent.setAdmin(true); parent.setEmail("wyouflf@gmail.com"); /*Parent parent2 = new Parent(); parent2.name = "測試2"; parent2.isVIP = false;*/ try { //DbUtils db = DbUtils.create(this, "/sdcard/", "test"); DbUtils db = DbUtils.create(this.getActivity()); db.configAllowTransaction(true); db.configDebug(true); Child child = new Child(); child.name = "child' name"; //db.saveBindingId(parent); //child.parent = new ForeignLazyLoader<Parent>(Child.class, "parentId", parent.getId()); //child.parent = parent; Parent test = db.findFirst(parent);//經過entity的屬性查找 if (test != null) { child.parent = test; temp += "first parent:" + test + "\n"; resultText.setText(temp); } else { child.parent = parent; } parent.setTime(new Date()); parent.setDate(new java.sql.Date(new Date().getTime())); db.saveBindingId(child);//保存對象關聯數據庫生成的id List<Child> children = db.findAll(Selector.from(Child.class));//.where(WhereBuilder.b("name", "=", "child' name"))); temp += "children size:" + children.size() + "\n"; resultText.setText(temp); if (children.size() > 0) { temp += "last children:" + children.get(children.size() - 1) + "\n"; resultText.setText(temp); } Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); calendar.add(Calendar.HOUR, 3); List<Parent> list = db.findAll( Selector.from(Parent.class) .where("id", "<", 54) .and("time", ">", calendar.getTime()) .orderBy("id") .limit(10)); temp += "find parent size:" + list.size() + "\n"; resultText.setText(temp); if (list.size() > 0) { temp += "last parent:" + list.get(list.size() - 1) + "\n"; resultText.setText(temp); } //parent.name = "hahaha123"; //db.update(parent); Parent entity = db.findById(Parent.class, child.parent.getId()); temp += "find by id:" + entity.toString() + "\n"; resultText.setText(temp); List<DbModel> dbModels = db.findDbModelAll(Selector.from(Parent.class) .groupBy("name") .select("name", "count(name) as count")); temp += "group by result:" + dbModels.get(0).getDataMap() + "\n"; resultText.setText(temp); } catch (DbException e) { temp += "error :" + e.getMessage() + "\n"; resultText.setText(temp); } }
很方便吧^_^ 數據庫
xUtils的所有源碼和更多的介紹在這裏:https://github.com/wyouflf 測試