xUtils 更新--orm模块加入外键支持:外键自动保存,自动加载对象,支持延迟加载...

      

      晚上有空就加了这个功能 -- orm模块加入外键支持:外键自动保存,自动加载对象,支持延迟加载。

      同时做了很多测试,修复了一些BUG.

测试代码如下:

测试用的实体类:

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("[email protected]");

        /*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


你可能感兴趣的:(android,orm,Afinal,xutils)