Greendao

github代码地址

DbUtils.getDb(this).insert(new User(null, "yym", 1, new Date(), 1));
User user = DbUtils.getDb(this).get(1L,User.class);

IDB.class

package com.yym.greendaoexample.db;

import java.util.List;

public interface IDb {

     int insert(E e);

     boolean delete(E e);

     boolean update(E e);

     List getAll(Class clazz);

     E get(Long id, Class clazz);
}

GreenDaoDbImpl.class

package com.yym.greendaoexample.db;

import android.content.Context;
import com.yym.greendaoexample.db.greendao.DaoMaster;
import com.yym.greendaoexample.db.greendao.DaoSession;
import java.util.List;

public class GreenDaoDbImpl implements IDb {
    private static final String TAG = "GreenDaoDbImpl";

    private DaoSession daoSession;

    public GreenDaoDbImpl(Context context) {
        DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "greendaoexample.db", null);
        DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase());
        daoSession = daoMaster.newSession();
    }

    @Override
    public  int insert(E e) {
        return Long.valueOf(daoSession.insert(e)).intValue();
    }

    @Override
    public  boolean delete(E e) {
        daoSession.delete(e);
        return true;
    }

    @Override
    public  boolean update(E e) {
        daoSession.update(e);
        return true;
    }

    @Override
    public  List getAll(Class clazz) {
        return daoSession.loadAll(clazz);
    }

    @Override
    public  E get(Long id,Class clazz) {
        return daoSession.load(clazz,id);
    }
}

DbUtils.class

package com.yym.greendaoexample.db;

import android.content.Context;

public class DbUtils{
    private static final String TAG = "Db";

    private static IDb mDb;

    private DbUtils() {}

    public static IDb getDb(Context context){
        if (mDb == null){
            mDb = new GreenDaoDbImpl(context.getApplicationContext());
        }
        return mDb;
    }
}

AGenerator.class

package com.yym.greendaoexample.db.greendao;

import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Schema;

public class AGenerator {
    private static final String TAG = "Generator";

    public static void main(String[] args) {
        Schema schema = new Schema(1,"com.yym.greendaoexample.db.greendao");

        addUserEntity(schema);
        addMessageEntity(schema);

        try {
            new DaoGenerator().generateAll(schema,"./app/src/main/java/");
        } catch (Exception e) {
            System.out.print(e);
        }
    }

    private static void addUserEntity(Schema schema) {
        Entity entity = schema.addEntity("User");
        entity.addIdProperty().autoincrement();
        entity.addStringProperty("name");
        entity.addIntProperty("gender");
        entity.addDateProperty("birthday");
        entity.addIntProperty("status");
    }

    private static void addMessageEntity(Schema schema){
        Entity entity = schema.addEntity("Message");
        entity.addIdProperty().autoincrement();
        entity.addStringProperty("content");
        entity.addIntProperty("userId");
        entity.addLongProperty("millisecond");
        entity.addDateProperty("createDate");
        entity.addIntProperty("status");
    }

}

你可能感兴趣的:(android)