dao

配置greendao:http://www.jianshu.com/p/00d93c2d511f

创建DBManger:

public class DBManger {

    private final static String dbName = "user_db";
    private static DBManger mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;

    private DBManger(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManger getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManger.class) {
                if (mInstance == null) {
                    mInstance = new DBManger(context);
                }
            }
        }
        return mInstance;
    }

    public SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    public SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }
}

封装方法类:

public class MyUserDao {
    Context context;
    public MyUserDao(Context context) {
        this.context = context;
    }


    //增加
    public void addUser(UserBean mybean){
        DaoMaster master = new DaoMaster(DBManger.getInstance(context).getWritableDatabase());
        DaoSession daoSession = master.newSession();
        UserBeanDao userBeanDao = daoSession.getUserBeanDao();
        userBeanDao.insert(mybean);
    }

    //查询所有
    public List<UserBean> queryUser(){
        DaoMaster daoMaster = new DaoMaster(DBManger.getInstance(context).getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userBeanDao = daoSession.getUserBeanDao();
        QueryBuilder<UserBean> qb = userBeanDao.queryBuilder();
        List<UserBean> list = qb.list();
        return list;
    }

    /**
     * 查询用户列表
     */
    public List<UserBean> queryUserList(int age) {
        DaoMaster daoMaster = new DaoMaster(DBManger.getInstance(context).getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userBeanDao = daoSession.getUserBeanDao();
        QueryBuilder<UserBean> qb = userBeanDao.queryBuilder();
        qb.where(UserBeanDao.Properties.Age.gt(age)).orderAsc(UserBeanDao.Properties.Age);
        List<UserBean> list = qb.list();
        return list;
    }

    /**
     * 更新一条记录
     *
     */
    public void updateUser(UserBean mybean) {
        DaoMaster daoMaster = new DaoMaster(DBManger.getInstance(context).getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.update(mybean);
    }

    /**
     * 删除一条记录
     *
     */
    public void deleteUser(UserBean mybean) {
        DaoMaster daoMaster = new DaoMaster(DBManger.getInstance(context).getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.delete(mybean);
    }


}

mainactivity:

public class MainActivity extends AppCompatActivity {

    private Button btn1,btn2,btn3,btn4;
    private List<UserBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1= (Button) findViewById(R.id.add);
        btn2= (Button) findViewById(R.id.chaxun);
        btn3= (Button) findViewById(R.id.update);
        btn4= (Button) findViewById(R.id.del);

        final MyUserDao userDao = new MyUserDao(this);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //增加
                for (int i=11;i<21;i++){
                    UserBean bean = new UserBean();
                    bean.setId(i);
                    bean.setUname("张三"+i);
                    bean.setAge(1+i);
                    userDao.addUser(bean);
                    Log.i("xx","添加成功");
                }
            }
        });


        //查询
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = userDao.queryUser();
               for (int i = 0; i< list.size(); i++) {
                   Log.i("xx", list.get(i).getUname()+list.get(i).getId());
               }
            }
        });

        //删除
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (UserBean bean:list){
                    if(bean.getId()==18){
                        userDao.deleteUser(bean);
                    }
                }
            }
        });

        //修改
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (UserBean bean:list){
                    if (bean.getId() == 13) {
                        bean.setUname("修改");
                        userDao.updateUser(bean);
                    }
                }

            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值