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 {

    <E> int insert(E e);

    <E> boolean delete(E e);

    <E> boolean update(E e);

    <E> List<E> getAll(Class<E> clazz);

    <E> E get(Long id, Class<E> 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 <E> int insert(E e) {
        return Long.valueOf(daoSession.insert(e)).intValue();
    }

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

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

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

    @Override
    public <E> E get(Long id,Class<E> 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");
    }

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值